1 /*
2 * Filter layer - format negotiation
3 * Copyright (c) 2007 Bobby Bingham
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
31
32 #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
33
34 /**
35 * Add all refs from a to ret and destroy a.
36 */
37 #define MERGE_REF(ret, a, fmts, type, fail) \
38 do { \
39 type ***tmp; \
40 int i; \
41 \
42 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
43 sizeof(*tmp)))) \
44 goto fail; \
45 ret->refs = tmp; \
46 \
47 for (i = 0; i < a->refcount; i ++) { \
48 ret->refs[ret->refcount] = a->refs[i]; \
49 *ret->refs[ret->refcount++] = ret; \
50 } \
51 \
52 av_freep(&a->refs); \
53 av_freep(&a->fmts); \
54 av_freep(&a); \
55 } while (0)
56
57 /**
58 * Add all formats common for a and b to ret, copy the refs and destroy
59 * a and b.
60 */
61 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
62 do { \
63 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
64 \
65 if (!(ret = av_mallocz(sizeof(*ret)))) \
66 goto fail; \
67 \
68 if (count) { \
69 if (!(ret->fmts = av_malloc_array(count, sizeof(*ret->fmts)))) \
70 goto fail; \
71 for (i = 0; i < a->nb; i++) \
72 for (j = 0; j < b->nb; j++) \
73 if (a->fmts[i] == b->fmts[j]) { \
74 if(k >= FFMIN(a->nb, b->nb)){ \
75 av_log(NULL, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
76 av_free(ret->fmts); \
77 av_free(ret); \
78 return NULL; \
79 } \
80 ret->fmts[k++] = a->fmts[i]; \
81 } \
82 } \
83 ret->nb = k; \
84 /* check that there was at least one common format */ \
85 if (!ret->nb) \
86 goto fail; \
87 \
88 MERGE_REF(ret, a, fmts, type, fail); \
89 MERGE_REF(ret, b, fmts, type, fail); \
90 } while (0)
91
94 {
96 int i, j;
97 int alpha1=0, alpha2=0;
98 int chroma1=0, chroma2=0;
99
100 if (a == b)
102
103 /* Do not lose chroma or alpha in merging.
104 It happens if both lists have formats with chroma (resp. alpha), but
105 the only formats in common do not have it (e.g. YUV+gray vs.
106 RGB+gray): in that case, the merging would select the gray format,
107 possibly causing a lossy conversion elsewhere in the graph.
108 To avoid that, pretend that there are no common formats to force the
109 insertion of a conversion filter. */
120 }
121 }
122
123 // If chroma or alpha can be lost through merging then do not merge
124 if (alpha2 > alpha1 || chroma2 > chroma1)
126
128
130 fail:
131 if (ret) {
134 }
137 }
138
141 {
143
144 if (a == b)
return a;
145
151 } else {
154 }
155
157 fail:
158 if (ret) {
161 }
164 }
165
168 {
172 int ret_max, ret_nb = 0, i, j,
round;
173
174 if (a == b)
return a;
175
176 /* Put the most generic set in a, to avoid doing everything twice */
177 if (a_all < b_all) {
179 FFSWAP(
unsigned, a_all, b_all);
180 }
181 if (a_all) {
182 if (a_all == 1 && !b_all) {
183 /* keep only known layouts in b; works also for b_all = 1 */
187 /* Not optimal: the unknown layouts of b may become known after
188 another merge. */
189 if (!j)
192 }
195 }
196
201 goto fail;
202
203 /* a[known] intersect b[known] */
206 continue;
211 }
212 }
213 }
214 /* 1st round: a[known] intersect b[generic]
215 2nd round: a[generic] intersect b[known] */
216 for (round = 0; round < 2; round++) {
219 if (!fmt || !
KNOWN(fmt))
220 continue;
225 }
226 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
228 }
229 /* a[generic] intersect b[generic] */
232 continue;
236 }
237
240 goto fail;
244
245 fail:
246 if (ret) {
249 }
252 }
253
255 {
256 const int *p;
257
258 for (p = fmts; *p != -1; p++) {
259 if (fmt == *p)
260 return 1;
261 }
262 return 0;
263 }
264
265 #define COPY_INT_LIST(list_copy, list, type) { \
266 int count = 0; \
267 if (list) \
268 for (count = 0; list[count] != -1; count++) \
269 ; \
270 list_copy = av_calloc(count+1, sizeof(type)); \
271 if (list_copy) { \
272 memcpy(list_copy, list, sizeof(type) * count); \
273 list_copy[count] = -1; \
274 } \
275 }
276
277 #define MAKE_FORMAT_LIST(type, field, count_field) \
278 type *formats; \
279 int count = 0; \
280 if (fmts) \
281 for (count = 0; fmts[count] != -1; count++) \
282 ; \
283 formats = av_mallocz(sizeof(*formats)); \
284 if (!formats) return NULL; \
285 formats->count_field = count; \
286 if (count) { \
287 formats->field = av_malloc_array(count, sizeof(*formats->field)); \
288 if (!formats->field) { \
289 av_free(formats); \
290 return NULL; \
291 } \
292 }
293
295 {
299
301 }
302
304 {
306 channel_layouts, nb_channel_layouts);
308 memcpy(
formats->channel_layouts, fmts,
310
312 }
313
314 #define ADD_FORMAT(f, fmt, type, list, nb) \
315 do { \
316 type *fmts; \
317 void *oldf = *f; \
318 \
319 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
320 return AVERROR(ENOMEM); \
321 \
322 fmts = av_realloc((*f)->list, \
323 sizeof(*(*f)->list) * ((*f)->nb + 1));\
324 if (!fmts) { \
325 if (!oldf) \
326 av_freep(f); \
327 return AVERROR(ENOMEM); \
328 } \
329 \
330 (*f)->list = fmts; \
331 (*f)->list[(*f)->nb++] = fmt; \
332 } while (0)
333
335 {
337 return 0;
338 }
339
341 {
343 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
344 return 0;
345 }
346
348 {
350
356 }
361 fmt++;
362 }
363 }
364
366 }
367
370 -1
371 };
372
373 // AVFilterFormats *avfilter_make_all_channel_layouts(void)
374 // {
375 // return avfilter_make_format64_list(avfilter_all_channel_layouts);
376 // }
377
379 {
382
386
388 }
389
391 {
394 }
395
397 {
399 if (!ret)
403 }
404
406 {
408 if (!ret)
412 }
413
414 #define FORMATS_REF(f, ref) \
415 do { \
416 *ref = f; \
417 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
418 f->refs[f->refcount-1] = ref; \
419 } while (0)
420
422 {
424 }
425
427 {
429 }
430
431 #define FIND_REF_INDEX(ref, idx) \
432 do { \
433 int i; \
434 for (i = 0; i < (*ref)->refcount; i ++) \
435 if((*ref)->refs[i] == ref) { \
436 idx = i; \
437 break; \
438 } \
439 } while (0)
440
441 #define FORMATS_UNREF(ref, list) \
442 do { \
443 int idx = -1; \
444 \
445 if (!*ref) \
446 return; \
447 \
448 FIND_REF_INDEX(ref, idx); \
449 \
450 if (idx >= 0) \
451 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
452 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
453 \
454 if(!--(*ref)->refcount) { \
455 av_free((*ref)->list); \
456 av_free((*ref)->refs); \
457 av_free(*ref); \
458 } \
459 *ref = NULL; \
460 } while (0)
461
463 {
465 }
466
468 {
470 }
471
472 #define FORMATS_CHANGEREF(oldref, newref) \
473 do { \
474 int idx = -1; \
475 \
476 FIND_REF_INDEX(oldref, idx); \
477 \
478 if (idx >= 0) { \
479 (*oldref)->refs[idx] = newref; \
480 *newref = *oldref; \
481 *oldref = NULL; \
482 } \
483 } while (0)
484
487 {
489 }
490
492 {
494 }
495
496 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
497 if (fmts) { \
498 int count = 0, i; \
499 \
500 for (i = 0; i < ctx->nb_inputs; i++) { \
501 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
502 ref(fmts, &ctx->inputs[i]->out_fmts); \
503 count++; \
504 } \
505 } \
506 for (i = 0; i < ctx->nb_outputs; i++) { \
507 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
508 ref(fmts, &ctx->outputs[i]->in_fmts); \
509 count++; \
510 } \
511 } \
512 \
513 if (!count) { \
514 av_freep(&fmts->list); \
515 av_freep(&fmts->refs); \
516 av_freep(&fmts); \
517 } \
518 }
519
522 {
525 }
526
529 {
532 }
533
534 /**
535 * A helper for query_formats() which sets all links to the same list of
536 * formats. If there are no links hooked to this filter, the list of formats is
537 * freed.
538 */
540 {
543 }
544
547 {
551
556 }
557
558 return 0;
559 }
560
562 {
564 }
565
567 {
569 }
570
571 /* internal functions for parsing audio format arguments */
572
574 {
575 char *tail;
578 pix_fmt = strtol(arg, &tail, 0);
582 }
583 }
585 return 0;
586 }
587
589 {
590 char *tail;
593 sfmt = strtol(arg, &tail, 0);
597 }
598 }
599 *ret = sfmt;
600 return 0;
601 }
602
604 {
609 }
611 return 0;
612 }
613
615 {
616 char *tail;
618 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
621 }
622 *ret = srate;
623 return 0;
624 }
625
627 void *log_ctx)
628 {
629 char *tail;
630 int64_t chlayout,
count;
631
632 if (nret) {
633 count = strtol(arg, &tail, 10);
634 if (*tail == 'c' && !tail[1] && count > 0 && count < 63) {
636 *ret = 0;
637 return 0;
638 }
639 }
641 if (chlayout == 0) {
642 chlayout = strtol(arg, &tail, 10);
643 if (*tail || chlayout == 0) {
646 }
647 }
648 *ret = chlayout;
649 if (nret)
651 return 0;
652 }
653
654 #ifdef TEST
655
656 #undef printf
657
659 {
660 const int64_t *cl;
662
665 printf("%s\n", buf);
666 }
667
668 return 0;
669 }
670
671 #endif
672