Libav
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/common.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "formats.h"
27 
31 #define MERGE_REF(ret, a, fmts, type, fail) \
32 do { \
33  type ***tmp; \
34  int i; \
35  \
36  if (!(tmp = av_realloc(ret->refs, \
37  sizeof(*tmp) * (ret->refcount + a->refcount)))) \
38  goto fail; \
39  ret->refs = tmp; \
40  \
41  for (i = 0; i < a->refcount; i ++) { \
42  ret->refs[ret->refcount] = a->refs[i]; \
43  *ret->refs[ret->refcount++] = ret; \
44  } \
45  \
46  av_freep(&a->refs); \
47  av_freep(&a->fmts); \
48  av_freep(&a); \
49 } while (0)
50 
55 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
56 do { \
57  int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
58  \
59  if (!(ret = av_mallocz(sizeof(*ret)))) \
60  goto fail; \
61  \
62  if (count) { \
63  if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
64  goto fail; \
65  for (i = 0; i < a->nb; i++) \
66  for (j = 0; j < b->nb; j++) \
67  if (a->fmts[i] == b->fmts[j]) \
68  ret->fmts[k++] = a->fmts[i]; \
69  \
70  ret->nb = k; \
71  } \
72  /* check that there was at least one common format */ \
73  if (!ret->nb) \
74  goto fail; \
75  \
76  MERGE_REF(ret, a, fmts, type, fail); \
77  MERGE_REF(ret, b, fmts, type, fail); \
78 } while (0)
79 
81 {
82  AVFilterFormats *ret = NULL;
83 
84  if (a == b)
85  return a;
86 
87  MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
88 
89  return ret;
90 fail:
91  if (ret) {
92  av_freep(&ret->refs);
93  av_freep(&ret->formats);
94  }
95  av_freep(&ret);
96  return NULL;
97 }
98 
101 {
102  AVFilterFormats *ret = NULL;
103 
104  if (a == b) return a;
105 
106  if (a->nb_formats && b->nb_formats) {
107  MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
108  } else if (a->nb_formats) {
109  MERGE_REF(a, b, formats, AVFilterFormats, fail);
110  ret = a;
111  } else {
112  MERGE_REF(b, a, formats, AVFilterFormats, fail);
113  ret = b;
114  }
115 
116  return ret;
117 fail:
118  if (ret) {
119  av_freep(&ret->refs);
120  av_freep(&ret->formats);
121  }
122  av_freep(&ret);
123  return NULL;
124 }
125 
128 {
130 
131  if (a == b) return a;
132 
133  if (a->nb_channel_layouts && b->nb_channel_layouts) {
134  MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
135  AVFilterChannelLayouts, fail);
136  } else if (a->nb_channel_layouts) {
137  MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
138  ret = a;
139  } else {
140  MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
141  ret = b;
142  }
143 
144  return ret;
145 fail:
146  if (ret) {
147  av_freep(&ret->refs);
148  av_freep(&ret->channel_layouts);
149  }
150  av_freep(&ret);
151  return NULL;
152 }
153 
154 int ff_fmt_is_in(int fmt, const int *fmts)
155 {
156  const int *p;
157 
158  for (p = fmts; *p != AV_PIX_FMT_NONE; p++) {
159  if (fmt == *p)
160  return 1;
161  }
162  return 0;
163 }
164 
166 {
168  int count;
169 
170  for (count = 0; fmts[count] != -1; count++)
171  ;
172 
173  formats = av_mallocz(sizeof(*formats));
174  if (count)
175  formats->formats = av_malloc(sizeof(*formats->formats) * count);
176  formats->nb_formats = count;
177  memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
178 
179  return formats;
180 }
181 
182 #define ADD_FORMAT(f, fmt, type, list, nb) \
183 do { \
184  type *fmts; \
185  \
186  if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
187  return AVERROR(ENOMEM); \
188  \
189  fmts = av_realloc((*f)->list, \
190  sizeof(*(*f)->list) * ((*f)->nb + 1));\
191  if (!fmts) \
192  return AVERROR(ENOMEM); \
193  \
194  (*f)->list = fmts; \
195  (*f)->list[(*f)->nb++] = fmt; \
196  return 0; \
197 } while (0)
198 
199 int ff_add_format(AVFilterFormats **avff, int fmt)
200 {
201  ADD_FORMAT(avff, fmt, int, formats, nb_formats);
202 }
203 
204 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
205 {
206  ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
207 }
208 
210 {
211  AVFilterFormats *ret = NULL;
212 
213  if (type == AVMEDIA_TYPE_VIDEO) {
214  const AVPixFmtDescriptor *desc = NULL;
215  while ((desc = av_pix_fmt_desc_next(desc))) {
216  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
218  }
219  } else if (type == AVMEDIA_TYPE_AUDIO) {
220  enum AVSampleFormat fmt = 0;
221  while (av_get_sample_fmt_name(fmt)) {
222  ff_add_format(&ret, fmt);
223  fmt++;
224  }
225  }
226 
227  return ret;
228 }
229 
231 {
232  AVFilterFormats *ret = NULL;
233  int fmt;
234 
235  for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
236  if (av_sample_fmt_is_planar(fmt))
237  ff_add_format(&ret, fmt);
238 
239  return ret;
240 }
241 
243 {
244  AVFilterFormats *ret = av_mallocz(sizeof(*ret));
245  return ret;
246 }
247 
249 {
250  AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
251  return ret;
252 }
253 
254 #define FORMATS_REF(f, ref) \
255 do { \
256  *ref = f; \
257  f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
258  f->refs[f->refcount-1] = ref; \
259 } while (0)
260 
262 {
263  FORMATS_REF(f, ref);
264 }
265 
267 {
268  FORMATS_REF(f, ref);
269 }
270 
271 #define FIND_REF_INDEX(ref, idx) \
272 do { \
273  int i; \
274  for (i = 0; i < (*ref)->refcount; i ++) \
275  if((*ref)->refs[i] == ref) { \
276  idx = i; \
277  break; \
278  } \
279 } while (0)
280 
281 #define FORMATS_UNREF(ref, list) \
282 do { \
283  int idx = -1; \
284  \
285  if (!*ref) \
286  return; \
287  \
288  FIND_REF_INDEX(ref, idx); \
289  \
290  if (idx >= 0) \
291  memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
292  sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
293  \
294  if(!--(*ref)->refcount) { \
295  av_free((*ref)->list); \
296  av_free((*ref)->refs); \
297  av_free(*ref); \
298  } \
299  *ref = NULL; \
300 } while (0)
301 
303 {
304  FORMATS_UNREF(ref, formats);
305 }
306 
308 {
309  FORMATS_UNREF(ref, channel_layouts);
310 }
311 
312 #define FORMATS_CHANGEREF(oldref, newref) \
313 do { \
314  int idx = -1; \
315  \
316  FIND_REF_INDEX(oldref, idx); \
317  \
318  if (idx >= 0) { \
319  (*oldref)->refs[idx] = newref; \
320  *newref = *oldref; \
321  *oldref = NULL; \
322  } \
323 } while (0)
324 
326  AVFilterChannelLayouts **newref)
327 {
328  FORMATS_CHANGEREF(oldref, newref);
329 }
330 
332 {
333  FORMATS_CHANGEREF(oldref, newref);
334 }
335 
336 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
337 { \
338  int count = 0, i; \
339  \
340  for (i = 0; i < ctx->nb_inputs; i++) { \
341  if (ctx->inputs[i]) { \
342  ref(fmts, &ctx->inputs[i]->out_fmts); \
343  count++; \
344  } \
345  } \
346  for (i = 0; i < ctx->nb_outputs; i++) { \
347  if (ctx->outputs[i]) { \
348  ref(fmts, &ctx->outputs[i]->in_fmts); \
349  count++; \
350  } \
351  } \
352  \
353  if (!count) { \
354  av_freep(&fmts->list); \
355  av_freep(&fmts->refs); \
356  av_freep(&fmts); \
357  } \
358 }
359 
362 {
363  SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
364  ff_channel_layouts_ref, channel_layouts);
365 }
366 
368  AVFilterFormats *samplerates)
369 {
370  SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
372 }
373 
380 {
381  SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
382  ff_formats_ref, formats);
383 }
384 
386 {
387  enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
388  ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
390 
392  if (type == AVMEDIA_TYPE_AUDIO) {
395  }
396 
397  return 0;
398 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Number of sample formats. DO NOT USE if linking dynamically.
Definition: samplefmt.h:75
#define FORMATS_UNREF(ref, list)
Definition: formats.c:281
Main libavfilter public API header.
void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref, AVFilterChannelLayouts **newref)
Definition: formats.c:325
static enum AVSampleFormat formats[]
struct AVFilterChannelLayouts *** refs
references to this list
Definition: formats.h:77
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
#define b
Definition: input.c:52
struct AVFilterFormats *** refs
references to this list
Definition: formats.h:69
void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:261
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:379
int ff_fmt_is_in(int fmt, const int *fmts)
Tell is a format is contained in the provided list terminated by -1.
Definition: formats.c:154
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by Libav for the given media type.
Definition: formats.c:209
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:101
void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
Before After |formats |<------—.
Definition: formats.c:331
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:204
uint64_t * channel_layouts
list of channel layouts
Definition: formats.h:73
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:120
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:47
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:1615
#define ADD_FORMAT(f, fmt, type, list, nb)
Definition: formats.c:182
unsigned nb_formats
number of formats
Definition: formats.h:65
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:230
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout/s...
Definition: formats.c:248
int ff_default_query_formats(AVFilterContext *ctx)
Definition: formats.c:385
NULL
Definition: eval.c:55
#define FORMATS_REF(f, ref)
Definition: formats.c:254
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:266
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
#define FORMATS_CHANGEREF(oldref, newref)
Definition: formats.c:312
AVFilterChannelLayouts * ff_merge_channel_layouts(AVFilterChannelLayouts *a, AVFilterChannelLayouts *b)
Return a channel layouts/samplerates list which contains the intersection of the layouts/samplerates ...
Definition: formats.c:126
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:307
AVFilterFormats * ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
Return a format list which contains the intersection of the formats of a and b.
Definition: formats.c:80
AVMediaType
Definition: avutil.h:185
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:302
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:433
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:242
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:367
common internal and external API header
int nb_channel_layouts
number of channel layouts
Definition: formats.h:74
int ff_add_format(AVFilterFormats **avff, int fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:199
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:563
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:360
#define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list)
Definition: formats.c:336
AVFilterFormats * ff_merge_samplerates(AVFilterFormats *a, AVFilterFormats *b)
Definition: formats.c:99
internal API functions
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:1606
int * formats
list of media formats
Definition: formats.h:66
#define MERGE_REF(ret, a, fmts, type, fail)
Add all refs from a to ret and destroy a.
Definition: formats.c:31
#define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail)
Add all formats common for a and b to ret, copy the refs and destroy a and b.
Definition: formats.c:55