Browse code

lavfi/overlay: add format option

In particular, fix misbehavior in case main and overlay input formats
mismatch (e.g. YUV420 and YUV444).

Stefano Sabatini authored on 2013/02/20 07:07:31
Showing 3 changed files
... ...
@@ -3878,9 +3878,27 @@ overlay input width and height
3878 3878
 same as @var{overlay_w} and @var{overlay_h}
3879 3879
 @end table
3880 3880
 
3881
+@item format
3882
+Set the format for the output video.
3883
+
3884
+It accepts the following values:
3885
+@table @samp
3886
+@item yuv420
3887
+force YUV420 output
3888
+
3889
+@item yuv444
3890
+force YUV444 output
3891
+
3881 3892
 @item rgb
3893
+force RGB output
3894
+@end table
3895
+
3896
+Default value is @samp{yuv420}.
3897
+
3898
+@item rgb @emph{(deprecated)}
3882 3899
 If set to 1, force the filter to accept inputs in the RGB
3883
-color space. Default value is 0.
3900
+color space. Default value is 0. This option is deprecated, use
3901
+@option{format} instead.
3884 3902
 
3885 3903
 @item shortest
3886 3904
 If set to 1, force the output to terminate when the shortest input
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32 32
 #define LIBAVFILTER_VERSION_MINOR  38
33
-#define LIBAVFILTER_VERSION_MICRO 105
33
+#define LIBAVFILTER_VERSION_MICRO 106
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
... ...
@@ -83,6 +83,7 @@ typedef struct {
83 83
     uint8_t overlay_is_packed_rgb;
84 84
     uint8_t overlay_rgba_map[4];
85 85
     uint8_t overlay_has_alpha;
86
+    enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
86 87
 
87 88
     AVFilterBufferRef *overpicref;
88 89
     struct FFBufQueue queue_main;
... ...
@@ -102,8 +103,14 @@ typedef struct {
102 102
 static const AVOption overlay_options[] = {
103 103
     { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
104 104
     { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
105
-    { "rgb", "force packed RGB in input and output", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
105
+    { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
106 106
     { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
107
+
108
+    { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
109
+    { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
110
+    { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
111
+    { "rgb",    "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB},    .flags = FLAGS, .unit = "format" },
112
+
107 113
     { NULL }
108 114
 };
109 115
 
... ...
@@ -113,11 +120,21 @@ static av_cold int init(AVFilterContext *ctx, const char *args)
113 113
 {
114 114
     OverlayContext *over = ctx->priv;
115 115
     static const char *shorthand[] = { "x", "y", NULL };
116
+    int ret;
116 117
 
117 118
     over->class = &overlay_class;
118 119
     av_opt_set_defaults(over);
119 120
 
120
-    return av_opt_set_from_string(over, args, shorthand, "=", ":");
121
+    ret = av_opt_set_from_string(over, args, shorthand, "=", ":");
122
+    if (ret < 0)
123
+        return ret;
124
+
125
+    if (over->allow_packed_rgb) {
126
+        av_log(ctx, AV_LOG_WARNING,
127
+               "The rgb option is deprecated and is overriding the format option, use format instead\n");
128
+        over->format = OVERLAY_FORMAT_RGB;
129
+    }
130
+    return 0;
121 131
 }
122 132
 
123 133
 static av_cold void uninit(AVFilterContext *ctx)
... ...
@@ -136,15 +153,20 @@ static int query_formats(AVFilterContext *ctx)
136 136
     OverlayContext *over = ctx->priv;
137 137
 
138 138
     /* overlay formats contains alpha, for avoiding conversion with alpha information loss */
139
-    static const enum AVPixelFormat main_pix_fmts_yuv[] = {
140
-        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
141
-        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
142
-        AV_PIX_FMT_NONE
139
+    static const enum AVPixelFormat main_pix_fmts_yuv420[] = {
140
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
143 141
     };
144
-    static const enum AVPixelFormat overlay_pix_fmts_yuv[] = {
145
-        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA444P,
146
-        AV_PIX_FMT_NONE
142
+    static const enum AVPixelFormat overlay_pix_fmts_yuv420[] = {
143
+        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE
144
+    };
145
+
146
+    static const enum AVPixelFormat main_pix_fmts_yuv444[] = {
147
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
147 148
     };
149
+    static const enum AVPixelFormat overlay_pix_fmts_yuv444[] = {
150
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_NONE
151
+    };
152
+
148 153
     static const enum AVPixelFormat main_pix_fmts_rgb[] = {
149 154
         AV_PIX_FMT_ARGB,  AV_PIX_FMT_RGBA,
150 155
         AV_PIX_FMT_ABGR,  AV_PIX_FMT_BGRA,
... ...
@@ -160,12 +182,19 @@ static int query_formats(AVFilterContext *ctx)
160 160
     AVFilterFormats *main_formats;
161 161
     AVFilterFormats *overlay_formats;
162 162
 
163
-    if (over->allow_packed_rgb) {
163
+    switch (over->format) {
164
+    case OVERLAY_FORMAT_YUV420:
165
+        main_formats    = ff_make_format_list(main_pix_fmts_yuv420);
166
+        overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv420);
167
+        break;
168
+    case OVERLAY_FORMAT_YUV444:
169
+        main_formats    = ff_make_format_list(main_pix_fmts_yuv444);
170
+        overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv444);
171
+        break;
172
+    case OVERLAY_FORMAT_RGB:
164 173
         main_formats    = ff_make_format_list(main_pix_fmts_rgb);
165 174
         overlay_formats = ff_make_format_list(overlay_pix_fmts_rgb);
166
-    } else {
167
-        main_formats    = ff_make_format_list(main_pix_fmts_yuv);
168
-        overlay_formats = ff_make_format_list(overlay_pix_fmts_yuv);
175
+        break;
169 176
     }
170 177
 
171 178
     ff_formats_ref(main_formats,    &ctx->inputs [MAIN   ]->out_formats);