]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_stereo3d.c
lavf/matroskaenc: return an error for unsupported types.
[ffmpeg] / libavfilter / vf_stereo3d.c
1 /*
2  * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
3  * Copyright (c) 2013 Paul B Mahol
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 General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with FFmpeg; 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/avassert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 enum StereoCode {
33     ANAGLYPH_RC_GRAY,   // anaglyph red/cyan gray
34     ANAGLYPH_RC_HALF,   // anaglyph red/cyan half colored
35     ANAGLYPH_RC_COLOR,  // anaglyph red/cyan colored
36     ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
37     ANAGLYPH_GM_GRAY,   // anaglyph green/magenta gray
38     ANAGLYPH_GM_HALF,   // anaglyph green/magenta half colored
39     ANAGLYPH_GM_COLOR,  // anaglyph green/magenta colored
40     ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
41     ANAGLYPH_YB_GRAY,   // anaglyph yellow/blue gray
42     ANAGLYPH_YB_HALF,   // anaglyph yellow/blue half colored
43     ANAGLYPH_YB_COLOR,  // anaglyph yellow/blue colored
44     ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
45     ANAGLYPH_RB_GRAY,   // anaglyph red/blue gray
46     ANAGLYPH_RG_GRAY,   // anaglyph red/green gray
47     MONO_L,             // mono output for debugging (left eye only)
48     MONO_R,             // mono output for debugging (right eye only)
49     INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
50     INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
51     SIDE_BY_SIDE_LR,    // side by side parallel (left eye left, right eye right)
52     SIDE_BY_SIDE_RL,    // side by side crosseye (right eye left, left eye right)
53     SIDE_BY_SIDE_2_LR,  // side by side parallel with half width resolution
54     SIDE_BY_SIDE_2_RL,  // side by side crosseye with half width resolution
55     ABOVE_BELOW_LR,     // above-below (left eye above, right eye below)
56     ABOVE_BELOW_RL,     // above-below (right eye above, left eye below)
57     ABOVE_BELOW_2_LR,   // above-below with half height resolution
58     ABOVE_BELOW_2_RL,   // above-below with half height resolution
59     ALTERNATING_LR,     // alternating frames (left eye first, right eye second)
60     ALTERNATING_RL,     // alternating frames (right eye first, left eye second)
61     STEREO_CODE_COUNT   // TODO: needs autodetection
62 };
63
64 typedef struct StereoComponent {
65     enum StereoCode format;
66     int width, height;
67     int off_left, off_right;
68     int off_lstep, off_rstep;
69     int row_left, row_right;
70 } StereoComponent;
71
72 static const int ana_coeff[][3][6] = {
73   [ANAGLYPH_RB_GRAY]   =
74     {{19595, 38470,  7471,     0,     0,     0},
75      {    0,     0,     0,     0,     0,     0},
76      {    0,     0,     0, 19595, 38470,  7471}},
77   [ANAGLYPH_RG_GRAY]   =
78     {{19595, 38470,  7471,     0,     0,     0},
79      {    0,     0,     0, 19595, 38470,  7471},
80      {    0,     0,     0,     0,     0,     0}},
81   [ANAGLYPH_RC_GRAY]   =
82     {{19595, 38470,  7471,     0,     0,     0},
83      {    0,     0,     0, 19595, 38470,  7471},
84      {    0,     0,     0, 19595, 38470,  7471}},
85   [ANAGLYPH_RC_HALF]   =
86     {{19595, 38470,  7471,     0,     0,     0},
87      {    0,     0,     0,     0, 65536,     0},
88      {    0,     0,     0,     0,     0, 65536}},
89   [ANAGLYPH_RC_COLOR]  =
90     {{65536,     0,     0,     0,     0,     0},
91      {    0,     0,     0,     0, 65536,     0},
92      {    0,     0,     0,     0,     0, 65536}},
93   [ANAGLYPH_RC_DUBOIS] =
94     {{29891, 32800, 11559, -2849, -5763,  -102},
95      {-2627, -2479, -1033, 24804, 48080, -1209},
96      { -997, -1350,  -358, -4729, -7403, 80373}},
97   [ANAGLYPH_GM_GRAY]   =
98     {{    0,     0,     0, 19595, 38470,  7471},
99      {19595, 38470,  7471,     0,     0,     0},
100      {    0,     0,     0, 19595, 38470,  7471}},
101   [ANAGLYPH_GM_HALF]   =
102     {{    0,     0,     0, 65536,     0,     0},
103      {19595, 38470,  7471,     0,     0,     0},
104      {    0,     0,     0,     0,     0, 65536}},
105   [ANAGLYPH_GM_COLOR]  =
106     {{    0,     0,     0, 65536,     0,     0},
107      {    0, 65536,     0,     0,     0,     0},
108      {    0,     0,     0,     0,     0, 65536}},
109   [ANAGLYPH_GM_DUBOIS]  =
110     {{-4063,-10354, -2556, 34669, 46203,  1573},
111      {18612, 43778,  9372, -1049,  -983, -4260},
112      { -983, -1769,  1376,   590,  4915, 61407}},
113   [ANAGLYPH_YB_GRAY]   =
114     {{    0,     0,     0, 19595, 38470,  7471},
115      {    0,     0,     0, 19595, 38470,  7471},
116      {19595, 38470,  7471,     0,     0,     0}},
117   [ANAGLYPH_YB_HALF]   =
118     {{    0,     0,     0, 65536,     0,     0},
119      {    0,     0,     0,     0, 65536,     0},
120      {19595, 38470,  7471,     0,     0,     0}},
121   [ANAGLYPH_YB_COLOR]  =
122     {{    0,     0,     0, 65536,     0,     0},
123      {    0,     0,     0,     0, 65536,     0},
124      {    0,     0, 65536,     0,     0,     0}},
125   [ANAGLYPH_YB_DUBOIS] =
126     {{65535,-12650,18451,   -987, -7590, -1049},
127      {-1604, 56032, 4196,    370,  3826, -1049},
128      {-2345,-10676, 1358,   5801, 11416, 56217}},
129 };
130
131 typedef struct Stereo3DContext {
132     const AVClass *class;
133     StereoComponent in, out;
134     int width, height;
135     int row_step;
136     int ana_matrix[3][6];
137     int nb_planes;
138     int linesize[4];
139     int pixstep[4];
140     AVFrame *prev;
141     double ts_unit;
142 } Stereo3DContext;
143
144 #define OFFSET(x) offsetof(Stereo3DContext, x)
145 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
146
147 static const AVOption stereo3d_options[] = {
148     { "in",    "set input format",  OFFSET(in.format),  AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, STEREO_CODE_COUNT-1, FLAGS, "in"},
149     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},  0, 0, FLAGS, "in" },
150     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},  0, 0, FLAGS, "in" },
151     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},    0, 0, FLAGS, "in" },
152     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},    0, 0, FLAGS, "in" },
153     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},    0, 0, FLAGS, "in" },
154     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},    0, 0, FLAGS, "in" },
155     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
156     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
157     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},   0, 0, FLAGS, "in" },
158     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},   0, 0, FLAGS, "in" },
159     { "out",   "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
160     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
161     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
162     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "out" },
163     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "out" },
164     { "agmc",  "anaglyph green magenta color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR},  0, 0, FLAGS, "out" },
165     { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
166     { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
167     { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
168     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},     0, 0, FLAGS, "out" },
169     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},     0, 0, FLAGS, "out" },
170     { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
171     { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
172     { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
173     { "arcg",  "anaglyph red cyan gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY},   0, 0, FLAGS, "out" },
174     { "arch",  "anaglyph red cyan half color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF},   0, 0, FLAGS, "out" },
175     { "argg",  "anaglyph red green gray",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY},   0, 0, FLAGS, "out" },
176     { "aybc",  "anaglyph yellow blue color",          0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR},  0, 0, FLAGS, "out" },
177     { "aybd",  "anaglyph yellow blue dubois",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
178     { "aybg",  "anaglyph yellow blue gray",           0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY},   0, 0, FLAGS, "out" },
179     { "aybh",  "anaglyph yellow blue half color",     0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF},   0, 0, FLAGS, "out" },
180     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
181     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
182     { "ml",    "mono left",                           0, AV_OPT_TYPE_CONST, {.i64=MONO_L},             0, 0, FLAGS, "out" },
183     { "mr",    "mono right",                          0, AV_OPT_TYPE_CONST, {.i64=MONO_R},             0, 0, FLAGS, "out" },
184     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "out" },
185     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
186     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
187     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
188     {NULL},
189 };
190
191 AVFILTER_DEFINE_CLASS(stereo3d);
192
193 static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
194 static const enum AVPixelFormat other_pix_fmts[] = {
195     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
196     AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
197     AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
198     AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
199     AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
200     AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
201     AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
202     AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
203     AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
204     AV_PIX_FMT_GBRP,
205     AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
206     AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
207     AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
208     AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
209     AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
210     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
211     AV_PIX_FMT_YUVJ444P,
212     AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
213     AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
214     AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
215     AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
216     AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
217     AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
218     AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
219     AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
220     AV_PIX_FMT_NONE
221 };
222
223 static int query_formats(AVFilterContext *ctx)
224 {
225     Stereo3DContext *s = ctx->priv;
226     const enum AVPixelFormat *pix_fmts;
227
228     switch (s->out.format) {
229     case ANAGLYPH_GM_COLOR:
230     case ANAGLYPH_GM_DUBOIS:
231     case ANAGLYPH_GM_GRAY:
232     case ANAGLYPH_GM_HALF:
233     case ANAGLYPH_RB_GRAY:
234     case ANAGLYPH_RC_COLOR:
235     case ANAGLYPH_RC_DUBOIS:
236     case ANAGLYPH_RC_GRAY:
237     case ANAGLYPH_RC_HALF:
238     case ANAGLYPH_RG_GRAY:
239     case ANAGLYPH_YB_COLOR:
240     case ANAGLYPH_YB_DUBOIS:
241     case ANAGLYPH_YB_GRAY:
242     case ANAGLYPH_YB_HALF:
243         pix_fmts = anaglyph_pix_fmts;
244         break;
245     default:
246         pix_fmts = other_pix_fmts;
247     }
248
249     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
250
251     return 0;
252 }
253
254 static int config_output(AVFilterLink *outlink)
255 {
256     AVFilterContext *ctx = outlink->src;
257     AVFilterLink *inlink = ctx->inputs[0];
258     Stereo3DContext *s = ctx->priv;
259     AVRational aspect = inlink->sample_aspect_ratio;
260     AVRational fps = inlink->frame_rate;
261     AVRational tb = inlink->time_base;
262     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
263     int ret;
264
265     switch (s->in.format) {
266     case SIDE_BY_SIDE_2_LR:
267     case SIDE_BY_SIDE_LR:
268     case SIDE_BY_SIDE_2_RL:
269     case SIDE_BY_SIDE_RL:
270         if (inlink->w & 1) {
271             av_log(ctx, AV_LOG_ERROR, "width must be even\n");
272             return AVERROR_INVALIDDATA;
273         }
274         break;
275     case ABOVE_BELOW_2_LR:
276     case ABOVE_BELOW_LR:
277     case ABOVE_BELOW_2_RL:
278     case ABOVE_BELOW_RL:
279         if (s->out.format == INTERLEAVE_ROWS_LR ||
280             s->out.format == INTERLEAVE_ROWS_RL) {
281             if (inlink->h & 3) {
282                 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
283                 return AVERROR_INVALIDDATA;
284             }
285         }
286         if (inlink->h & 1) {
287             av_log(ctx, AV_LOG_ERROR, "height must be even\n");
288             return AVERROR_INVALIDDATA;
289         }
290         break;
291     }
292
293     s->in.width     =
294     s->width        = inlink->w;
295     s->in.height    =
296     s->height       = inlink->h;
297     s->row_step     = 1;
298     s->in.off_lstep =
299     s->in.off_rstep =
300     s->in.off_left  =
301     s->in.off_right =
302     s->in.row_left  =
303     s->in.row_right = 0;
304
305     switch (s->in.format) {
306     case SIDE_BY_SIDE_2_LR:
307         aspect.num     *= 2;
308     case SIDE_BY_SIDE_LR:
309         s->width        = inlink->w / 2;
310         s->in.off_right = s->width;
311         break;
312     case SIDE_BY_SIDE_2_RL:
313         aspect.num     *= 2;
314     case SIDE_BY_SIDE_RL:
315         s->width        = inlink->w / 2;
316         s->in.off_left  = s->width;
317         break;
318     case ABOVE_BELOW_2_LR:
319         aspect.den     *= 2;
320     case ABOVE_BELOW_LR:
321         s->in.row_right =
322         s->height       = inlink->h / 2;
323         break;
324     case ABOVE_BELOW_2_RL:
325         aspect.den     *= 2;
326     case ABOVE_BELOW_RL:
327         s->in.row_left  =
328         s->height       = inlink->h / 2;
329         break;
330     case ALTERNATING_RL:
331     case ALTERNATING_LR:
332         outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
333         fps.den        *= 2;
334         tb.num         *= 2;
335         break;
336     default:
337         av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
338         return AVERROR(EINVAL);
339     }
340
341     s->out.width     = s->width;
342     s->out.height    = s->height;
343     s->out.off_lstep =
344     s->out.off_rstep =
345     s->out.off_left  =
346     s->out.off_right =
347     s->out.row_left  =
348     s->out.row_right = 0;
349
350     switch (s->out.format) {
351     case ANAGLYPH_RB_GRAY:
352     case ANAGLYPH_RG_GRAY:
353     case ANAGLYPH_RC_GRAY:
354     case ANAGLYPH_RC_HALF:
355     case ANAGLYPH_RC_COLOR:
356     case ANAGLYPH_RC_DUBOIS:
357     case ANAGLYPH_GM_GRAY:
358     case ANAGLYPH_GM_HALF:
359     case ANAGLYPH_GM_COLOR:
360     case ANAGLYPH_GM_DUBOIS:
361     case ANAGLYPH_YB_GRAY:
362     case ANAGLYPH_YB_HALF:
363     case ANAGLYPH_YB_COLOR:
364     case ANAGLYPH_YB_DUBOIS:
365         memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
366         break;
367     case SIDE_BY_SIDE_2_LR:
368         aspect.den      *= 2;
369     case SIDE_BY_SIDE_LR:
370         s->out.width     = s->width * 2;
371         s->out.off_right = s->width;
372         break;
373     case SIDE_BY_SIDE_2_RL:
374         aspect.den      *= 2;
375     case SIDE_BY_SIDE_RL:
376         s->out.width     = s->width * 2;
377         s->out.off_left  = s->width;
378         break;
379     case ABOVE_BELOW_2_LR:
380         aspect.num      *= 2;
381     case ABOVE_BELOW_LR:
382         s->out.height    = s->height * 2;
383         s->out.row_right = s->height;
384         break;
385     case ABOVE_BELOW_2_RL:
386         aspect.num      *= 2;
387     case ABOVE_BELOW_RL:
388         s->out.height    = s->height * 2;
389         s->out.row_left  = s->height;
390         break;
391     case INTERLEAVE_ROWS_LR:
392         s->row_step      = 2;
393         s->height        = s->height / 2;
394         s->out.off_rstep =
395         s->in.off_rstep  = 1;
396         break;
397     case INTERLEAVE_ROWS_RL:
398         s->row_step      = 2;
399         s->height        = s->height / 2;
400         s->out.off_lstep =
401         s->in.off_lstep  = 1;
402         break;
403     case MONO_R:
404         s->in.off_left   = s->in.off_right;
405         s->in.row_left   = s->in.row_right;
406     case MONO_L:
407         break;
408     case ALTERNATING_RL:
409     case ALTERNATING_LR:
410         fps.num         *= 2;
411         tb.den          *= 2;
412         break;
413     default:
414         av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
415         return AVERROR(EINVAL);
416     }
417
418     outlink->w = s->out.width;
419     outlink->h = s->out.height;
420     outlink->frame_rate = fps;
421     outlink->time_base = tb;
422     outlink->sample_aspect_ratio = aspect;
423
424     if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
425         return ret;
426     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
427     av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
428     s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
429
430     return 0;
431 }
432
433 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
434 {
435     int sum;
436
437     sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
438     sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
439     sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
440
441     return av_clip_uint8(sum >> 16);
442 }
443
444 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
445 {
446     AVFilterContext *ctx  = inlink->dst;
447     Stereo3DContext *s = ctx->priv;
448     AVFilterLink *outlink = ctx->outputs[0];
449     AVFrame *out, *oleft, *oright, *ileft, *iright;
450     int out_off_left[4], out_off_right[4];
451     int in_off_left[4], in_off_right[4];
452     int i;
453
454     switch (s->in.format) {
455     case ALTERNATING_LR:
456     case ALTERNATING_RL:
457         if (!s->prev) {
458             s->prev = inpicref;
459             return 0;
460         }
461         ileft  = s->prev;
462         iright = inpicref;
463         if (s->in.format == ALTERNATING_RL)
464             FFSWAP(AVFrame *, ileft, iright);
465         break;
466     default:
467         ileft = iright = inpicref;
468     };
469
470     out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
471     if (!out) {
472         av_frame_free(&s->prev);
473         av_frame_free(&inpicref);
474         return AVERROR(ENOMEM);
475     }
476     av_frame_copy_props(out, inpicref);
477
478     if (s->out.format == ALTERNATING_LR ||
479         s->out.format == ALTERNATING_RL) {
480         oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
481         if (!oright) {
482             av_frame_free(&oleft);
483             av_frame_free(&s->prev);
484             av_frame_free(&inpicref);
485             return AVERROR(ENOMEM);
486         }
487         av_frame_copy_props(oright, inpicref);
488     }
489
490     for (i = 0; i < 4; i++) {
491         in_off_left[i]   = (s->in.row_left   + s->in.off_lstep)  * ileft->linesize[i]  + s->in.off_left   * s->pixstep[i];
492         in_off_right[i]  = (s->in.row_right  + s->in.off_rstep)  * iright->linesize[i] + s->in.off_right  * s->pixstep[i];
493         out_off_left[i]  = (s->out.row_left  + s->out.off_lstep) * oleft->linesize[i]  + s->out.off_left  * s->pixstep[i];
494         out_off_right[i] = (s->out.row_right + s->out.off_rstep) * oright->linesize[i] + s->out.off_right * s->pixstep[i];
495     }
496
497     switch (s->out.format) {
498     case ALTERNATING_LR:
499     case ALTERNATING_RL:
500     case SIDE_BY_SIDE_LR:
501     case SIDE_BY_SIDE_RL:
502     case SIDE_BY_SIDE_2_LR:
503     case SIDE_BY_SIDE_2_RL:
504     case ABOVE_BELOW_LR:
505     case ABOVE_BELOW_RL:
506     case ABOVE_BELOW_2_LR:
507     case ABOVE_BELOW_2_RL:
508     case INTERLEAVE_ROWS_LR:
509     case INTERLEAVE_ROWS_RL:
510         for (i = 0; i < s->nb_planes; i++) {
511             av_image_copy_plane(oleft->data[i] + out_off_left[i],
512                                 oleft->linesize[i] * s->row_step,
513                                 ileft->data[i] + in_off_left[i],
514                                 ileft->linesize[i] * s->row_step,
515                                 s->linesize[i], s->height);
516             av_image_copy_plane(oright->data[i] + out_off_right[i],
517                                 oright->linesize[i] * s->row_step,
518                                 iright->data[i] + in_off_right[i],
519                                 iright->linesize[i] * s->row_step,
520                                 s->linesize[i], s->height);
521         }
522         break;
523     case MONO_L:
524         iright = ileft;
525     case MONO_R:
526         for (i = 0; i < s->nb_planes; i++) {
527             av_image_copy_plane(out->data[i], out->linesize[i],
528                                 iright->data[i] + in_off_left[i],
529                                 iright->linesize[i],
530                                 s->linesize[i], s->height);
531         }
532         break;
533     case ANAGLYPH_RB_GRAY:
534     case ANAGLYPH_RG_GRAY:
535     case ANAGLYPH_RC_GRAY:
536     case ANAGLYPH_RC_HALF:
537     case ANAGLYPH_RC_COLOR:
538     case ANAGLYPH_RC_DUBOIS:
539     case ANAGLYPH_GM_GRAY:
540     case ANAGLYPH_GM_HALF:
541     case ANAGLYPH_GM_COLOR:
542     case ANAGLYPH_GM_DUBOIS:
543     case ANAGLYPH_YB_GRAY:
544     case ANAGLYPH_YB_HALF:
545     case ANAGLYPH_YB_COLOR:
546     case ANAGLYPH_YB_DUBOIS: {
547         int i, x, y, il, ir, o;
548         uint8_t *lsrc = ileft->data[0];
549         uint8_t *rsrc = iright->data[0];
550         uint8_t *dst = out->data[0];
551         int out_width = s->out.width;
552         int *ana_matrix[3];
553
554         for (i = 0; i < 3; i++)
555             ana_matrix[i] = s->ana_matrix[i];
556
557         for (y = 0; y < s->out.height; y++) {
558             o   = out->linesize[0] * y;
559             il  = in_off_left[0]  + y * ileft->linesize[0];
560             ir  = in_off_right[0] + y * iright->linesize[0];
561             for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
562                 dst[o    ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
563                 dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
564                 dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
565             }
566         }
567         break;
568     }
569     default:
570         av_assert0(0);
571     }
572
573     av_frame_free(&inpicref);
574     av_frame_free(&s->prev);
575     if (oright != oleft) {
576         if (s->out.format == ALTERNATING_LR)
577             FFSWAP(AVFrame *, oleft, oright);
578         oright->pts = outlink->frame_count * s->ts_unit;
579         ff_filter_frame(outlink, oright);
580         out = oleft;
581         oleft->pts = outlink->frame_count * s->ts_unit;
582     } else if (s->in.format == ALTERNATING_LR ||
583                s->in.format == ALTERNATING_RL) {
584         out->pts = outlink->frame_count * s->ts_unit;
585     }
586     return ff_filter_frame(outlink, out);
587 }
588
589 static av_cold void uninit(AVFilterContext *ctx)
590 {
591     Stereo3DContext *s = ctx->priv;
592
593     av_frame_free(&s->prev);
594 }
595
596 static const AVFilterPad stereo3d_inputs[] = {
597     {
598         .name             = "default",
599         .type             = AVMEDIA_TYPE_VIDEO,
600         .filter_frame     = filter_frame,
601     },
602     { NULL }
603 };
604
605 static const AVFilterPad stereo3d_outputs[] = {
606     {
607         .name         = "default",
608         .type         = AVMEDIA_TYPE_VIDEO,
609         .config_props = config_output,
610     },
611     { NULL }
612 };
613
614 AVFilter avfilter_vf_stereo3d = {
615     .name          = "stereo3d",
616     .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
617     .priv_size     = sizeof(Stereo3DContext),
618     .uninit        = uninit,
619     .query_formats = query_formats,
620     .inputs        = stereo3d_inputs,
621     .outputs       = stereo3d_outputs,
622     .priv_class    = &stereo3d_class,
623 };