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 INTERLEAVE_COLS_LR, // column-interleave (left eye first, right eye second)
66 INTERLEAVE_COLS_RL, // column-interleave (right eye first, left eye second)
67 HDMI, // HDMI frame pack (left eye first, right eye second)
68 STEREO_CODE_COUNT // TODO: needs autodetection
71 typedef struct StereoComponent {
72 int format; ///< StereoCode
74 int off_left, off_right;
75 int off_lstep, off_rstep;
76 int row_left, row_right;
80 static const int ana_coeff[][3][6] = {
82 {{19595, 38470, 7471, 0, 0, 0},
84 { 0, 0, 0, 19595, 38470, 7471}},
86 {{19595, 38470, 7471, 0, 0, 0},
87 { 0, 0, 0, 19595, 38470, 7471},
90 {{19595, 38470, 7471, 0, 0, 0},
91 { 0, 0, 0, 19595, 38470, 7471},
92 { 0, 0, 0, 19595, 38470, 7471}},
94 {{19595, 38470, 7471, 0, 0, 0},
95 { 0, 0, 0, 0, 65536, 0},
96 { 0, 0, 0, 0, 0, 65536}},
98 {{65536, 0, 0, 0, 0, 0},
99 { 0, 0, 0, 0, 65536, 0},
100 { 0, 0, 0, 0, 0, 65536}},
101 [ANAGLYPH_RC_DUBOIS] =
102 {{29891, 32800, 11559, -2849, -5763, -102},
103 {-2627, -2479, -1033, 24804, 48080, -1209},
104 { -997, -1350, -358, -4729, -7403, 80373}},
106 {{ 0, 0, 0, 19595, 38470, 7471},
107 {19595, 38470, 7471, 0, 0, 0},
108 { 0, 0, 0, 19595, 38470, 7471}},
110 {{ 0, 0, 0, 65536, 0, 0},
111 {19595, 38470, 7471, 0, 0, 0},
112 { 0, 0, 0, 0, 0, 65536}},
113 [ANAGLYPH_GM_COLOR] =
114 {{ 0, 0, 0, 65536, 0, 0},
115 { 0, 65536, 0, 0, 0, 0},
116 { 0, 0, 0, 0, 0, 65536}},
117 [ANAGLYPH_GM_DUBOIS] =
118 {{-4063,-10354, -2556, 34669, 46203, 1573},
119 {18612, 43778, 9372, -1049, -983, -4260},
120 { -983, -1769, 1376, 590, 4915, 61407}},
122 {{ 0, 0, 0, 19595, 38470, 7471},
123 { 0, 0, 0, 19595, 38470, 7471},
124 {19595, 38470, 7471, 0, 0, 0}},
126 {{ 0, 0, 0, 65536, 0, 0},
127 { 0, 0, 0, 0, 65536, 0},
128 {19595, 38470, 7471, 0, 0, 0}},
129 [ANAGLYPH_YB_COLOR] =
130 {{ 0, 0, 0, 65536, 0, 0},
131 { 0, 0, 0, 0, 65536, 0},
132 { 0, 0, 65536, 0, 0, 0}},
133 [ANAGLYPH_YB_DUBOIS] =
134 {{65535,-12650,18451, -987, -7590, -1049},
135 {-1604, 56032, 4196, 370, 3826, -1049},
136 {-2345,-10676, 1358, 5801, 11416, 56217}},
139 typedef struct Stereo3DContext {
140 const AVClass *class;
141 StereoComponent in, out;
143 const int *ana_matrix[3];
152 int in_off_left[4], in_off_right[4];
155 #define OFFSET(x) offsetof(Stereo3DContext, x)
156 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
158 static const AVOption stereo3d_options[] = {
159 { "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"},
160 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
161 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
162 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
163 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
164 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "in" },
165 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "in" },
166 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
167 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
168 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
169 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
170 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "in" },
171 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "in" },
172 { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
173 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
174 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
175 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
176 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
177 { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
178 { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
179 { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
180 { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
181 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "out" },
182 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "out" },
183 { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
184 { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
185 { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
186 { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
187 { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
188 { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
189 { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
190 { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
191 { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
192 { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
193 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
194 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
195 { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
196 { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
197 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
198 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
199 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
200 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
201 { "chl", "checkerboard left first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_LR}, 0, 0, FLAGS, "out" },
202 { "chr", "checkerboard right first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_RL}, 0, 0, FLAGS, "out" },
203 { "icl", "interleave columns left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_LR}, 0, 0, FLAGS, "out" },
204 { "icr", "interleave columns right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_RL}, 0, 0, FLAGS, "out" },
205 { "hdmi", "HDMI frame pack", 0, AV_OPT_TYPE_CONST, {.i64=HDMI}, 0, 0, FLAGS, "out" },
209 AVFILTER_DEFINE_CLASS(stereo3d);
211 static const enum AVPixelFormat anaglyph_pix_fmts[] = {
212 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
216 static const enum AVPixelFormat other_pix_fmts[] = {
217 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
218 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
219 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
220 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
221 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
222 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
223 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
224 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
225 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
227 AV_PIX_FMT_GBRP9BE, AV_PIX_FMT_GBRP9LE,
228 AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
229 AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
230 AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
231 AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
234 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
235 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
237 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
243 AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
244 AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUVA420P9BE,
245 AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUVA422P9LE,
246 AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUVA422P9BE,
247 AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
248 AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUVA444P9BE,
249 AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
250 AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
251 AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
252 AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
253 AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
254 AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
255 AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE,
256 AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE,
257 AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE,
258 AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE,
259 AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE,
260 AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE,
261 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
262 AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
263 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
264 AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
265 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
266 AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
270 static int query_formats(AVFilterContext *ctx)
272 Stereo3DContext *s = ctx->priv;
273 const enum AVPixelFormat *pix_fmts;
274 AVFilterFormats *fmts_list;
276 switch (s->out.format) {
277 case ANAGLYPH_GM_COLOR:
278 case ANAGLYPH_GM_DUBOIS:
279 case ANAGLYPH_GM_GRAY:
280 case ANAGLYPH_GM_HALF:
281 case ANAGLYPH_RB_GRAY:
282 case ANAGLYPH_RC_COLOR:
283 case ANAGLYPH_RC_DUBOIS:
284 case ANAGLYPH_RC_GRAY:
285 case ANAGLYPH_RC_HALF:
286 case ANAGLYPH_RG_GRAY:
287 case ANAGLYPH_YB_COLOR:
288 case ANAGLYPH_YB_DUBOIS:
289 case ANAGLYPH_YB_GRAY:
290 case ANAGLYPH_YB_HALF:
291 pix_fmts = anaglyph_pix_fmts;
294 pix_fmts = other_pix_fmts;
297 fmts_list = ff_make_format_list(pix_fmts);
299 return AVERROR(ENOMEM);
300 return ff_set_common_formats(ctx, fmts_list);
303 static int config_output(AVFilterLink *outlink)
305 AVFilterContext *ctx = outlink->src;
306 AVFilterLink *inlink = ctx->inputs[0];
307 Stereo3DContext *s = ctx->priv;
308 AVRational aspect = inlink->sample_aspect_ratio;
309 AVRational fps = inlink->frame_rate;
310 AVRational tb = inlink->time_base;
311 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
314 switch (s->in.format) {
315 case SIDE_BY_SIDE_2_LR:
316 case SIDE_BY_SIDE_LR:
317 case SIDE_BY_SIDE_2_RL:
318 case SIDE_BY_SIDE_RL:
320 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
321 return AVERROR_INVALIDDATA;
324 case INTERLEAVE_ROWS_LR:
325 case INTERLEAVE_ROWS_RL:
326 case ABOVE_BELOW_2_LR:
328 case ABOVE_BELOW_2_RL:
330 if (s->out.format == INTERLEAVE_ROWS_LR ||
331 s->out.format == INTERLEAVE_ROWS_RL) {
333 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
334 return AVERROR_INVALIDDATA;
338 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
339 return AVERROR_INVALIDDATA;
345 s->width = inlink->w;
347 s->height = inlink->h;
356 switch (s->in.format) {
357 case SIDE_BY_SIDE_2_LR:
359 case SIDE_BY_SIDE_LR:
360 s->width = inlink->w / 2;
361 s->in.off_right = s->width;
363 case SIDE_BY_SIDE_2_RL:
365 case SIDE_BY_SIDE_RL:
366 s->width = inlink->w / 2;
367 s->in.off_left = s->width;
369 case ABOVE_BELOW_2_LR:
373 s->height = inlink->h / 2;
375 case ABOVE_BELOW_2_RL:
379 s->height = inlink->h / 2;
383 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
387 case INTERLEAVE_ROWS_LR:
388 case INTERLEAVE_ROWS_RL:
390 if (s->in.format == INTERLEAVE_ROWS_RL)
394 if (s->out.format != INTERLEAVE_ROWS_LR &&
395 s->out.format != INTERLEAVE_ROWS_RL &&
396 s->out.format != CHECKERBOARD_LR &&
397 s->out.format != CHECKERBOARD_RL)
398 s->height = inlink->h / 2;
401 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
402 return AVERROR(EINVAL);
405 s->out.width = s->width;
406 s->out.height = s->height;
412 s->out.row_right = 0;
415 switch (s->out.format) {
416 case ANAGLYPH_RB_GRAY:
417 case ANAGLYPH_RG_GRAY:
418 case ANAGLYPH_RC_GRAY:
419 case ANAGLYPH_RC_HALF:
420 case ANAGLYPH_RC_COLOR:
421 case ANAGLYPH_RC_DUBOIS:
422 case ANAGLYPH_GM_GRAY:
423 case ANAGLYPH_GM_HALF:
424 case ANAGLYPH_GM_COLOR:
425 case ANAGLYPH_GM_DUBOIS:
426 case ANAGLYPH_YB_GRAY:
427 case ANAGLYPH_YB_HALF:
428 case ANAGLYPH_YB_COLOR:
429 case ANAGLYPH_YB_DUBOIS: {
432 ff_fill_rgba_map(rgba_map, outlink->format);
433 s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
434 s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
435 s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
438 case SIDE_BY_SIDE_2_LR:
440 case SIDE_BY_SIDE_LR:
441 s->out.width = s->width * 2;
442 s->out.off_right = s->width;
444 case SIDE_BY_SIDE_2_RL:
446 case SIDE_BY_SIDE_RL:
447 s->out.width = s->width * 2;
448 s->out.off_left = s->width;
450 case ABOVE_BELOW_2_LR:
453 s->out.height = s->height * 2;
454 s->out.row_right = s->height;
457 if (s->in.height <= 720)
461 s->out.height = s->height * 2 + s->blanks;
462 s->out.row_right = s->height + s->blanks;
464 case ABOVE_BELOW_2_RL:
467 s->out.height = s->height * 2;
468 s->out.row_left = s->height;
470 case INTERLEAVE_ROWS_LR:
473 s->height = s->height / 2;
474 s->out.off_rstep = 1;
475 s->in.off_rstep = s->in.format != INTERLEAVE_ROWS_RL;
477 case INTERLEAVE_ROWS_RL:
480 s->height = s->height / 2;
481 s->out.off_lstep = 1;
482 s->in.off_lstep = s->in.format != INTERLEAVE_ROWS_LR;
485 s->in.off_left = s->in.off_right;
486 s->in.row_left = s->in.row_right;
487 if (s->in.format == INTERLEAVE_ROWS_LR)
488 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
491 if (s->in.format == INTERLEAVE_ROWS_RL)
492 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
499 case CHECKERBOARD_LR:
500 case CHECKERBOARD_RL:
501 s->out.width = s->width * 2;
503 case INTERLEAVE_COLS_LR:
504 case INTERLEAVE_COLS_RL:
505 s->out.width = s->width * 2;
508 av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
509 return AVERROR(EINVAL);
512 outlink->w = s->out.width;
513 outlink->h = s->out.height;
514 outlink->frame_rate = fps;
515 outlink->time_base = tb;
516 outlink->sample_aspect_ratio = aspect;
518 if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
520 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
521 av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
522 s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
523 s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
524 s->pheight[0] = s->pheight[3] = s->height;
525 s->hsub = desc->log2_chroma_w;
526 s->vsub = desc->log2_chroma_h;
531 static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
535 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
536 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
537 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
539 return av_clip_uint8(sum >> 16);
542 typedef struct ThreadData {
543 AVFrame *ileft, *iright;
547 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
549 Stereo3DContext *s = ctx->priv;
550 ThreadData *td = arg;
551 AVFrame *ileft = td->ileft;
552 AVFrame *iright = td->iright;
553 AVFrame *out = td->out;
554 int height = s->out.height;
555 int start = (height * jobnr ) / nb_jobs;
556 int end = (height * (jobnr+1)) / nb_jobs;
557 uint8_t *dst = out->data[0];
558 const int **ana_matrix = s->ana_matrix;
560 const uint8_t *lsrc = ileft->data[0];
561 const uint8_t *rsrc = iright->data[0];
562 int out_width = s->out.width;
564 for (y = start; y < end; y++) {
565 o = out->linesize[0] * y;
566 il = s->in_off_left[0] + y * ileft->linesize[0] * s->in.row_step;
567 ir = s->in_off_right[0] + y * iright->linesize[0] * s->in.row_step;
568 for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
569 dst[o ] = ana_convert(ana_matrix[0], lsrc + il, rsrc + ir);
570 dst[o + 1] = ana_convert(ana_matrix[1], lsrc + il, rsrc + ir);
571 dst[o + 2] = ana_convert(ana_matrix[2], lsrc + il, rsrc + ir);
578 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
580 AVFilterContext *ctx = inlink->dst;
581 Stereo3DContext *s = ctx->priv;
582 AVFilterLink *outlink = ctx->outputs[0];
583 AVFrame *out, *oleft, *oright, *ileft, *iright;
584 int out_off_left[4], out_off_right[4];
587 if (s->in.format == s->out.format)
588 return ff_filter_frame(outlink, inpicref);
590 switch (s->in.format) {
599 if (s->in.format == ALTERNATING_RL)
600 FFSWAP(AVFrame *, ileft, iright);
603 ileft = iright = inpicref;
606 out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
608 av_frame_free(&s->prev);
609 av_frame_free(&inpicref);
610 return AVERROR(ENOMEM);
612 av_frame_copy_props(out, inpicref);
614 if (s->out.format == ALTERNATING_LR ||
615 s->out.format == ALTERNATING_RL) {
616 oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
618 av_frame_free(&oleft);
619 av_frame_free(&s->prev);
620 av_frame_free(&inpicref);
621 return AVERROR(ENOMEM);
623 av_frame_copy_props(oright, inpicref);
626 for (i = 0; i < 4; i++) {
627 int hsub = i == 1 || i == 2 ? s->hsub : 0;
628 int vsub = i == 1 || i == 2 ? s->vsub : 0;
629 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);
630 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);
631 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);
632 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);
635 switch (s->out.format) {
637 for (i = 0; i < s->nb_planes; i++) {
638 int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
639 int b = (s->blanks) >> ((i == 1 || i == 2) ? s->vsub : 0);
641 for (j = h; j < h + b; j++)
642 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
646 case SIDE_BY_SIDE_LR:
647 case SIDE_BY_SIDE_RL:
648 case SIDE_BY_SIDE_2_LR:
649 case SIDE_BY_SIDE_2_RL:
652 case ABOVE_BELOW_2_LR:
653 case ABOVE_BELOW_2_RL:
654 case INTERLEAVE_ROWS_LR:
655 case INTERLEAVE_ROWS_RL:
656 for (i = 0; i < s->nb_planes; i++) {
657 av_image_copy_plane(oleft->data[i] + out_off_left[i],
658 oleft->linesize[i] * s->out.row_step,
659 ileft->data[i] + s->in_off_left[i],
660 ileft->linesize[i] * s->in.row_step,
661 s->linesize[i], s->pheight[i]);
662 av_image_copy_plane(oright->data[i] + out_off_right[i],
663 oright->linesize[i] * s->out.row_step,
664 iright->data[i] + s->in_off_right[i],
665 iright->linesize[i] * s->in.row_step,
666 s->linesize[i], s->pheight[i]);
672 for (i = 0; i < s->nb_planes; i++) {
673 av_image_copy_plane(out->data[i], out->linesize[i],
674 iright->data[i] + s->in_off_left[i],
675 iright->linesize[i] * s->in.row_step,
676 s->linesize[i], s->pheight[i]);
679 case ANAGLYPH_RB_GRAY:
680 case ANAGLYPH_RG_GRAY:
681 case ANAGLYPH_RC_GRAY:
682 case ANAGLYPH_RC_HALF:
683 case ANAGLYPH_RC_COLOR:
684 case ANAGLYPH_RC_DUBOIS:
685 case ANAGLYPH_GM_GRAY:
686 case ANAGLYPH_GM_HALF:
687 case ANAGLYPH_GM_COLOR:
688 case ANAGLYPH_GM_DUBOIS:
689 case ANAGLYPH_YB_GRAY:
690 case ANAGLYPH_YB_HALF:
691 case ANAGLYPH_YB_COLOR:
692 case ANAGLYPH_YB_DUBOIS: {
695 td.ileft = ileft; td.iright = iright; td.out = out;
696 ctx->internal->execute(ctx, filter_slice, &td, NULL,
697 FFMIN(s->out.height, ctx->graph->nb_threads));
700 case CHECKERBOARD_RL:
701 case CHECKERBOARD_LR:
702 for (i = 0; i < s->nb_planes; i++) {
705 for (y = 0; y < s->pheight[i]; y++) {
706 uint8_t *dst = out->data[i] + out->linesize[i] * y;
707 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y + s->in_off_left[i];
708 uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i];
711 if (s->out.format == CHECKERBOARD_RL)
712 FFSWAP(uint8_t*, left, right);
713 switch (s->pixstep[i]) {
715 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
716 dst[x ] = (b&1) == (y&1) ? left[p] : right[p];
717 dst[x+1] = (b&1) != (y&1) ? left[p] : right[p];
721 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
722 AV_WN16(&dst[x ], (b&1) == (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
723 AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
727 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
728 AV_WB24(&dst[x ], (b&1) == (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
729 AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
733 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
734 AV_WN32(&dst[x ], (b&1) == (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
735 AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
739 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
740 AV_WB48(&dst[x ], (b&1) == (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
741 AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
745 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
746 AV_WN64(&dst[x ], (b&1) == (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
747 AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
754 case INTERLEAVE_COLS_LR:
755 case INTERLEAVE_COLS_RL:
756 for (i = 0; i < s->nb_planes; i++) {
759 for (y = 0; y < s->pheight[i]; y++) {
760 uint8_t *dst = out->data[i] + out->linesize[i] * y;
761 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i];
762 uint8_t *right = iright->data[i] + iright->linesize[i] * y * s->in.row_step + s->in_off_right[i];
765 if (s->out.format == INTERLEAVE_COLS_LR)
766 FFSWAP(uint8_t*, left, right);
768 switch (s->pixstep[i]) {
770 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
771 dst[x ] = b&1 ? left[p] : right[p];
772 dst[x+1] = !(b&1) ? left[p] : right[p];
776 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
777 AV_WN16(&dst[x ], b&1 ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
778 AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
782 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
783 AV_WB24(&dst[x ], b&1 ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
784 AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
788 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
789 AV_WN32(&dst[x ], b&1 ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
790 AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
794 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
795 AV_WB48(&dst[x ], b&1 ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
796 AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
800 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
801 AV_WN64(&dst[x ], b&1 ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
802 AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
813 av_frame_free(&inpicref);
814 av_frame_free(&s->prev);
815 if (oright != oleft) {
816 if (s->out.format == ALTERNATING_LR)
817 FFSWAP(AVFrame *, oleft, oright);
818 oright->pts = outlink->frame_count * s->ts_unit;
819 ff_filter_frame(outlink, oright);
821 oleft->pts = outlink->frame_count * s->ts_unit;
822 } else if (s->in.format == ALTERNATING_LR ||
823 s->in.format == ALTERNATING_RL) {
824 out->pts = outlink->frame_count * s->ts_unit;
826 return ff_filter_frame(outlink, out);
829 static av_cold void uninit(AVFilterContext *ctx)
831 Stereo3DContext *s = ctx->priv;
833 av_frame_free(&s->prev);
836 static const AVFilterPad stereo3d_inputs[] = {
839 .type = AVMEDIA_TYPE_VIDEO,
840 .filter_frame = filter_frame,
845 static const AVFilterPad stereo3d_outputs[] = {
848 .type = AVMEDIA_TYPE_VIDEO,
849 .config_props = config_output,
854 AVFilter ff_vf_stereo3d = {
856 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
857 .priv_size = sizeof(Stereo3DContext),
859 .query_formats = query_formats,
860 .inputs = stereo3d_inputs,
861 .outputs = stereo3d_outputs,
862 .priv_class = &stereo3d_class,
863 .flags = AVFILTER_FLAG_SLICE_THREADS,