2 * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
3 * Copyright (c) 2013-2015 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/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/pixdesc.h"
29 #include "drawutils.h"
35 ANAGLYPH_RC_GRAY, // anaglyph red/cyan gray
36 ANAGLYPH_RC_HALF, // anaglyph red/cyan half colored
37 ANAGLYPH_RC_COLOR, // anaglyph red/cyan colored
38 ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
39 ANAGLYPH_GM_GRAY, // anaglyph green/magenta gray
40 ANAGLYPH_GM_HALF, // anaglyph green/magenta half colored
41 ANAGLYPH_GM_COLOR, // anaglyph green/magenta colored
42 ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
43 ANAGLYPH_YB_GRAY, // anaglyph yellow/blue gray
44 ANAGLYPH_YB_HALF, // anaglyph yellow/blue half colored
45 ANAGLYPH_YB_COLOR, // anaglyph yellow/blue colored
46 ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
47 ANAGLYPH_RB_GRAY, // anaglyph red/blue gray
48 ANAGLYPH_RG_GRAY, // anaglyph red/green gray
49 MONO_L, // mono output for debugging (left eye only)
50 MONO_R, // mono output for debugging (right eye only)
51 INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
52 INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
53 SIDE_BY_SIDE_LR, // side by side parallel (left eye left, right eye right)
54 SIDE_BY_SIDE_RL, // side by side crosseye (right eye left, left eye right)
55 SIDE_BY_SIDE_2_LR, // side by side parallel with half width resolution
56 SIDE_BY_SIDE_2_RL, // side by side crosseye with half width resolution
57 ABOVE_BELOW_LR, // above-below (left eye above, right eye below)
58 ABOVE_BELOW_RL, // above-below (right eye above, left eye below)
59 ABOVE_BELOW_2_LR, // above-below with half height resolution
60 ABOVE_BELOW_2_RL, // above-below with half height resolution
61 ALTERNATING_LR, // alternating frames (left eye first, right eye second)
62 ALTERNATING_RL, // alternating frames (right eye first, left eye second)
63 CHECKERBOARD_LR, // checkerboard pattern (left eye first, right eye second)
64 CHECKERBOARD_RL, // checkerboard pattern (right eye first, left eye second)
65 HDMI, // HDMI frame pack (left eye first, right eye second)
66 STEREO_CODE_COUNT // TODO: needs autodetection
69 typedef struct StereoComponent {
70 int format; ///< StereoCode
72 int off_left, off_right;
73 int off_lstep, off_rstep;
74 int row_left, row_right;
78 static const int ana_coeff[][3][6] = {
80 {{19595, 38470, 7471, 0, 0, 0},
82 { 0, 0, 0, 19595, 38470, 7471}},
84 {{19595, 38470, 7471, 0, 0, 0},
85 { 0, 0, 0, 19595, 38470, 7471},
88 {{19595, 38470, 7471, 0, 0, 0},
89 { 0, 0, 0, 19595, 38470, 7471},
90 { 0, 0, 0, 19595, 38470, 7471}},
92 {{19595, 38470, 7471, 0, 0, 0},
93 { 0, 0, 0, 0, 65536, 0},
94 { 0, 0, 0, 0, 0, 65536}},
96 {{65536, 0, 0, 0, 0, 0},
97 { 0, 0, 0, 0, 65536, 0},
98 { 0, 0, 0, 0, 0, 65536}},
99 [ANAGLYPH_RC_DUBOIS] =
100 {{29891, 32800, 11559, -2849, -5763, -102},
101 {-2627, -2479, -1033, 24804, 48080, -1209},
102 { -997, -1350, -358, -4729, -7403, 80373}},
104 {{ 0, 0, 0, 19595, 38470, 7471},
105 {19595, 38470, 7471, 0, 0, 0},
106 { 0, 0, 0, 19595, 38470, 7471}},
108 {{ 0, 0, 0, 65536, 0, 0},
109 {19595, 38470, 7471, 0, 0, 0},
110 { 0, 0, 0, 0, 0, 65536}},
111 [ANAGLYPH_GM_COLOR] =
112 {{ 0, 0, 0, 65536, 0, 0},
113 { 0, 65536, 0, 0, 0, 0},
114 { 0, 0, 0, 0, 0, 65536}},
115 [ANAGLYPH_GM_DUBOIS] =
116 {{-4063,-10354, -2556, 34669, 46203, 1573},
117 {18612, 43778, 9372, -1049, -983, -4260},
118 { -983, -1769, 1376, 590, 4915, 61407}},
120 {{ 0, 0, 0, 19595, 38470, 7471},
121 { 0, 0, 0, 19595, 38470, 7471},
122 {19595, 38470, 7471, 0, 0, 0}},
124 {{ 0, 0, 0, 65536, 0, 0},
125 { 0, 0, 0, 0, 65536, 0},
126 {19595, 38470, 7471, 0, 0, 0}},
127 [ANAGLYPH_YB_COLOR] =
128 {{ 0, 0, 0, 65536, 0, 0},
129 { 0, 0, 0, 0, 65536, 0},
130 { 0, 0, 65536, 0, 0, 0}},
131 [ANAGLYPH_YB_DUBOIS] =
132 {{65535,-12650,18451, -987, -7590, -1049},
133 {-1604, 56032, 4196, 370, 3826, -1049},
134 {-2345,-10676, 1358, 5801, 11416, 56217}},
137 typedef struct Stereo3DContext {
138 const AVClass *class;
139 StereoComponent in, out;
141 const int *ana_matrix[3];
150 int in_off_left[4], in_off_right[4];
153 #define OFFSET(x) offsetof(Stereo3DContext, x)
154 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
156 static const AVOption stereo3d_options[] = {
157 { "in", "set input format", OFFSET(in.format), AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, INTERLEAVE_ROWS_LR, STEREO_CODE_COUNT-1, FLAGS, "in"},
158 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
159 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
160 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
161 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
162 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "in" },
163 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "in" },
164 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
165 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
166 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
167 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
168 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "in" },
169 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "in" },
170 { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
171 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
172 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
173 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
174 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
175 { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
176 { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
177 { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
178 { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
179 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "out" },
180 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "out" },
181 { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
182 { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
183 { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
184 { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
185 { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
186 { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
187 { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
188 { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
189 { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
190 { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
191 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
192 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
193 { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
194 { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
195 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
196 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
197 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
198 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
199 { "chl", "checkerboard left first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_LR}, 0, 0, FLAGS, "out" },
200 { "chr", "checkerboard right first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_RL}, 0, 0, FLAGS, "out" },
201 { "hdmi", "HDMI frame pack", 0, AV_OPT_TYPE_CONST, {.i64=HDMI}, 0, 0, FLAGS, "out" },
205 AVFILTER_DEFINE_CLASS(stereo3d);
207 static const enum AVPixelFormat anaglyph_pix_fmts[] = {
208 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
212 static const enum AVPixelFormat other_pix_fmts[] = {
213 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
214 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
215 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
216 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
217 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
218 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
219 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
220 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
221 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
223 AV_PIX_FMT_GBRP9BE, AV_PIX_FMT_GBRP9LE,
224 AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
225 AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
226 AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
227 AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
230 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
231 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
233 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
239 AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
240 AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUVA420P9BE,
241 AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUVA422P9LE,
242 AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUVA422P9BE,
243 AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
244 AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUVA444P9BE,
245 AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
246 AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
247 AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
248 AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
249 AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
250 AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
251 AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE,
252 AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE,
253 AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE,
254 AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE,
255 AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE,
256 AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE,
257 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
258 AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
259 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
260 AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
261 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
262 AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
266 static int query_formats(AVFilterContext *ctx)
268 Stereo3DContext *s = ctx->priv;
269 const enum AVPixelFormat *pix_fmts;
270 AVFilterFormats *fmts_list;
272 switch (s->out.format) {
273 case ANAGLYPH_GM_COLOR:
274 case ANAGLYPH_GM_DUBOIS:
275 case ANAGLYPH_GM_GRAY:
276 case ANAGLYPH_GM_HALF:
277 case ANAGLYPH_RB_GRAY:
278 case ANAGLYPH_RC_COLOR:
279 case ANAGLYPH_RC_DUBOIS:
280 case ANAGLYPH_RC_GRAY:
281 case ANAGLYPH_RC_HALF:
282 case ANAGLYPH_RG_GRAY:
283 case ANAGLYPH_YB_COLOR:
284 case ANAGLYPH_YB_DUBOIS:
285 case ANAGLYPH_YB_GRAY:
286 case ANAGLYPH_YB_HALF:
287 pix_fmts = anaglyph_pix_fmts;
290 pix_fmts = other_pix_fmts;
293 fmts_list = ff_make_format_list(pix_fmts);
295 return AVERROR(ENOMEM);
296 return ff_set_common_formats(ctx, fmts_list);
299 static int config_output(AVFilterLink *outlink)
301 AVFilterContext *ctx = outlink->src;
302 AVFilterLink *inlink = ctx->inputs[0];
303 Stereo3DContext *s = ctx->priv;
304 AVRational aspect = inlink->sample_aspect_ratio;
305 AVRational fps = inlink->frame_rate;
306 AVRational tb = inlink->time_base;
307 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
310 switch (s->in.format) {
311 case SIDE_BY_SIDE_2_LR:
312 case SIDE_BY_SIDE_LR:
313 case SIDE_BY_SIDE_2_RL:
314 case SIDE_BY_SIDE_RL:
316 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
317 return AVERROR_INVALIDDATA;
320 case INTERLEAVE_ROWS_LR:
321 case INTERLEAVE_ROWS_RL:
322 case ABOVE_BELOW_2_LR:
324 case ABOVE_BELOW_2_RL:
326 if (s->out.format == INTERLEAVE_ROWS_LR ||
327 s->out.format == INTERLEAVE_ROWS_RL) {
329 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
330 return AVERROR_INVALIDDATA;
334 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
335 return AVERROR_INVALIDDATA;
341 s->width = inlink->w;
343 s->height = inlink->h;
352 switch (s->in.format) {
353 case SIDE_BY_SIDE_2_LR:
355 case SIDE_BY_SIDE_LR:
356 s->width = inlink->w / 2;
357 s->in.off_right = s->width;
359 case SIDE_BY_SIDE_2_RL:
361 case SIDE_BY_SIDE_RL:
362 s->width = inlink->w / 2;
363 s->in.off_left = s->width;
365 case ABOVE_BELOW_2_LR:
369 s->height = inlink->h / 2;
371 case ABOVE_BELOW_2_RL:
375 s->height = inlink->h / 2;
379 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
383 case INTERLEAVE_ROWS_LR:
384 case INTERLEAVE_ROWS_RL:
386 if (s->in.format == INTERLEAVE_ROWS_RL)
390 if (s->out.format != INTERLEAVE_ROWS_LR &&
391 s->out.format != INTERLEAVE_ROWS_RL &&
392 s->out.format != CHECKERBOARD_LR &&
393 s->out.format != CHECKERBOARD_RL)
394 s->height = inlink->h / 2;
397 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
398 return AVERROR(EINVAL);
401 s->out.width = s->width;
402 s->out.height = s->height;
408 s->out.row_right = 0;
411 switch (s->out.format) {
412 case ANAGLYPH_RB_GRAY:
413 case ANAGLYPH_RG_GRAY:
414 case ANAGLYPH_RC_GRAY:
415 case ANAGLYPH_RC_HALF:
416 case ANAGLYPH_RC_COLOR:
417 case ANAGLYPH_RC_DUBOIS:
418 case ANAGLYPH_GM_GRAY:
419 case ANAGLYPH_GM_HALF:
420 case ANAGLYPH_GM_COLOR:
421 case ANAGLYPH_GM_DUBOIS:
422 case ANAGLYPH_YB_GRAY:
423 case ANAGLYPH_YB_HALF:
424 case ANAGLYPH_YB_COLOR:
425 case ANAGLYPH_YB_DUBOIS: {
428 ff_fill_rgba_map(rgba_map, outlink->format);
429 s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
430 s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
431 s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
434 case SIDE_BY_SIDE_2_LR:
436 case SIDE_BY_SIDE_LR:
437 s->out.width = s->width * 2;
438 s->out.off_right = s->width;
440 case SIDE_BY_SIDE_2_RL:
442 case SIDE_BY_SIDE_RL:
443 s->out.width = s->width * 2;
444 s->out.off_left = s->width;
446 case ABOVE_BELOW_2_LR:
449 s->out.height = s->height * 2;
450 s->out.row_right = s->height;
453 if (s->in.height <= 720)
457 s->out.height = s->height * 2 + s->blanks;
458 s->out.row_right = s->height + s->blanks;
460 case ABOVE_BELOW_2_RL:
463 s->out.height = s->height * 2;
464 s->out.row_left = s->height;
466 case INTERLEAVE_ROWS_LR:
469 s->height = s->height / 2;
470 s->out.off_rstep = 1;
471 s->in.off_rstep = s->in.format != INTERLEAVE_ROWS_RL;
473 case INTERLEAVE_ROWS_RL:
476 s->height = s->height / 2;
477 s->out.off_lstep = 1;
478 s->in.off_lstep = s->in.format != INTERLEAVE_ROWS_LR;
481 s->in.off_left = s->in.off_right;
482 s->in.row_left = s->in.row_right;
483 if (s->in.format == INTERLEAVE_ROWS_LR)
484 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
487 if (s->in.format == INTERLEAVE_ROWS_RL)
488 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
495 case CHECKERBOARD_LR:
496 case CHECKERBOARD_RL:
497 s->out.width = s->width * 2;
500 av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
501 return AVERROR(EINVAL);
504 outlink->w = s->out.width;
505 outlink->h = s->out.height;
506 outlink->frame_rate = fps;
507 outlink->time_base = tb;
508 outlink->sample_aspect_ratio = aspect;
510 if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
512 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
513 av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
514 s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
515 s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
516 s->pheight[0] = s->pheight[3] = s->height;
517 s->hsub = desc->log2_chroma_w;
518 s->vsub = desc->log2_chroma_h;
523 static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
527 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
528 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
529 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
531 return av_clip_uint8(sum >> 16);
534 typedef struct ThreadData {
535 AVFrame *ileft, *iright;
539 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
541 Stereo3DContext *s = ctx->priv;
542 ThreadData *td = arg;
543 AVFrame *ileft = td->ileft;
544 AVFrame *iright = td->iright;
545 AVFrame *out = td->out;
546 int height = s->out.height;
547 int start = (height * jobnr ) / nb_jobs;
548 int end = (height * (jobnr+1)) / nb_jobs;
549 uint8_t *dst = out->data[0];
550 const int **ana_matrix = s->ana_matrix;
552 const uint8_t *lsrc = ileft->data[0];
553 const uint8_t *rsrc = iright->data[0];
554 int out_width = s->out.width;
556 for (y = start; y < end; y++) {
557 o = out->linesize[0] * y;
558 il = s->in_off_left[0] + y * ileft->linesize[0] * s->in.row_step;
559 ir = s->in_off_right[0] + y * iright->linesize[0] * s->in.row_step;
560 for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
561 dst[o ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
562 dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
563 dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
570 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
572 AVFilterContext *ctx = inlink->dst;
573 Stereo3DContext *s = ctx->priv;
574 AVFilterLink *outlink = ctx->outputs[0];
575 AVFrame *out, *oleft, *oright, *ileft, *iright;
576 int out_off_left[4], out_off_right[4];
579 if (s->in.format == s->out.format)
580 return ff_filter_frame(outlink, inpicref);
582 switch (s->in.format) {
591 if (s->in.format == ALTERNATING_RL)
592 FFSWAP(AVFrame *, ileft, iright);
595 ileft = iright = inpicref;
598 out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
600 av_frame_free(&s->prev);
601 av_frame_free(&inpicref);
602 return AVERROR(ENOMEM);
604 av_frame_copy_props(out, inpicref);
606 if (s->out.format == ALTERNATING_LR ||
607 s->out.format == ALTERNATING_RL) {
608 oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
610 av_frame_free(&oleft);
611 av_frame_free(&s->prev);
612 av_frame_free(&inpicref);
613 return AVERROR(ENOMEM);
615 av_frame_copy_props(oright, inpicref);
618 for (i = 0; i < 4; i++) {
619 int hsub = i == 1 || i == 2 ? s->hsub : 0;
620 int vsub = i == 1 || i == 2 ? s->vsub : 0;
621 s->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);
622 s->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);
623 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);
624 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);
627 switch (s->out.format) {
629 for (i = 0; i < s->nb_planes; i++) {
630 int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
631 int b = (s->blanks) >> ((i == 1 || i == 2) ? s->vsub : 0);
633 for (j = h; j < h + b; j++)
634 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
638 case SIDE_BY_SIDE_LR:
639 case SIDE_BY_SIDE_RL:
640 case SIDE_BY_SIDE_2_LR:
641 case SIDE_BY_SIDE_2_RL:
644 case ABOVE_BELOW_2_LR:
645 case ABOVE_BELOW_2_RL:
646 case INTERLEAVE_ROWS_LR:
647 case INTERLEAVE_ROWS_RL:
648 for (i = 0; i < s->nb_planes; i++) {
649 av_image_copy_plane(oleft->data[i] + out_off_left[i],
650 oleft->linesize[i] * s->out.row_step,
651 ileft->data[i] + s->in_off_left[i],
652 ileft->linesize[i] * s->in.row_step,
653 s->linesize[i], s->pheight[i]);
654 av_image_copy_plane(oright->data[i] + out_off_right[i],
655 oright->linesize[i] * s->out.row_step,
656 iright->data[i] + s->in_off_right[i],
657 iright->linesize[i] * s->in.row_step,
658 s->linesize[i], s->pheight[i]);
664 for (i = 0; i < s->nb_planes; i++) {
665 av_image_copy_plane(out->data[i], out->linesize[i],
666 iright->data[i] + s->in_off_left[i],
667 iright->linesize[i] * s->in.row_step,
668 s->linesize[i], s->pheight[i]);
671 case ANAGLYPH_RB_GRAY:
672 case ANAGLYPH_RG_GRAY:
673 case ANAGLYPH_RC_GRAY:
674 case ANAGLYPH_RC_HALF:
675 case ANAGLYPH_RC_COLOR:
676 case ANAGLYPH_RC_DUBOIS:
677 case ANAGLYPH_GM_GRAY:
678 case ANAGLYPH_GM_HALF:
679 case ANAGLYPH_GM_COLOR:
680 case ANAGLYPH_GM_DUBOIS:
681 case ANAGLYPH_YB_GRAY:
682 case ANAGLYPH_YB_HALF:
683 case ANAGLYPH_YB_COLOR:
684 case ANAGLYPH_YB_DUBOIS: {
687 td.ileft = ileft; td.iright = iright; td.out = out;
688 ctx->internal->execute(ctx, filter_slice, &td, NULL,
689 FFMIN(s->out.height, ctx->graph->nb_threads));
692 case CHECKERBOARD_RL:
693 case CHECKERBOARD_LR:
694 for (i = 0; i < s->nb_planes; i++) {
697 for (y = 0; y < s->pheight[i]; y++) {
698 uint8_t *dst = out->data[i] + out->linesize[i] * y;
699 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y + s->in_off_left[i];
700 uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i];
703 if (s->out.format == CHECKERBOARD_RL)
704 FFSWAP(uint8_t*, left, right);
705 switch (s->pixstep[i]) {
707 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
708 dst[x ] = (b&1) == (y&1) ? left[p] : right[p];
709 dst[x+1] = (b&1) != (y&1) ? left[p] : right[p];
713 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
714 AV_WN16(&dst[x ], (b&1) == (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
715 AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
719 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
720 AV_WB24(&dst[x ], (b&1) == (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
721 AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
725 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
726 AV_WN32(&dst[x ], (b&1) == (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
727 AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
731 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
732 AV_WB48(&dst[x ], (b&1) == (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
733 AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
737 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
738 AV_WN64(&dst[x ], (b&1) == (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
739 AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
750 av_frame_free(&inpicref);
751 av_frame_free(&s->prev);
752 if (oright != oleft) {
753 if (s->out.format == ALTERNATING_LR)
754 FFSWAP(AVFrame *, oleft, oright);
755 oright->pts = outlink->frame_count * s->ts_unit;
756 ff_filter_frame(outlink, oright);
758 oleft->pts = outlink->frame_count * s->ts_unit;
759 } else if (s->in.format == ALTERNATING_LR ||
760 s->in.format == ALTERNATING_RL) {
761 out->pts = outlink->frame_count * s->ts_unit;
763 return ff_filter_frame(outlink, out);
766 static av_cold void uninit(AVFilterContext *ctx)
768 Stereo3DContext *s = ctx->priv;
770 av_frame_free(&s->prev);
773 static const AVFilterPad stereo3d_inputs[] = {
776 .type = AVMEDIA_TYPE_VIDEO,
777 .filter_frame = filter_frame,
782 static const AVFilterPad stereo3d_outputs[] = {
785 .type = AVMEDIA_TYPE_VIDEO,
786 .config_props = config_output,
791 AVFilter ff_vf_stereo3d = {
793 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
794 .priv_size = sizeof(Stereo3DContext),
796 .query_formats = query_formats,
797 .inputs = stereo3d_inputs,
798 .outputs = stereo3d_outputs,
799 .priv_class = &stereo3d_class,
800 .flags = AVFILTER_FLAG_SLICE_THREADS,