]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_stereo3d.c
Merge commit 'feec9349d35b3a46d0c6a05e3b23626050b76a77'
[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     STEREO_CODE_COUNT   // TODO: needs autodetection
60 };
61
62 typedef struct StereoComponent {
63     enum StereoCode format;
64     int width, height;
65     int off_left, off_right;
66     int off_lstep, off_rstep;
67     int row_left, row_right;
68 } StereoComponent;
69
70 static const int ana_coeff[][3][6] = {
71   [ANAGLYPH_RB_GRAY]   =
72     {{19595, 38470,  7471,     0,     0,     0},
73      {    0,     0,     0,     0,     0,     0},
74      {    0,     0,     0, 19595, 38470,  7471}},
75   [ANAGLYPH_RG_GRAY]   =
76     {{19595, 38470,  7471,     0,     0,     0},
77      {    0,     0,     0, 19595, 38470,  7471},
78      {    0,     0,     0,     0,     0,     0}},
79   [ANAGLYPH_RC_GRAY]   =
80     {{19595, 38470,  7471,     0,     0,     0},
81      {    0,     0,     0, 19595, 38470,  7471},
82      {    0,     0,     0, 19595, 38470,  7471}},
83   [ANAGLYPH_RC_HALF]   =
84     {{19595, 38470,  7471,     0,     0,     0},
85      {    0,     0,     0,     0, 65536,     0},
86      {    0,     0,     0,     0,     0, 65536}},
87   [ANAGLYPH_RC_COLOR]  =
88     {{65536,     0,     0,     0,     0,     0},
89      {    0,     0,     0,     0, 65536,     0},
90      {    0,     0,     0,     0,     0, 65536}},
91   [ANAGLYPH_RC_DUBOIS] =
92     {{29891, 32800, 11559, -2849, -5763,  -102},
93      {-2627, -2479, -1033, 24804, 48080, -1209},
94      { -997, -1350,  -358, -4729, -7403, 80373}},
95   [ANAGLYPH_GM_GRAY]   =
96     {{    0,     0,     0, 19595, 38470,  7471},
97      {19595, 38470,  7471,     0,     0,     0},
98      {    0,     0,     0, 19595, 38470,  7471}},
99   [ANAGLYPH_GM_HALF]   =
100     {{    0,     0,     0, 65536,     0,     0},
101      {19595, 38470,  7471,     0,     0,     0},
102      {    0,     0,     0,     0,     0, 65536}},
103   [ANAGLYPH_GM_COLOR]  =
104     {{    0,     0,     0, 65536,     0,     0},
105      {    0, 65536,     0,     0,     0,     0},
106      {    0,     0,     0,     0,     0, 65536}},
107   [ANAGLYPH_GM_DUBOIS]  =
108     {{-4063,-10354, -2556, 34669, 46203,  1573},
109      {18612, 43778,  9372, -1049,  -983, -4260},
110      { -983, -1769,  1376,   590,  4915, 61407}},
111   [ANAGLYPH_YB_GRAY]   =
112     {{    0,     0,     0, 19595, 38470,  7471},
113      {    0,     0,     0, 19595, 38470,  7471},
114      {19595, 38470,  7471,     0,     0,     0}},
115   [ANAGLYPH_YB_HALF]   =
116     {{    0,     0,     0, 65536,     0,     0},
117      {    0,     0,     0,     0, 65536,     0},
118      {19595, 38470,  7471,     0,     0,     0}},
119   [ANAGLYPH_YB_COLOR]  =
120     {{    0,     0,     0, 65536,     0,     0},
121      {    0,     0,     0,     0, 65536,     0},
122      {    0,     0, 65536,     0,     0,     0}},
123   [ANAGLYPH_YB_DUBOIS] =
124     {{65535,-12650,18451,   -987, -7590, -1049},
125      {-1604, 56032, 4196,    370,  3826, -1049},
126      {-2345,-10676, 1358,   5801, 11416, 56217}},
127 };
128
129 typedef struct Stereo3DContext {
130     const AVClass *class;
131     StereoComponent in, out;
132     int width, height;
133     int row_step;
134     int ana_matrix[3][6];
135     int nb_planes;
136     int linesize[4];
137     int pixstep[4];
138 } Stereo3DContext;
139
140 #define OFFSET(x) offsetof(Stereo3DContext, x)
141 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
142
143 static const AVOption stereo3d_options[] = {
144     { "in",    "set input format",  OFFSET(in.format),  AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, ABOVE_BELOW_2_RL, FLAGS, "in"},
145     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},  0, 0, FLAGS, "in" },
146     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},  0, 0, FLAGS, "in" },
147     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},    0, 0, FLAGS, "in" },
148     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},    0, 0, FLAGS, "in" },
149     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
150     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
151     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},   0, 0, FLAGS, "in" },
152     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},   0, 0, FLAGS, "in" },
153     { "out",   "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
154     { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
155     { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
156     { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "out" },
157     { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "out" },
158     { "agmc",  "anaglyph green magenta color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR},  0, 0, FLAGS, "out" },
159     { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
160     { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
161     { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
162     { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
163     { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
164     { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
165     { "arcg",  "anaglyph red cyan gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY},   0, 0, FLAGS, "out" },
166     { "arch",  "anaglyph red cyan half color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF},   0, 0, FLAGS, "out" },
167     { "argg",  "anaglyph red green gray",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY},   0, 0, FLAGS, "out" },
168     { "aybc",  "anaglyph yellow blue color",          0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR},  0, 0, FLAGS, "out" },
169     { "aybd",  "anaglyph yellow blue dubois",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
170     { "aybg",  "anaglyph yellow blue gray",           0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY},   0, 0, FLAGS, "out" },
171     { "aybh",  "anaglyph yellow blue half color",     0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF},   0, 0, FLAGS, "out" },
172     { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
173     { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
174     { "ml",    "mono left",                           0, AV_OPT_TYPE_CONST, {.i64=MONO_L},             0, 0, FLAGS, "out" },
175     { "mr",    "mono right",                          0, AV_OPT_TYPE_CONST, {.i64=MONO_R},             0, 0, FLAGS, "out" },
176     { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "out" },
177     { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
178     { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
179     { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
180     {NULL},
181 };
182
183 AVFILTER_DEFINE_CLASS(stereo3d);
184
185 static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
186 static const enum AVPixelFormat other_pix_fmts[] = {
187     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
188     AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
189     AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
190     AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
191     AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
192     AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
193     AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
194     AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
195     AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
196     AV_PIX_FMT_GBRP,
197     AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
198     AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
199     AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
200     AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
201     AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
202     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
203     AV_PIX_FMT_YUVJ444P,
204     AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
205     AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
206     AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
207     AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
208     AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
209     AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
210     AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
211     AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
212     AV_PIX_FMT_NONE
213 };
214
215 static int query_formats(AVFilterContext *ctx)
216 {
217     Stereo3DContext *s = ctx->priv;
218     const enum AVPixelFormat *pix_fmts;
219
220     switch (s->out.format) {
221     case ANAGLYPH_GM_COLOR:
222     case ANAGLYPH_GM_DUBOIS:
223     case ANAGLYPH_GM_GRAY:
224     case ANAGLYPH_GM_HALF:
225     case ANAGLYPH_RB_GRAY:
226     case ANAGLYPH_RC_COLOR:
227     case ANAGLYPH_RC_DUBOIS:
228     case ANAGLYPH_RC_GRAY:
229     case ANAGLYPH_RC_HALF:
230     case ANAGLYPH_RG_GRAY:
231     case ANAGLYPH_YB_COLOR:
232     case ANAGLYPH_YB_DUBOIS:
233     case ANAGLYPH_YB_GRAY:
234     case ANAGLYPH_YB_HALF:
235         pix_fmts = anaglyph_pix_fmts;
236         break;
237     default:
238         pix_fmts = other_pix_fmts;
239     }
240
241     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
242
243     return 0;
244 }
245
246 static int config_output(AVFilterLink *outlink)
247 {
248     AVFilterContext *ctx = outlink->src;
249     AVFilterLink *inlink = ctx->inputs[0];
250     Stereo3DContext *s = ctx->priv;
251     AVRational aspect = inlink->sample_aspect_ratio;
252     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
253     int ret;
254
255     switch (s->in.format) {
256     case SIDE_BY_SIDE_2_LR:
257     case SIDE_BY_SIDE_LR:
258     case SIDE_BY_SIDE_2_RL:
259     case SIDE_BY_SIDE_RL:
260         if (inlink->w & 1) {
261             av_log(ctx, AV_LOG_ERROR, "width must be even\n");
262             return AVERROR_INVALIDDATA;
263         }
264         break;
265     case ABOVE_BELOW_2_LR:
266     case ABOVE_BELOW_LR:
267     case ABOVE_BELOW_2_RL:
268     case ABOVE_BELOW_RL:
269         if (s->out.format == INTERLEAVE_ROWS_LR ||
270             s->out.format == INTERLEAVE_ROWS_RL) {
271             if (inlink->h & 3) {
272                 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
273                 return AVERROR_INVALIDDATA;
274             }
275         }
276         if (inlink->h & 1) {
277             av_log(ctx, AV_LOG_ERROR, "height must be even\n");
278             return AVERROR_INVALIDDATA;
279         }
280         break;
281     }
282
283     s->in.width     =
284     s->width        = inlink->w;
285     s->in.height    =
286     s->height       = inlink->h;
287     s->row_step     = 1;
288     s->in.off_lstep =
289     s->in.off_rstep =
290     s->in.off_left  =
291     s->in.off_right =
292     s->in.row_left  =
293     s->in.row_right = 0;
294
295     switch (s->in.format) {
296     case SIDE_BY_SIDE_2_LR:
297         aspect.num     *= 2;
298     case SIDE_BY_SIDE_LR:
299         s->width        = inlink->w / 2;
300         s->in.off_right = s->width;
301         break;
302     case SIDE_BY_SIDE_2_RL:
303         aspect.num     *= 2;
304     case SIDE_BY_SIDE_RL:
305         s->width        = inlink->w / 2;
306         s->in.off_left  = s->width;
307         break;
308     case ABOVE_BELOW_2_LR:
309         aspect.den     *= 2;
310     case ABOVE_BELOW_LR:
311         s->in.row_right =
312         s->height       = inlink->h / 2;
313         break;
314     case ABOVE_BELOW_2_RL:
315         aspect.den     *= 2;
316     case ABOVE_BELOW_RL:
317         s->in.row_left  =
318         s->height       = inlink->h / 2;
319         break;
320     default:
321         av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
322         return AVERROR(EINVAL);
323     }
324
325     s->out.width     = s->width;
326     s->out.height    = s->height;
327     s->out.off_lstep =
328     s->out.off_rstep =
329     s->out.off_left  =
330     s->out.off_right =
331     s->out.row_left  =
332     s->out.row_right = 0;
333
334     switch (s->out.format) {
335     case ANAGLYPH_RB_GRAY:
336     case ANAGLYPH_RG_GRAY:
337     case ANAGLYPH_RC_GRAY:
338     case ANAGLYPH_RC_HALF:
339     case ANAGLYPH_RC_COLOR:
340     case ANAGLYPH_RC_DUBOIS:
341     case ANAGLYPH_GM_GRAY:
342     case ANAGLYPH_GM_HALF:
343     case ANAGLYPH_GM_COLOR:
344     case ANAGLYPH_GM_DUBOIS:
345     case ANAGLYPH_YB_GRAY:
346     case ANAGLYPH_YB_HALF:
347     case ANAGLYPH_YB_COLOR:
348     case ANAGLYPH_YB_DUBOIS:
349         memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
350         break;
351     case SIDE_BY_SIDE_2_LR:
352         aspect.num      /= 2;
353     case SIDE_BY_SIDE_LR:
354         s->out.width     = s->width * 2;
355         s->out.off_right = s->width;
356         break;
357     case SIDE_BY_SIDE_2_RL:
358         aspect.num      /= 2;
359     case SIDE_BY_SIDE_RL:
360         s->out.width     = s->width * 2;
361         s->out.off_left  = s->width;
362         break;
363     case ABOVE_BELOW_2_LR:
364         aspect.den      /= 2;
365     case ABOVE_BELOW_LR:
366         s->out.height    = s->height * 2;
367         s->out.row_right = s->height;
368         break;
369     case ABOVE_BELOW_2_RL:
370         aspect.den      /= 2;
371     case ABOVE_BELOW_RL:
372         s->out.height    = s->height * 2;
373         s->out.row_left  = s->height;
374         break;
375     case INTERLEAVE_ROWS_LR:
376         s->row_step      = 2;
377         s->height        = s->height / 2;
378         s->out.off_rstep =
379         s->in.off_rstep  = 1;
380         break;
381     case INTERLEAVE_ROWS_RL:
382         s->row_step      = 2;
383         s->height        = s->height / 2;
384         s->out.off_lstep =
385         s->in.off_lstep  = 1;
386         break;
387     case MONO_R:
388         s->in.off_left   = s->in.off_right;
389         s->in.row_left   = s->in.row_right;
390     case MONO_L:
391         break;
392     default:
393         av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
394         return AVERROR(EINVAL);
395     }
396
397     outlink->w = s->out.width;
398     outlink->h = s->out.height;
399     outlink->sample_aspect_ratio = aspect;
400
401     if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
402         return ret;
403     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
404     av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
405
406     return 0;
407 }
408
409 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
410 {
411     int sum;
412
413     sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
414     sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
415     sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
416
417     return av_clip_uint8(sum >> 16);
418 }
419
420 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
421 {
422     AVFilterContext *ctx  = inlink->dst;
423     Stereo3DContext *s = ctx->priv;
424     AVFilterLink *outlink = ctx->outputs[0];
425     AVFrame *out;
426     int out_off_left[4], out_off_right[4];
427     int in_off_left[4], in_off_right[4];
428     int i;
429
430     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
431     if (!out) {
432         av_frame_free(&inpicref);
433         return AVERROR(ENOMEM);
434     }
435     av_frame_copy_props(out, inpicref);
436
437     for (i = 0; i < 4; i++) {
438         in_off_left[i]   = (s->in.row_left   + s->in.off_lstep)  * inpicref->linesize[i] + s->in.off_left   * s->pixstep[i];
439         in_off_right[i]  = (s->in.row_right  + s->in.off_rstep)  * inpicref->linesize[i] + s->in.off_right  * s->pixstep[i];
440         out_off_left[i]  = (s->out.row_left  + s->out.off_lstep) * out->linesize[i]      + s->out.off_left  * s->pixstep[i];
441         out_off_right[i] = (s->out.row_right + s->out.off_rstep) * out->linesize[i]      + s->out.off_right * s->pixstep[i];
442     }
443
444     switch (s->out.format) {
445     case SIDE_BY_SIDE_LR:
446     case SIDE_BY_SIDE_RL:
447     case SIDE_BY_SIDE_2_LR:
448     case SIDE_BY_SIDE_2_RL:
449     case ABOVE_BELOW_LR:
450     case ABOVE_BELOW_RL:
451     case ABOVE_BELOW_2_LR:
452     case ABOVE_BELOW_2_RL:
453     case INTERLEAVE_ROWS_LR:
454     case INTERLEAVE_ROWS_RL:
455         for (i = 0; i < s->nb_planes; i++) {
456             av_image_copy_plane(out->data[i] + out_off_left[i],
457                                 out->linesize[i] * s->row_step,
458                                 inpicref->data[i] + in_off_left[i],
459                                 inpicref->linesize[i] * s->row_step,
460                                 s->linesize[i], s->height);
461             av_image_copy_plane(out->data[i] + out_off_right[i],
462                                 out->linesize[i] * s->row_step,
463                                 inpicref->data[i] + in_off_right[i],
464                                 inpicref->linesize[i] * s->row_step,
465                                 s->linesize[i], s->height);
466         }
467         break;
468     case MONO_L:
469     case MONO_R:
470         for (i = 0; i < s->nb_planes; i++) {
471             av_image_copy_plane(out->data[i], out->linesize[i],
472                                 inpicref->data[i] + in_off_left[i],
473                                 inpicref->linesize[i],
474                                 s->linesize[i], s->height);
475         }
476         break;
477     case ANAGLYPH_RB_GRAY:
478     case ANAGLYPH_RG_GRAY:
479     case ANAGLYPH_RC_GRAY:
480     case ANAGLYPH_RC_HALF:
481     case ANAGLYPH_RC_COLOR:
482     case ANAGLYPH_RC_DUBOIS:
483     case ANAGLYPH_GM_GRAY:
484     case ANAGLYPH_GM_HALF:
485     case ANAGLYPH_GM_COLOR:
486     case ANAGLYPH_GM_DUBOIS:
487     case ANAGLYPH_YB_GRAY:
488     case ANAGLYPH_YB_HALF:
489     case ANAGLYPH_YB_COLOR:
490     case ANAGLYPH_YB_DUBOIS: {
491         int i, x, y, il, ir, o;
492         uint8_t *src = inpicref->data[0];
493         uint8_t *dst = out->data[0];
494         int out_width = s->out.width;
495         int *ana_matrix[3];
496
497         for (i = 0; i < 3; i++)
498             ana_matrix[i] = s->ana_matrix[i];
499
500         for (y = 0; y < s->out.height; y++) {
501             o   = out->linesize[0] * y;
502             il  = in_off_left[0]  + y * inpicref->linesize[0];
503             ir  = in_off_right[0] + y * inpicref->linesize[0];
504             for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
505                 dst[o    ] = ana_convert(ana_matrix[0], src + il, src + ir);
506                 dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
507                 dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
508             }
509         }
510         break;
511     }
512     default:
513         av_assert0(0);
514     }
515
516     av_frame_free(&inpicref);
517     return ff_filter_frame(outlink, out);
518 }
519
520 static const AVFilterPad stereo3d_inputs[] = {
521     {
522         .name             = "default",
523         .type             = AVMEDIA_TYPE_VIDEO,
524         .filter_frame     = filter_frame,
525     },
526     { NULL }
527 };
528
529 static const AVFilterPad stereo3d_outputs[] = {
530     {
531         .name         = "default",
532         .type         = AVMEDIA_TYPE_VIDEO,
533         .config_props = config_output,
534     },
535     { NULL }
536 };
537
538 AVFilter avfilter_vf_stereo3d = {
539     .name          = "stereo3d",
540     .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
541     .priv_size     = sizeof(Stereo3DContext),
542     .query_formats = query_formats,
543     .inputs        = stereo3d_inputs,
544     .outputs       = stereo3d_outputs,
545     .priv_class    = &stereo3d_class,
546 };