2 * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
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"
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
64 typedef struct StereoComponent {
65 enum StereoCode format;
67 int off_left, off_right;
68 int off_lstep, off_rstep;
69 int row_left, row_right;
72 static const int ana_coeff[][3][6] = {
74 {{19595, 38470, 7471, 0, 0, 0},
76 { 0, 0, 0, 19595, 38470, 7471}},
78 {{19595, 38470, 7471, 0, 0, 0},
79 { 0, 0, 0, 19595, 38470, 7471},
82 {{19595, 38470, 7471, 0, 0, 0},
83 { 0, 0, 0, 19595, 38470, 7471},
84 { 0, 0, 0, 19595, 38470, 7471}},
86 {{19595, 38470, 7471, 0, 0, 0},
87 { 0, 0, 0, 0, 65536, 0},
88 { 0, 0, 0, 0, 0, 65536}},
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}},
98 {{ 0, 0, 0, 19595, 38470, 7471},
99 {19595, 38470, 7471, 0, 0, 0},
100 { 0, 0, 0, 19595, 38470, 7471}},
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}},
114 {{ 0, 0, 0, 19595, 38470, 7471},
115 { 0, 0, 0, 19595, 38470, 7471},
116 {19595, 38470, 7471, 0, 0, 0}},
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}},
131 typedef struct Stereo3DContext {
132 const AVClass *class;
133 StereoComponent in, out;
136 int ana_matrix[3][6];
146 #define OFFSET(x) offsetof(Stereo3DContext, x)
147 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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" },
193 AVFILTER_DEFINE_CLASS(stereo3d);
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,
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,
214 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
215 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
217 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
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,
250 static int query_formats(AVFilterContext *ctx)
252 Stereo3DContext *s = ctx->priv;
253 const enum AVPixelFormat *pix_fmts;
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;
273 pix_fmts = other_pix_fmts;
276 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
281 static int config_output(AVFilterLink *outlink)
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);
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:
298 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
299 return AVERROR_INVALIDDATA;
302 case ABOVE_BELOW_2_LR:
304 case ABOVE_BELOW_2_RL:
306 if (s->out.format == INTERLEAVE_ROWS_LR ||
307 s->out.format == INTERLEAVE_ROWS_RL) {
309 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
310 return AVERROR_INVALIDDATA;
314 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
315 return AVERROR_INVALIDDATA;
321 s->width = inlink->w;
323 s->height = inlink->h;
332 switch (s->in.format) {
333 case SIDE_BY_SIDE_2_LR:
335 case SIDE_BY_SIDE_LR:
336 s->width = inlink->w / 2;
337 s->in.off_right = s->width;
339 case SIDE_BY_SIDE_2_RL:
341 case SIDE_BY_SIDE_RL:
342 s->width = inlink->w / 2;
343 s->in.off_left = s->width;
345 case ABOVE_BELOW_2_LR:
349 s->height = inlink->h / 2;
351 case ABOVE_BELOW_2_RL:
355 s->height = inlink->h / 2;
359 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
364 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
365 return AVERROR(EINVAL);
368 s->out.width = s->width;
369 s->out.height = s->height;
375 s->out.row_right = 0;
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));
394 case SIDE_BY_SIDE_2_LR:
396 case SIDE_BY_SIDE_LR:
397 s->out.width = s->width * 2;
398 s->out.off_right = s->width;
400 case SIDE_BY_SIDE_2_RL:
402 case SIDE_BY_SIDE_RL:
403 s->out.width = s->width * 2;
404 s->out.off_left = s->width;
406 case ABOVE_BELOW_2_LR:
409 s->out.height = s->height * 2;
410 s->out.row_right = s->height;
412 case ABOVE_BELOW_2_RL:
415 s->out.height = s->height * 2;
416 s->out.row_left = s->height;
418 case INTERLEAVE_ROWS_LR:
420 s->height = s->height / 2;
424 case INTERLEAVE_ROWS_RL:
426 s->height = s->height / 2;
431 s->in.off_left = s->in.off_right;
432 s->in.row_left = s->in.row_right;
441 av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
442 return AVERROR(EINVAL);
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;
451 if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
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;
464 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
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
472 return av_clip_uint8(sum >> 16);
475 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
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];
485 switch (s->in.format) {
494 if (s->in.format == ALTERNATING_RL)
495 FFSWAP(AVFrame *, ileft, iright);
498 ileft = iright = inpicref;
501 out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
503 av_frame_free(&s->prev);
504 av_frame_free(&inpicref);
505 return AVERROR(ENOMEM);
507 av_frame_copy_props(out, inpicref);
509 if (s->out.format == ALTERNATING_LR ||
510 s->out.format == ALTERNATING_RL) {
511 oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
513 av_frame_free(&oleft);
514 av_frame_free(&s->prev);
515 av_frame_free(&inpicref);
516 return AVERROR(ENOMEM);
518 av_frame_copy_props(oright, inpicref);
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);
530 switch (s->out.format) {
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:
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]);
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],
563 s->linesize[i], s->pheight[i]);
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;
587 for (i = 0; i < 3; i++)
588 ana_matrix[i] = s->ana_matrix[i];
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);
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);
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;
619 return ff_filter_frame(outlink, out);
622 static av_cold void uninit(AVFilterContext *ctx)
624 Stereo3DContext *s = ctx->priv;
626 av_frame_free(&s->prev);
629 static const AVFilterPad stereo3d_inputs[] = {
632 .type = AVMEDIA_TYPE_VIDEO,
633 .filter_frame = filter_frame,
638 static const AVFilterPad stereo3d_outputs[] = {
641 .type = AVMEDIA_TYPE_VIDEO,
642 .config_props = config_output,
647 AVFilter avfilter_vf_stereo3d = {
649 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
650 .priv_size = sizeof(Stereo3DContext),
652 .query_formats = query_formats,
653 .inputs = stereo3d_inputs,
654 .outputs = stereo3d_outputs,
655 .priv_class = &stereo3d_class,