formats.c
Go to the documentation of this file.
1 /*
2  * Filter layer - format negotiation
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/pixdesc.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 
30 {
31  int i;
32 
33  for(i = 0; i < a->refcount; i ++) {
34  ret->refs[ret->refcount] = a->refs[i];
35  *ret->refs[ret->refcount++] = ret;
36  }
37 
38  av_free(a->refs);
39  av_free(a->formats);
40  av_free(a);
41 }
42 
44 {
45  AVFilterFormats *ret;
46  unsigned i, j, k = 0, m_count;
47 
48  ret = av_mallocz(sizeof(AVFilterFormats));
49 
50  /* merge list of formats */
51  m_count = FFMIN(a->format_count, b->format_count);
52  if (m_count) {
53  ret->formats = av_malloc(sizeof(*ret->formats) * m_count);
54  for(i = 0; i < a->format_count; i ++)
55  for(j = 0; j < b->format_count; j ++)
56  if(a->formats[i] == b->formats[j])
57  ret->formats[k++] = a->formats[i];
58 
59  ret->format_count = k;
60  }
61  /* check that there was at least one common format */
62  if(!ret->format_count) {
63  av_free(ret->formats);
64  av_free(ret);
65  return NULL;
66  }
67 
68  ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
69 
70  merge_ref(ret, a);
71  merge_ref(ret, b);
72 
73  return ret;
74 }
75 
76 int ff_fmt_is_in(int fmt, const int *fmts)
77 {
78  const int *p;
79 
80  for (p = fmts; *p != PIX_FMT_NONE; p++) {
81  if (fmt == *p)
82  return 1;
83  }
84  return 0;
85 }
86 
88 {
89  AVFilterFormats *formats;
90  int count;
91 
92  for (count = 0; fmts[count] != -1; count++)
93  ;
94 
95  formats = av_mallocz(sizeof(AVFilterFormats));
96  if (count)
97  formats->formats = av_malloc(sizeof(*formats->formats) * count);
98  formats->format_count = count;
99  memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
100 
101  return formats;
102 }
103 
105 {
106  int *fmts;
107 
108  if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
109  return AVERROR(ENOMEM);
110 
111  fmts = av_realloc((*avff)->formats,
112  sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
113  if (!fmts)
114  return AVERROR(ENOMEM);
115 
116  (*avff)->formats = fmts;
117  (*avff)->formats[(*avff)->format_count++] = fmt;
118  return 0;
119 }
120 
122 {
123  AVFilterFormats *ret = NULL;
124  int fmt;
125  int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
126  type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
127 
128  for (fmt = 0; fmt < num_formats; fmt++)
129  if ((type != AVMEDIA_TYPE_VIDEO) ||
131  avfilter_add_format(&ret, fmt);
132 
133  return ret;
134 }
135 
137 {
138  *ref = f;
139  f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
140  f->refs[f->refcount-1] = ref;
141 }
142 
144 {
145  int i;
146  for(i = 0; i < (*ref)->refcount; i ++)
147  if((*ref)->refs[i] == ref)
148  return i;
149  return -1;
150 }
151 
153 {
154  int idx;
155 
156  if (!*ref)
157  return;
158 
159  idx = find_ref_index(ref);
160 
161  if(idx >= 0)
162  memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
163  sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
164 
165  if(!--(*ref)->refcount) {
166  av_free((*ref)->formats);
167  av_free((*ref)->refs);
168  av_free(*ref);
169  }
170  *ref = NULL;
171 }
172 
174  AVFilterFormats **newref)
175 {
176  int idx = find_ref_index(oldref);
177 
178  if(idx >= 0) {
179  (*oldref)->refs[idx] = newref;
180  *newref = *oldref;
181  *oldref = NULL;
182  }
183 }
184