]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_stereo3d.c
Merge commit '4b054a3400f728c54470ee6a1eefe1d82420f6a2'
[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 pheight[4];
140     int hsub, vsub;
141     int pixstep[4];
142     AVFrame *prev;
143     double ts_unit;
144 } Stereo3DContext;
145
146 #define OFFSET(x) offsetof(Stereo3DContext, x)
147 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
148
149 static const AVOption stereo3d_options[] = {
150     { "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"},
151     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},  0, 0, FLAGS, "in" },
152     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},  0, 0, FLAGS, "in" },
153     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},    0, 0, FLAGS, "in" },
154     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},    0, 0, FLAGS, "in" },
155     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},    0, 0, FLAGS, "in" },
156     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},    0, 0, FLAGS, "in" },
157     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
158     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
159     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},   0, 0, FLAGS, "in" },
160     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},   0, 0, FLAGS, "in" },
161     { "out",   "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
162     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
163     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
164     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "out" },
165     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "out" },
166     { "agmc",  "anaglyph green magenta color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR},  0, 0, FLAGS, "out" },
167     { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
168     { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
169     { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
170     { "al",    "alternating frames left first",       0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR},     0, 0, FLAGS, "out" },
171     { "ar",    "alternating frames right first",      0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL},     0, 0, FLAGS, "out" },
172     { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
173     { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
174     { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
175     { "arcg",  "anaglyph red cyan gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY},   0, 0, FLAGS, "out" },
176     { "arch",  "anaglyph red cyan half color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF},   0, 0, FLAGS, "out" },
177     { "argg",  "anaglyph red green gray",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY},   0, 0, FLAGS, "out" },
178     { "aybc",  "anaglyph yellow blue color",          0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR},  0, 0, FLAGS, "out" },
179     { "aybd",  "anaglyph yellow blue dubois",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
180     { "aybg",  "anaglyph yellow blue gray",           0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY},   0, 0, FLAGS, "out" },
181     { "aybh",  "anaglyph yellow blue half color",     0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF},   0, 0, FLAGS, "out" },
182     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
183     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
184     { "ml",    "mono left",                           0, AV_OPT_TYPE_CONST, {.i64=MONO_L},             0, 0, FLAGS, "out" },
185     { "mr",    "mono right",                          0, AV_OPT_TYPE_CONST, {.i64=MONO_R},             0, 0, FLAGS, "out" },
186     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "out" },
187     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
188     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
189     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
190     {NULL},
191 };
192
193 AVFILTER_DEFINE_CLASS(stereo3d);
194
195 static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
196 static const enum AVPixelFormat other_pix_fmts[] = {
197     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
198     AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
199     AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
200     AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
201     AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
202     AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
203     AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
204     AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
205     AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
206     AV_PIX_FMT_GBRP,
207     AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
208     AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
209     AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
210     AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
211     AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
212     AV_PIX_FMT_YUV410P,
213     AV_PIX_FMT_YUV411P,
214     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
215     AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
216     AV_PIX_FMT_YUV440P,
217     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
218     AV_PIX_FMT_YUVJ411P,
219     AV_PIX_FMT_YUVJ420P,
220     AV_PIX_FMT_YUVJ422P,
221     AV_PIX_FMT_YUVJ440P,
222     AV_PIX_FMT_YUVJ444P,
223     AV_PIX_FMT_YUV420P9LE,  AV_PIX_FMT_YUVA420P9LE,
224     AV_PIX_FMT_YUV420P9BE,  AV_PIX_FMT_YUVA420P9BE,
225     AV_PIX_FMT_YUV422P9LE,  AV_PIX_FMT_YUVA422P9LE,
226     AV_PIX_FMT_YUV422P9BE,  AV_PIX_FMT_YUVA422P9BE,
227     AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
228     AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
229     AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
230     AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
231     AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
232     AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
233     AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
234     AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
235     AV_PIX_FMT_YUV420P12BE,  AV_PIX_FMT_YUV420P12LE,
236     AV_PIX_FMT_YUV422P12BE,  AV_PIX_FMT_YUV422P12LE,
237     AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
238     AV_PIX_FMT_YUV420P14BE,  AV_PIX_FMT_YUV420P14LE,
239     AV_PIX_FMT_YUV422P14BE,  AV_PIX_FMT_YUV422P14LE,
240     AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
241     AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
242     AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
243     AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
244     AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
245     AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
246     AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
247     AV_PIX_FMT_NONE
248 };
249
250 static int query_formats(AVFilterContext *ctx)
251 {
252     Stereo3DContext *s = ctx->priv;
253     const enum AVPixelFormat *pix_fmts;
254
255     switch (s->out.format) {
256     case ANAGLYPH_GM_COLOR:
257     case ANAGLYPH_GM_DUBOIS:
258     case ANAGLYPH_GM_GRAY:
259     case ANAGLYPH_GM_HALF:
260     case ANAGLYPH_RB_GRAY:
261     case ANAGLYPH_RC_COLOR:
262     case ANAGLYPH_RC_DUBOIS:
263     case ANAGLYPH_RC_GRAY:
264     case ANAGLYPH_RC_HALF:
265     case ANAGLYPH_RG_GRAY:
266     case ANAGLYPH_YB_COLOR:
267     case ANAGLYPH_YB_DUBOIS:
268     case ANAGLYPH_YB_GRAY:
269     case ANAGLYPH_YB_HALF:
270         pix_fmts = anaglyph_pix_fmts;
271         break;
272     default:
273         pix_fmts = other_pix_fmts;
274     }
275
276     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
277
278     return 0;
279 }
280
281 static int config_output(AVFilterLink *outlink)
282 {
283     AVFilterContext *ctx = outlink->src;
284     AVFilterLink *inlink = ctx->inputs[0];
285     Stereo3DContext *s = ctx->priv;
286     AVRational aspect = inlink->sample_aspect_ratio;
287     AVRational fps = inlink->frame_rate;
288     AVRational tb = inlink->time_base;
289     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
290     int ret;
291
292     switch (s->in.format) {
293     case SIDE_BY_SIDE_2_LR:
294     case SIDE_BY_SIDE_LR:
295     case SIDE_BY_SIDE_2_RL:
296     case SIDE_BY_SIDE_RL:
297         if (inlink->w & 1) {
298             av_log(ctx, AV_LOG_ERROR, "width must be even\n");
299             return AVERROR_INVALIDDATA;
300         }
301         break;
302     case ABOVE_BELOW_2_LR:
303     case ABOVE_BELOW_LR:
304     case ABOVE_BELOW_2_RL:
305     case ABOVE_BELOW_RL:
306         if (s->out.format == INTERLEAVE_ROWS_LR ||
307             s->out.format == INTERLEAVE_ROWS_RL) {
308             if (inlink->h & 3) {
309                 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
310                 return AVERROR_INVALIDDATA;
311             }
312         }
313         if (inlink->h & 1) {
314             av_log(ctx, AV_LOG_ERROR, "height must be even\n");
315             return AVERROR_INVALIDDATA;
316         }
317         break;
318     }
319
320     s->in.width     =
321     s->width        = inlink->w;
322     s->in.height    =
323     s->height       = inlink->h;
324     s->row_step     = 1;
325     s->in.off_lstep =
326     s->in.off_rstep =
327     s->in.off_left  =
328     s->in.off_right =
329     s->in.row_left  =
330     s->in.row_right = 0;
331
332     switch (s->in.format) {
333     case SIDE_BY_SIDE_2_LR:
334         aspect.num     *= 2;
335     case SIDE_BY_SIDE_LR:
336         s->width        = inlink->w / 2;
337         s->in.off_right = s->width;
338         break;
339     case SIDE_BY_SIDE_2_RL:
340         aspect.num     *= 2;
341     case SIDE_BY_SIDE_RL:
342         s->width        = inlink->w / 2;
343         s->in.off_left  = s->width;
344         break;
345     case ABOVE_BELOW_2_LR:
346         aspect.den     *= 2;
347     case ABOVE_BELOW_LR:
348         s->in.row_right =
349         s->height       = inlink->h / 2;
350         break;
351     case ABOVE_BELOW_2_RL:
352         aspect.den     *= 2;
353     case ABOVE_BELOW_RL:
354         s->in.row_left  =
355         s->height       = inlink->h / 2;
356         break;
357     case ALTERNATING_RL:
358     case ALTERNATING_LR:
359         outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
360         fps.den        *= 2;
361         tb.num         *= 2;
362         break;
363     default:
364         av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
365         return AVERROR(EINVAL);
366     }
367
368     s->out.width     = s->width;
369     s->out.height    = s->height;
370     s->out.off_lstep =
371     s->out.off_rstep =
372     s->out.off_left  =
373     s->out.off_right =
374     s->out.row_left  =
375     s->out.row_right = 0;
376
377     switch (s->out.format) {
378     case ANAGLYPH_RB_GRAY:
379     case ANAGLYPH_RG_GRAY:
380     case ANAGLYPH_RC_GRAY:
381     case ANAGLYPH_RC_HALF:
382     case ANAGLYPH_RC_COLOR:
383     case ANAGLYPH_RC_DUBOIS:
384     case ANAGLYPH_GM_GRAY:
385     case ANAGLYPH_GM_HALF:
386     case ANAGLYPH_GM_COLOR:
387     case ANAGLYPH_GM_DUBOIS:
388     case ANAGLYPH_YB_GRAY:
389     case ANAGLYPH_YB_HALF:
390     case ANAGLYPH_YB_COLOR:
391     case ANAGLYPH_YB_DUBOIS:
392         memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
393         break;
394     case SIDE_BY_SIDE_2_LR:
395         aspect.den      *= 2;
396     case SIDE_BY_SIDE_LR:
397         s->out.width     = s->width * 2;
398         s->out.off_right = s->width;
399         break;
400     case SIDE_BY_SIDE_2_RL:
401         aspect.den      *= 2;
402     case SIDE_BY_SIDE_RL:
403         s->out.width     = s->width * 2;
404         s->out.off_left  = s->width;
405         break;
406     case ABOVE_BELOW_2_LR:
407         aspect.num      *= 2;
408     case ABOVE_BELOW_LR:
409         s->out.height    = s->height * 2;
410         s->out.row_right = s->height;
411         break;
412     case ABOVE_BELOW_2_RL:
413         aspect.num      *= 2;
414     case ABOVE_BELOW_RL:
415         s->out.height    = s->height * 2;
416         s->out.row_left  = s->height;
417         break;
418     case INTERLEAVE_ROWS_LR:
419         s->row_step      = 2;
420         s->height        = s->height / 2;
421         s->out.off_rstep =
422         s->in.off_rstep  = 1;
423         break;
424     case INTERLEAVE_ROWS_RL:
425         s->row_step      = 2;
426         s->height        = s->height / 2;
427         s->out.off_lstep =
428         s->in.off_lstep  = 1;
429         break;
430     case MONO_R:
431         s->in.off_left   = s->in.off_right;
432         s->in.row_left   = s->in.row_right;
433     case MONO_L:
434         break;
435     case ALTERNATING_RL:
436     case ALTERNATING_LR:
437         fps.num         *= 2;
438         tb.den          *= 2;
439         break;
440     default:
441         av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
442         return AVERROR(EINVAL);
443     }
444
445     outlink->w = s->out.width;
446     outlink->h = s->out.height;
447     outlink->frame_rate = fps;
448     outlink->time_base = tb;
449     outlink->sample_aspect_ratio = aspect;
450
451     if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
452         return ret;
453     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
454     av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
455     s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
456     s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
457     s->pheight[0] = s->pheight[3] = s->height;
458     s->hsub = desc->log2_chroma_w;
459     s->vsub = desc->log2_chroma_h;
460
461     return 0;
462 }
463
464 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
465 {
466     int sum;
467
468     sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
469     sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
470     sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
471
472     return av_clip_uint8(sum >> 16);
473 }
474
475 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
476 {
477     AVFilterContext *ctx  = inlink->dst;
478     Stereo3DContext *s = ctx->priv;
479     AVFilterLink *outlink = ctx->outputs[0];
480     AVFrame *out, *oleft, *oright, *ileft, *iright;
481     int out_off_left[4], out_off_right[4];
482     int in_off_left[4], in_off_right[4];
483     int i;
484
485     switch (s->in.format) {
486     case ALTERNATING_LR:
487     case ALTERNATING_RL:
488         if (!s->prev) {
489             s->prev = inpicref;
490             return 0;
491         }
492         ileft  = s->prev;
493         iright = inpicref;
494         if (s->in.format == ALTERNATING_RL)
495             FFSWAP(AVFrame *, ileft, iright);
496         break;
497     default:
498         ileft = iright = inpicref;
499     };
500
501     out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
502     if (!out) {
503         av_frame_free(&s->prev);
504         av_frame_free(&inpicref);
505         return AVERROR(ENOMEM);
506     }
507     av_frame_copy_props(out, inpicref);
508
509     if (s->out.format == ALTERNATING_LR ||
510         s->out.format == ALTERNATING_RL) {
511         oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
512         if (!oright) {
513             av_frame_free(&oleft);
514             av_frame_free(&s->prev);
515             av_frame_free(&inpicref);
516             return AVERROR(ENOMEM);
517         }
518         av_frame_copy_props(oright, inpicref);
519     }
520
521     for (i = 0; i < 4; i++) {
522         int hsub = i == 1 || i == 2 ? s->hsub : 0;
523         int vsub = i == 1 || i == 2 ? s->vsub : 0;
524         in_off_left[i]   = (FF_CEIL_RSHIFT(s->in.row_left,   vsub) + s->in.off_lstep)  * ileft->linesize[i]  + FF_CEIL_RSHIFT(s->in.off_left   * s->pixstep[i], hsub);
525         in_off_right[i]  = (FF_CEIL_RSHIFT(s->in.row_right,  vsub) + s->in.off_rstep)  * iright->linesize[i] + FF_CEIL_RSHIFT(s->in.off_right  * s->pixstep[i], hsub);
526         out_off_left[i]  = (FF_CEIL_RSHIFT(s->out.row_left,  vsub) + s->out.off_lstep) * oleft->linesize[i]  + FF_CEIL_RSHIFT(s->out.off_left  * s->pixstep[i], hsub);
527         out_off_right[i] = (FF_CEIL_RSHIFT(s->out.row_right, vsub) + s->out.off_rstep) * oright->linesize[i] + FF_CEIL_RSHIFT(s->out.off_right * s->pixstep[i], hsub);
528     }
529
530     switch (s->out.format) {
531     case ALTERNATING_LR:
532     case ALTERNATING_RL:
533     case SIDE_BY_SIDE_LR:
534     case SIDE_BY_SIDE_RL:
535     case SIDE_BY_SIDE_2_LR:
536     case SIDE_BY_SIDE_2_RL:
537     case ABOVE_BELOW_LR:
538     case ABOVE_BELOW_RL:
539     case ABOVE_BELOW_2_LR:
540     case ABOVE_BELOW_2_RL:
541     case INTERLEAVE_ROWS_LR:
542     case INTERLEAVE_ROWS_RL:
543         for (i = 0; i < s->nb_planes; i++) {
544             av_image_copy_plane(oleft->data[i] + out_off_left[i],
545                                 oleft->linesize[i] * s->row_step,
546                                 ileft->data[i] + in_off_left[i],
547                                 ileft->linesize[i] * s->row_step,
548                                 s->linesize[i], s->pheight[i]);
549             av_image_copy_plane(oright->data[i] + out_off_right[i],
550                                 oright->linesize[i] * s->row_step,
551                                 iright->data[i] + in_off_right[i],
552                                 iright->linesize[i] * s->row_step,
553                                 s->linesize[i], s->pheight[i]);
554         }
555         break;
556     case MONO_L:
557         iright = ileft;
558     case MONO_R:
559         for (i = 0; i < s->nb_planes; i++) {
560             av_image_copy_plane(out->data[i], out->linesize[i],
561                                 iright->data[i] + in_off_left[i],
562                                 iright->linesize[i],
563                                 s->linesize[i], s->pheight[i]);
564         }
565         break;
566     case ANAGLYPH_RB_GRAY:
567     case ANAGLYPH_RG_GRAY:
568     case ANAGLYPH_RC_GRAY:
569     case ANAGLYPH_RC_HALF:
570     case ANAGLYPH_RC_COLOR:
571     case ANAGLYPH_RC_DUBOIS:
572     case ANAGLYPH_GM_GRAY:
573     case ANAGLYPH_GM_HALF:
574     case ANAGLYPH_GM_COLOR:
575     case ANAGLYPH_GM_DUBOIS:
576     case ANAGLYPH_YB_GRAY:
577     case ANAGLYPH_YB_HALF:
578     case ANAGLYPH_YB_COLOR:
579     case ANAGLYPH_YB_DUBOIS: {
580         int i, x, y, il, ir, o;
581         uint8_t *lsrc = ileft->data[0];
582         uint8_t *rsrc = iright->data[0];
583         uint8_t *dst = out->data[0];
584         int out_width = s->out.width;
585         int *ana_matrix[3];
586
587         for (i = 0; i < 3; i++)
588             ana_matrix[i] = s->ana_matrix[i];
589
590         for (y = 0; y < s->out.height; y++) {
591             o   = out->linesize[0] * y;
592             il  = in_off_left[0]  + y * ileft->linesize[0];
593             ir  = in_off_right[0] + y * iright->linesize[0];
594             for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
595                 dst[o    ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
596                 dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
597                 dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
598             }
599         }
600         break;
601     }
602     default:
603         av_assert0(0);
604     }
605
606     av_frame_free(&inpicref);
607     av_frame_free(&s->prev);
608     if (oright != oleft) {
609         if (s->out.format == ALTERNATING_LR)
610             FFSWAP(AVFrame *, oleft, oright);
611         oright->pts = outlink->frame_count * s->ts_unit;
612         ff_filter_frame(outlink, oright);
613         out = oleft;
614         oleft->pts = outlink->frame_count * s->ts_unit;
615     } else if (s->in.format == ALTERNATING_LR ||
616                s->in.format == ALTERNATING_RL) {
617         out->pts = outlink->frame_count * s->ts_unit;
618     }
619     return ff_filter_frame(outlink, out);
620 }
621
622 static av_cold void uninit(AVFilterContext *ctx)
623 {
624     Stereo3DContext *s = ctx->priv;
625
626     av_frame_free(&s->prev);
627 }
628
629 static const AVFilterPad stereo3d_inputs[] = {
630     {
631         .name             = "default",
632         .type             = AVMEDIA_TYPE_VIDEO,
633         .filter_frame     = filter_frame,
634     },
635     { NULL }
636 };
637
638 static const AVFilterPad stereo3d_outputs[] = {
639     {
640         .name         = "default",
641         .type         = AVMEDIA_TYPE_VIDEO,
642         .config_props = config_output,
643     },
644     { NULL }
645 };
646
647 AVFilter avfilter_vf_stereo3d = {
648     .name          = "stereo3d",
649     .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
650     .priv_size     = sizeof(Stereo3DContext),
651     .uninit        = uninit,
652     .query_formats = query_formats,
653     .inputs        = stereo3d_inputs,
654     .outputs       = stereo3d_outputs,
655     .priv_class    = &stereo3d_class,
656 };