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 /**
33 * Add all refs from a to ret and destroy a.
34 */
35 #define MERGE_REF(ret, a, fmts, type, fail_statement) \
36 do { \
37 type ***tmp; \
38 int i; \
39 \
40 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
41 sizeof(*tmp)))) \
42 { fail_statement } \
43 ret->refs = tmp; \
44 \
45 for (i = 0; i < a->refcount; i ++) { \
46 ret->refs[ret->refcount] = a->refs[i]; \
47 *ret->refs[ret->refcount++] = ret; \
48 } \
49 \
50 av_freep(&a->refs); \
51 av_freep(&a->fmts); \
52 av_freep(&a); \
53 } while (0)
54
55 /**
56 * Add all formats common to a and b to a, add b's refs to a and destroy b.
57 * If check is set, nothing is modified and it is only checked whether
58 * the formats are compatible.
59 * If empty_allowed is set and one of a,b->nb is zero, the lists are
60 * merged; otherwise, 0 (for nonmergeability) is returned.
61 */
62 #define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed) \
63 do { \
64 int i, j, k = 0, skip = 0; \
65 \
66 if (empty_allowed) { \
67 if (!a->nb || !b->nb) { \
68 if (check) \
69 return 1; \
70 if (!a->nb) \
71 FFSWAP(type *, a, b); \
72 skip = 1; \
73 } \
74 } \
75 if (!skip) { \
76 for (i = 0; i < a->nb; i++) \
77 for (j = 0; j < b->nb; j++) \
78 if (a->fmts[i] == b->fmts[j]) { \
79 if (check) \
80 return 1; \
81 a->fmts[k++] = a->fmts[i]; \
82 break; \
83 } \
84 /* Check that there was at least one common format. \
85 * Notice that both a and b are unchanged if not. */ \
86 if (!k) \
87 return 0; \
88 av_assert2(!check); \
89 a->nb = k; \
90 } \
91 \
92 MERGE_REF(a, b, fmts, type, return AVERROR(ENOMEM);); \
93 } while (0)
97 {
99 int alpha1=0, alpha2=0;
100 int chroma1=0, chroma2=0;
101
103
105 return 1;
106
107 /* Do not lose chroma or alpha in merging.
108 It happens if both lists have formats with chroma (resp. alpha), but
109 the only formats in common do not have it (e.g. YUV+gray vs.
110 RGB+gray): in that case, the merging would select the gray format,
111 possibly causing a lossy conversion elsewhere in the graph.
112 To avoid that, pretend that there are no common formats to force the
113 insertion of a conversion filter. */
115 for (
i = 0;
i <
a->nb_formats;
i++) {
117 for (j = 0; j <
b->nb_formats; j++) {
121 if (
a->formats[
i] ==
b->formats[j]) {
124 }
125 }
126 }
127
128 // If chroma or alpha can be lost through merging then do not merge
129 if (alpha2 > alpha1 || chroma2 > chroma1)
130 return 0;
131
133
134 return 1;
135 }
136
137
138 /**
139 * Check the formats lists for compatibility for merging without actually
140 * merging.
141 *
142 * @return 1 if they are compatible, 0 if not.
145 {
148 }
149
150 /**
151 * Merge the formats lists if they are compatible and update all the
152 * references of a and b to point to the combined list and free the old
153 * lists as needed. The combined list usually contains the intersection of
154 * the lists of a and b.
155 *
156 * Both a and b must have owners (i.e. refcount > 0) for these functions.
157 *
158 * @return 1 if merging succeeded, 0 if a and b are incompatible
159 * and negative AVERROR code on failure.
160 * a and b are unmodified if 0 is returned.
163 {
165 }
166
167 /**
168 * See can_merge_pix_fmts().
171 {
174 }
175
176 /**
177 * See merge_pix_fmts().
180 {
182 }
186 {
188 if (
a ==
b)
return 1;
189
191 return 1;
192 }
193
194 /**
195 * See can_merge_pix_fmts().
198 {
200 }
201
202 /**
203 * See merge_pix_fmts().
206 {
208 }
209
210 /**
211 * See merge_pix_fmts().
215 {
217 unsigned a_all =
a->all_layouts +
a->all_counts;
218 unsigned b_all =
b->all_layouts +
b->all_counts;
219 int ret_max, ret_nb = 0,
i, j,
round;
220
222
223 if (
a ==
b)
return 1;
224
225 /* Put the most generic set in a, to avoid doing everything twice */
226 if (a_all < b_all) {
228 FFSWAP(
unsigned, a_all, b_all);
229 }
230 if (a_all) {
231 if (a_all == 1 && !b_all) {
232 /* keep only known layouts in b; works also for b_all = 1 */
233 for (
i = j = 0;
i <
b->nb_channel_layouts;
i++)
234 if (
KNOWN(&
b->channel_layouts[
i]) &&
i != j++) {
236 return 1;
238 }
239 /* Not optimal: the unknown layouts of b may become known after
240 another merge. */
241 if (!j)
242 return 0;
243 b->nb_channel_layouts = j;
244 }
246 return 1;
247 }
248
249 ret_max =
a->nb_channel_layouts +
b->nb_channel_layouts;
252
253 /* a[known] intersect b[known] */
254 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
255 if (!
KNOWN(&
a->channel_layouts[
i]))
256 continue;
257 for (j = 0; j <
b->nb_channel_layouts; j++) {
260 return 1;
264 break;
265 }
266 }
267 }
268 /* 1st round: a[known] intersect b[generic]
269 2nd round: a[generic] intersect b[known] */
271 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
274 continue;
276 for (j = 0; j <
b->nb_channel_layouts; j++)
279 return 1;
281 }
282 }
283 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
285 }
286 /* a[generic] intersect b[generic] */
287 for (
i = 0;
i <
a->nb_channel_layouts;
i++) {
288 if (
KNOWN(&
a->channel_layouts[
i]))
289 continue;
290 for (j = 0; j <
b->nb_channel_layouts; j++)
293 return 1;
295 }
296 }
297
298 if (!ret_nb) {
300 return 0;
301 }
302
303 if (
a->refcount >
b->refcount)
305
310 b->nb_channel_layouts = ret_nb;
311 return 1;
312 }
315 {
318 }
321 {
323 }
327 {
329
331 return 1;
332
334
335 return 1;
336 }
339 {
342 }
345 {
347 }
349 #define PRINT_NAME(type, type_name) \
350 static void print_##type_name(AVBPrint *bp, const void *fmtsp) \
351 { \
352 const AVFilterFormats *fmts = fmtsp; \
353 for (int i = 0; i < fmts->nb_formats; i++) { \
354 const char *name = av_##type_name(fmts->formats[i]); \
355 av_bprint_chars(bp, ' ', i ? 1 : 0); \
356 av_bprint_append_data(bp, name, name ? strlen(name) : 0); \
357 } \
358 }
359
367 {
369 for (
int i = 0;
i <
layouts->nb_channel_layouts;
i++) {
372 }
373 }
376 {
378 for (
int i = 0;
i <
rates->nb_formats;
i++)
380 }
382 #define CONVERSION_FILTER_SWSCALE \
383 .conversion_filter = "scale", \
384 .conversion_opts_offset = offsetof(AVFilterGraph, scale_sws_opts),
386 #define CONVERSION_FILTER_ARESAMPLE \
387 .conversion_filter = "aresample", \
388 .conversion_opts_offset = offsetof(AVFilterGraph, aresample_swr_opts),
391 {
392 .name = "Pixel formats",
396 .print_list = print_get_pix_fmt_name,
398 },
399 {
400 .name = "Color spaces",
404 .print_list = print_color_space_name,
406 },
407 {
408 .name = "Color ranges",
412 .print_list = print_color_range_name,
414 },
415 {
416 .name = "Alpha modes",
420 .print_list = print_alpha_mode_name,
421 .conversion_filter = "premultiply_dynamic",
422 },
423 };
426 {
427 .name = "Channel layouts",
433 },
434 {
435 .name = "Sample rates",
441 },
442 {
443 .name = "Sample formats",
447 .print_list = print_get_sample_fmt_name,
449 },
450 };
455 };
460 };
463 {
464 switch (
link->type) {
467 default:
return NULL;
468 }
469 }
472 {
474
475 for (
p = fmts; *
p != -1;
p++) {
477 return 1;
478 }
479 return 0;
480 }
482 #define MAKE_FORMAT_LIST(type, field, count_field) \
483 type *formats; \
484 int count = 0; \
485 if (fmts) \
486 for (count = 0; fmts[count] != -1; count++) \
487 ; \
488 formats = av_mallocz(sizeof(*formats)); \
489 if (!formats) \
490 return NULL; \
491 formats->count_field = count; \
492 if (count) { \
493 formats->field = av_malloc_array(count, sizeof(*formats->field)); \
494 if (!formats->field) { \
495 av_freep(&formats); \
496 return NULL; \
497 } \
498 }
501 {
503 while (count--)
504 formats->formats[count] = fmts[count];
505
507 }
510 {
512 int count = 0;
513 if (fmts)
515 ;
520 if (count) {
526 }
527 for (
int i = 0;
i < count;
i++) {
531 }
532 }
533
535
537 for (
int i = 0;
i < count;
i++)
541
543 }
545 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
546 do { \
547 type *fmts; \
548 \
549 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
550 return AVERROR(ENOMEM); \
551 } \
552 \
553 fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
554 sizeof(*(*f)->list)); \
555 if (!fmts) { \
556 unref_fn(f); \
557 return AVERROR(ENOMEM); \
558 } \
559 \
560 (*f)->list = fmts; \
561 ASSIGN_FMT(f, fmt, list, nb); \
562 } while (0)
563
564 #define ASSIGN_FMT(f, fmt, list, nb) \
565 do { \
566 (*f)->list[(*f)->nb++] = fmt; \
567 } while (0)
570 {
572 return 0;
573 }
574
576 #define ASSIGN_FMT(f, fmt, list, nb) \
577 do { \
578 int ret; \
579 memset((*f)->list + (*f)->nb, 0, sizeof(*(*f)->list)); \
580 ret = av_channel_layout_copy(&(*f)->list[(*f)->nb], fmt); \
581 if (ret < 0) \
582 return ret; \
583 (*f)->nb++; \
584 } while (0)
588 {
591 return 0;
592 }
595 {
596 int fmts[2] = { fmt, -1 };
598 }
601 {
603
611 fmt++;
612 }
613 }
614
616 }
619 {
620 unsigned nb_formats, fmt,
flags;
622
623 while (1) {
624 nb_formats = 0;
625 for (fmt = 0;; fmt++) {
628 break;
632 (
desc->log2_chroma_w ||
desc->log2_chroma_h))
634 if ((
flags & (want | rej)) != want)
635 continue;
637 formats->formats[nb_formats] = fmt;
638 nb_formats++;
639 }
643 }
647 formats->nb_formats = nb_formats;
648 if (nb_formats) {
653 }
654 }
655 }
656 }
659 {
661 int fmt;
662
667
669 }
672 {
675 }
678 {
682 ret->all_layouts = 1;
684 }
687 {
691 ret->all_layouts =
ret->all_counts = 1;
693 }
696 {
703 continue;
706 }
707
709 }
712 {
717 }
718
720 }
723 {
728 }
729
731 }
733 #define FORMATS_REF(f, ref, unref_fn) \
734 void *tmp; \
735 \
736 if (!f) \
737 return AVERROR(ENOMEM); \
738 \
739 tmp = av_realloc_array(f->refs, f->refcount + 1, sizeof(*f->refs)); \
740 if (!tmp) { \
741 unref_fn(&f); \
742 return AVERROR(ENOMEM); \
743 } \
744 f->refs = tmp; \
745 f->refs[f->refcount++] = ref; \
746 *ref = f; \
747 return 0
750 {
752 }
755 {
757 }
759 #define FIND_REF_INDEX(ref, idx) \
760 do { \
761 int i; \
762 for (i = 0; i < (*ref)->refcount; i ++) \
763 if((*ref)->refs[i] == ref) { \
764 idx = i; \
765 break; \
766 } \
767 } while (0)
769 #define FORMATS_UNREF(ref, list) \
770 do { \
771 int idx = -1; \
772 \
773 if (!*ref) \
774 return; \
775 \
776 FIND_REF_INDEX(ref, idx); \
777 \
778 if (idx >= 0) { \
779 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
780 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
781 --(*ref)->refcount; \
782 } \
783 if (!(*ref)->refcount) { \
784 FREE_LIST(ref, list); \
785 av_free((*ref)->list); \
786 av_free((*ref)->refs); \
787 av_free(*ref); \
788 } \
789 *ref = NULL; \
790 } while (0)
791
792 #define FREE_LIST(ref, list) do { } while(0)
794 {
796 }
797
799 #define FREE_LIST(ref, list) \
800 do { \
801 for (int i = 0; i < (*ref)->nb_channel_layouts; i++) \
802 av_channel_layout_uninit(&(*ref)->list[i]); \
803 } while(0)
806 {
808 }
810 #define FORMATS_CHANGEREF(oldref, newref) \
811 do { \
812 int idx = -1; \
813 \
814 FIND_REF_INDEX(oldref, idx); \
815 \
816 if (idx >= 0) { \
817 (*oldref)->refs[idx] = newref; \
818 *newref = *oldref; \
819 *oldref = NULL; \
820 } \
821 } while (0)
825 {
827 }
830 {
832 }
834 #define SET_COMMON_FORMATS(ctx, fmts, media_type, ref_fn, unref_fn) \
835 int i; \
836 \
837 if (!fmts) \
838 return AVERROR(ENOMEM); \
839 \
840 for (i = 0; i < ctx->nb_inputs; i++) { \
841 AVFilterLink *const link = ctx->inputs[i]; \
842 if (link && !link->outcfg.fmts && \
843 (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
844 int ret = ref_fn(fmts, &ctx->inputs[i]->outcfg.fmts); \
845 if (ret < 0) { \
846 return ret; \
847 } \
848 } \
849 } \
850 for (i = 0; i < ctx->nb_outputs; i++) { \
851 AVFilterLink *const link = ctx->outputs[i]; \
852 if (link && !link->incfg.fmts && \
853 (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
854 int ret = ref_fn(fmts, &ctx->outputs[i]->incfg.fmts); \
855 if (ret < 0) { \
856 return ret; \
857 } \
858 } \
859 } \
860 \
861 if (!fmts->refcount) \
862 unref_fn(&fmts); \
863 \
864 return 0;
868 {
871 }
875 {
877 }
880 {
882 }
886 {
889 }
893 {
895 }
898 {
900 }
904 {
907 }
911 {
913 }
916 {
918 }
922 {
925 }
929 {
931 }
934 {
936 }
940 {
943 }
947 {
949 }
952 {
954 }
955
956 /**
957 * A helper for query_formats() which sets all links to the same list of
958 * formats. If there are no links hooked to this filter, the list of formats is
959 * freed.
962 {
965 }
968 {
970 }
972 #define SET_COMMON_FORMATS2(ctx, cfg_in, cfg_out, fmts, media_type, \
973 ref_fn, unref_fn) \
974 if (!fmts) \
975 return AVERROR(ENOMEM); \
976 \
977 for (unsigned i = 0; i < ctx->nb_inputs; i++) { \
978 const AVFilterLink *const link = ctx->inputs[i]; \
979 if (!cfg_in[i]->fmts && \
980 (media_type == AVMEDIA_TYPE_UNKNOWN || \
981 link->type == media_type)) { \
982 int ret = ref_fn(fmts, &cfg_in[i]->fmts); \
983 if (ret < 0) { \
984 return ret; \
985 } \
986 } \
987 } \
988 for (unsigned i = 0; i < ctx->nb_outputs; i++) { \
989 const AVFilterLink *const link = ctx->outputs[i]; \
990 if (!cfg_out[i]->fmts && \
991 (media_type == AVMEDIA_TYPE_UNKNOWN || \
992 link->type == media_type)) { \
993 int ret = ref_fn(fmts, &cfg_out[i]->fmts); \
994 if (ret < 0) { \
995 return ret; \
996 } \
997 } \
998 } \
999 \
1000 if (!fmts->refcount) \
1001 unref_fn(&fmts); \
1002 \
1003 return 0;
1009 {
1012 }
1018 {
1020 }
1025 {
1027 }
1033 {
1036 }
1042 {
1044 }
1049 {
1051 }
1057 {
1060 }
1066 {
1068 }
1073 {
1075 }
1081 {
1084 }
1090 {
1092 }
1097 {
1099 }
1105 {
1108 }
1114 {
1116 }
1121 {
1123 }
1129 {
1132 }
1137 const int *fmts)
1138 {
1140 }
1141
1144 {
1149
1150 switch (
f->formats_state) {
1154 break;
1158 break;
1162 break;
1166 break;
1167 default:
1169 /* Intended fallthrough */
1175 ctx->nb_outputs ?
ctx->outputs[0]->type :
1177 break;
1178 }
1179
1193 }
1201 }
1202
1203 return 0;
1204 }
1207 {
1209
1210 if (!fmts)
1211 return 0;
1215 }
1221 }
1222 }
1223 }
1224 return 0;
1225 }
1228 {
1230 }
1233 {
1235 }
1238 {
1240 return 0;
1242 }
1245 {
1250 }
1251 }
1253 }
1256 {
1258 }
1261 {
1263 }
1266 {
1270 }
1273 {
1275
1276 if (!fmts)
1277 return 0;
1281 }
1285 }
1291 }
1292 }
1293 }
1294 return 0;
1295 }