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
30
31 /**
32 * Add all refs from a to ret and destroy a.
33 */
34 #define MERGE_REF(ret, a, fmts, type, fail_statement) \
35 do { \
36 type ***tmp; \
37 int i; \
38 \
39 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
40 sizeof(*tmp)))) \
41 { fail_statement } \
42 ret->refs = tmp; \
43 \
44 for (i = 0; i < a->refcount; i ++) { \
45 ret->refs[ret->refcount] = a->refs[i]; \
46 *ret->refs[ret->refcount++] = ret; \
47 } \
48 \
49 av_freep(&a->refs); \
50 av_freep(&a->fmts); \
51 av_freep(&a); \
52 } while (0)
53
54 /**
55 * Add all formats common to a and b to a, add b's refs to a and destroy b.
56 * If check is set, nothing is modified and it is only checked whether
57 * the formats are compatible.
58 * If empty_allowed is set and one of a,b->nb is zero, the lists are
59 * merged; otherwise, 0 (for nonmergeability) is returned.
60 */
61 #define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed) \
62 do { \
63 int i, j, k = 0, skip = 0; \
64 \
65 if (empty_allowed) { \
66 if (!a->nb || !b->nb) { \
67 if (check) \
68 return 1; \
69 if (!a->nb) \
70 FFSWAP(type *, a, b); \
71 skip = 1; \
72 } \
73 } \
74 if (!skip) { \
75 for (i = 0; i < a->nb; i++) \
76 for (j = 0; j < b->nb; j++) \
77 if (a->fmts[i] == b->fmts[j]) { \
78 if (check) \
79 return 1; \
80 a->fmts[k++] = a->fmts[i]; \
81 break; \
82 } \
83 /* Check that there was at least one common format. \
84 * Notice that both a and b are unchanged if not. */ \
85 if (!k) \
86 return 0; \
87 av_assert2(!check); \
88 a->nb = k; \
89 } \
90 \
91 MERGE_REF(a, b, fmts, type, return AVERROR(ENOMEM);); \
92 } while (0)
96 {
98 int alpha1=0, alpha2=0;
99 int chroma1=0, chroma2=0;
100
102
104 return 1;
105
106 /* Do not lose chroma or alpha in merging.
107 It happens if both lists have formats with chroma (resp. alpha), but
108 the only formats in common do not have it (e.g. YUV+gray vs.
109 RGB+gray): in that case, the merging would select the gray format,
110 possibly causing a lossy conversion elsewhere in the graph.
111 To avoid that, pretend that there are no common formats to force the
112 insertion of a conversion filter. */
114 for (
i = 0;
i <
a->nb_formats;
i++) {
116 for (j = 0; j <
b->nb_formats; j++) {
120 if (
a->formats[
i] ==
b->formats[j]) {
123 }
124 }
125 }
126
127 // If chroma or alpha can be lost through merging then do not merge
128 if (alpha2 > alpha1 || chroma2 > chroma1)
129 return 0;
130
132
133 return 1;
134 }
135
136
137 /**
138 * Check the formats lists for compatibility for merging without actually
139 * merging.
140 *
141 * @return 1 if they are compatible, 0 if not.
144 {
147 }
148
149 /**
150 * Merge the formats lists if they are compatible and update all the
151 * references of a and b to point to the combined list and free the old
152 * lists as needed. The combined list usually contains the intersection of
153 * the lists of a and b.
154 *
155 * Both a and b must have owners (i.e. refcount > 0) for these functions.
156 *
157 * @return 1 if merging succeeded, 0 if a and b are incompatible
158 * and negative AVERROR code on failure.
159 * a and b are unmodified if 0 is returned.
162 {
164 }
165
166 /**
167 * See can_merge_pix_fmts().
170 {
173 }
174
175 /**
176 * See merge_pix_fmts().
179 {
181 }
185 {
187 if (
a ==
b)
return 1;
188
190 return 1;
191 }
192
193 /**
194 * See can_merge_pix_fmts().
197 {
199 }
200
201 /**
202 * See merge_pix_fmts().
205 {
207 }
208
209 /**
210 * See merge_pix_fmts().
214 {
216 unsigned a_all =
a->all_layouts +
a->all_counts;
217 unsigned b_all =
b->all_layouts +
b->all_counts;
218 int ret_max, ret_nb = 0,
i, j,
round;
219
221
222 if (
a ==
b)
return 1;
223
224 /* Put the most generic set in a, to avoid doing everything twice */
225 if (a_all < b_all) {
227 FFSWAP(
unsigned, a_all, b_all);
228 }
229 if (a_all) {
230 if (a_all == 1 && !b_all) {
231 /* keep only known layouts in b; works also for b_all = 1 */
232 for (
i = j = 0;
i <
b->nb_channel_layouts;
i++)
233 if (
KNOWN(&
b->channel_layouts[
i]) &&
i != j++) {
235 return 1;
237 }
238 /* Not optimal: the unknown layouts of b may become known after
239 another merge. */
240 if (!j)
241 return 0;
242 b->nb_channel_layouts = j;
243 }
245 return 1;
246 }
247
248 ret_max =
a->nb_channel_layouts +
b->nb_channel_layouts;
251
252 /* a[known] intersect b[known] */
253 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
254 if (!
KNOWN(&
a->channel_layouts[
i]))
255 continue;
256 for (j = 0; j <
b->nb_channel_layouts; j++) {
259 return 1;
263 break;
264 }
265 }
266 }
267 /* 1st round: a[known] intersect b[generic]
268 2nd round: a[generic] intersect b[known] */
270 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
273 continue;
275 for (j = 0; j <
b->nb_channel_layouts; j++)
278 return 1;
280 }
281 }
282 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
284 }
285 /* a[generic] intersect b[generic] */
286 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
287 if (
KNOWN(&
a->channel_layouts[
i]))
288 continue;
289 for (j = 0; j <
b->nb_channel_layouts; j++)
292 return 1;
294 }
295 }
296
297 if (!ret_nb) {
299 return 0;
300 }
301
302 if (
a->refcount >
b->refcount)
304
309 b->nb_channel_layouts = ret_nb;
310 return 1;
311 }
314 {
317 }
320 {
322 }
326 {
328
330 return 1;
331
333
334 return 1;
335 }
338 {
341 }
344 {
346 }
349 {
353 },
354 {
358 },
359 {
363 },
364 };
367 {
371 },
372 {
376 },
377 {
381 },
382 };
387 .conversion_filter = "scale",
389 };
394 .conversion_filter = "aresample",
396 };
399 {
400 switch (
link->type) {
403 default:
return NULL;
404 }
405 }
408 {
409 const int *p;
410
411 for (p = fmts; *p != -1; p++) {
412 if (fmt == *p)
413 return 1;
414 }
415 return 0;
416 }
418 #define MAKE_FORMAT_LIST(type, field, count_field) \
419 type *formats; \
420 int count = 0; \
421 if (fmts) \
422 for (count = 0; fmts[count] != -1; count++) \
423 ; \
424 formats = av_mallocz(sizeof(*formats)); \
425 if (!formats) \
426 return NULL; \
427 formats->count_field = count; \
428 if (count) { \
429 formats->field = av_malloc_array(count, sizeof(*formats->field)); \
430 if (!formats->field) { \
431 av_freep(&formats); \
432 return NULL; \
433 } \
434 }
437 {
439 while (count--)
440 formats->formats[count] = fmts[count];
441
443 }
446 {
448 int count = 0;
449 if (fmts)
451 ;
456 if (count) {
462 }
463 for (
int i = 0;
i < count;
i++) {
467 }
468 }
469
471
473 for (
int i = 0;
i < count;
i++)
477
479 }
481 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
482 do { \
483 type *fmts; \
484 \
485 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
486 return AVERROR(ENOMEM); \
487 } \
488 \
489 fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
490 sizeof(*(*f)->list)); \
491 if (!fmts) { \
492 unref_fn(f); \
493 return AVERROR(ENOMEM); \
494 } \
495 \
496 (*f)->list = fmts; \
497 ASSIGN_FMT(f, fmt, list, nb); \
498 } while (0)
499
500 #define ASSIGN_FMT(f, fmt, list, nb) \
501 do { \
502 (*f)->list[(*f)->nb++] = fmt; \
503 } while (0)
506 {
508 return 0;
509 }
510
512 #define ASSIGN_FMT(f, fmt, list, nb) \
513 do { \
514 int ret; \
515 memset((*f)->list + (*f)->nb, 0, sizeof(*(*f)->list)); \
516 ret = av_channel_layout_copy(&(*f)->list[(*f)->nb], fmt); \
517 if (ret < 0) \
518 return ret; \
519 (*f)->nb++; \
520 } while (0)
524 {
527 return 0;
528 }
531 {
532 int fmts[2] = { fmt, -1 };
534 }
537 {
539
547 fmt++;
548 }
549 }
550
552 }
555 {
556 unsigned nb_formats, fmt,
flags;
558
559 while (1) {
560 nb_formats = 0;
561 for (fmt = 0;; fmt++) {
564 break;
568 (
desc->log2_chroma_w ||
desc->log2_chroma_h))
570 if ((
flags & (want | rej)) != want)
571 continue;
573 formats->formats[nb_formats] = fmt;
574 nb_formats++;
575 }
579 }
583 formats->nb_formats = nb_formats;
584 if (nb_formats) {
589 }
590 }
591 }
592 }
595 {
597 int fmt;
598
603
605 }
608 {
611 }
614 {
618 ret->all_layouts = 1;
620 }
623 {
627 ret->all_layouts =
ret->all_counts = 1;
629 }
632 {
639 continue;
642 }
643
645 }
648 {
653 }
654
656 }
658 #define FORMATS_REF(f, ref, unref_fn) \
659 void *tmp; \
660 \
661 if (!f) \
662 return AVERROR(ENOMEM); \
663 \
664 tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1); \
665 if (!tmp) { \
666 unref_fn(&f); \
667 return AVERROR(ENOMEM); \
668 } \
669 f->refs = tmp; \
670 f->refs[f->refcount++] = ref; \
671 *ref = f; \
672 return 0
675 {
677 }
680 {
682 }
684 #define FIND_REF_INDEX(ref, idx) \
685 do { \
686 int i; \
687 for (i = 0; i < (*ref)->refcount; i ++) \
688 if((*ref)->refs[i] == ref) { \
689 idx = i; \
690 break; \
691 } \
692 } while (0)
694 #define FORMATS_UNREF(ref, list) \
695 do { \
696 int idx = -1; \
697 \
698 if (!*ref) \
699 return; \
700 \
701 FIND_REF_INDEX(ref, idx); \
702 \
703 if (idx >= 0) { \
704 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
705 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
706 --(*ref)->refcount; \
707 } \
708 if (!(*ref)->refcount) { \
709 FREE_LIST(ref, list); \
710 av_free((*ref)->list); \
711 av_free((*ref)->refs); \
712 av_free(*ref); \
713 } \
714 *ref = NULL; \
715 } while (0)
716
717 #define FREE_LIST(ref, list) do { } while(0)
719 {
721 }
722
724 #define FREE_LIST(ref, list) \
725 do { \
726 for (int i = 0; i < (*ref)->nb_channel_layouts; i++) \
727 av_channel_layout_uninit(&(*ref)->list[i]); \
728 } while(0)
731 {
733 }
735 #define FORMATS_CHANGEREF(oldref, newref) \
736 do { \
737 int idx = -1; \
738 \
739 FIND_REF_INDEX(oldref, idx); \
740 \
741 if (idx >= 0) { \
742 (*oldref)->refs[idx] = newref; \
743 *newref = *oldref; \
744 *oldref = NULL; \
745 } \
746 } while (0)
750 {
752 }
755 {
757 }
759 #define SET_COMMON_FORMATS(ctx, fmts, media_type, ref_fn, unref_fn) \
760 int i; \
761 \
762 if (!fmts) \
763 return AVERROR(ENOMEM); \
764 \
765 for (i = 0; i < ctx->nb_inputs; i++) { \
766 AVFilterLink *const link = ctx->inputs[i]; \
767 if (link && !link->outcfg.fmts && \
768 (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
769 int ret = ref_fn(fmts, &ctx->inputs[i]->outcfg.fmts); \
770 if (ret < 0) { \
771 return ret; \
772 } \
773 } \
774 } \
775 for (i = 0; i < ctx->nb_outputs; i++) { \
776 AVFilterLink *const link = ctx->outputs[i]; \
777 if (link && !link->incfg.fmts && \
778 (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
779 int ret = ref_fn(fmts, &ctx->outputs[i]->incfg.fmts); \
780 if (ret < 0) { \
781 return ret; \
782 } \
783 } \
784 } \
785 \
786 if (!fmts->refcount) \
787 unref_fn(&fmts); \
788 \
789 return 0;
793 {
796 }
800 {
802 }
805 {
807 }
811 {
814 }
817 const int *samplerates)
818 {
820 }
823 {
825 }
829 {
832 }
835 const int *color_ranges)
836 {
838 }
841 {
843 }
847 {
850 }
853 const int *color_ranges)
854 {
856 }
859 {
861 }
862
863 /**
864 * A helper for query_formats() which sets all links to the same list of
865 * formats. If there are no links hooked to this filter, the list of formats is
866 * freed.
869 {
872 }
875 {
877 }
879 #define SET_COMMON_FORMATS2(ctx, cfg_in, cfg_out, fmts, media_type, \
880 ref_fn, unref_fn) \
881 if (!fmts) \
882 return AVERROR(ENOMEM); \
883 \
884 for (unsigned i = 0; i < ctx->nb_inputs; i++) { \
885 const AVFilterLink *const link = ctx->inputs[i]; \
886 if (!cfg_in[i]->fmts && \
887 (media_type == AVMEDIA_TYPE_UNKNOWN || \
888 link->type == media_type)) { \
889 int ret = ref_fn(fmts, &cfg_in[i]->fmts); \
890 if (ret < 0) { \
891 return ret; \
892 } \
893 } \
894 } \
895 for (unsigned i = 0; i < ctx->nb_outputs; i++) { \
896 const AVFilterLink *const link = ctx->outputs[i]; \
897 if (!cfg_out[i]->fmts && \
898 (media_type == AVMEDIA_TYPE_UNKNOWN || \
899 link->type == media_type)) { \
900 int ret = ref_fn(fmts, &cfg_out[i]->fmts); \
901 if (ret < 0) { \
902 return ret; \
903 } \
904 } \
905 } \
906 \
907 if (!fmts->refcount) \
908 unref_fn(&fmts); \
909 \
910 return 0;
916 {
919 }
925 {
927 }
932 {
934 }
940 {
943 }
948 const int *samplerates)
949 {
951 }
956 {
958 }
964 {
967 }
972 const int *color_ranges)
973 {
975 }
980 {
982 }
988 {
991 }
996 const int *color_ranges)
997 {
999 }
1004 {
1006 }
1012 {
1015 }
1020 const int *fmts)
1021 {
1023 }
1024
1027 {
1032
1033 switch (
f->formats_state) {
1037 break;
1041 break;
1045 break;
1049 break;
1050 default:
1052 /* Intended fallthrough */
1058 ctx->nb_outputs ?
ctx->outputs[0]->type :
1060 break;
1061 }
1062
1073 }
1081 }
1082
1083 return 0;
1084 }
1087 {
1089
1090 if (!fmts)
1091 return 0;
1095 }
1101 }
1102 }
1103 }
1104 return 0;
1105 }
1108 {
1110 }
1113 {
1115 }
1118 {
1120 return 0;
1122 }
1125 {
1130 }
1131 }
1133 }
1136 {
1138 }
1141 {
1145 }
1148 {
1150
1151 if (!fmts)
1152 return 0;
1156 }
1160 }
1166 }
1167 }
1168 }
1169 return 0;
1170 }