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:
331 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
332 return AVERROR_INVALIDDATA;
338 s->width = inlink->w;
340 s->height = inlink->h;
349 switch (s->in.format) {
350 case SIDE_BY_SIDE_2_LR:
352 case SIDE_BY_SIDE_LR:
353 s->width = inlink->w / 2;
354 s->in.off_right = s->width;
356 case SIDE_BY_SIDE_2_RL:
358 case SIDE_BY_SIDE_RL:
359 s->width = inlink->w / 2;
360 s->in.off_left = s->width;
362 case ABOVE_BELOW_2_LR:
366 s->height = inlink->h / 2;
368 case ABOVE_BELOW_2_RL:
372 s->height = inlink->h / 2;
379 case INTERLEAVE_ROWS_LR:
380 case INTERLEAVE_ROWS_RL:
382 if (s->in.format == INTERLEAVE_ROWS_RL)
386 if (s->out.format != CHECKERBOARD_LR &&
387 s->out.format != CHECKERBOARD_RL)
388 s->height = inlink->h / 2;
391 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
392 return AVERROR(EINVAL);
395 s->out.width = s->width;
396 s->out.height = s->height;
402 s->out.row_right = 0;
405 switch (s->out.format) {
406 case ANAGLYPH_RB_GRAY:
407 case ANAGLYPH_RG_GRAY:
408 case ANAGLYPH_RC_GRAY:
409 case ANAGLYPH_RC_HALF:
410 case ANAGLYPH_RC_COLOR:
411 case ANAGLYPH_RC_DUBOIS:
412 case ANAGLYPH_GM_GRAY:
413 case ANAGLYPH_GM_HALF:
414 case ANAGLYPH_GM_COLOR:
415 case ANAGLYPH_GM_DUBOIS:
416 case ANAGLYPH_YB_GRAY:
417 case ANAGLYPH_YB_HALF:
418 case ANAGLYPH_YB_COLOR:
419 case ANAGLYPH_YB_DUBOIS: {
422 ff_fill_rgba_map(rgba_map, outlink->format);
423 s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
424 s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
425 s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
428 case SIDE_BY_SIDE_2_LR:
430 case SIDE_BY_SIDE_LR:
431 s->out.width = s->width * 2;
432 s->out.off_right = s->width;
434 case SIDE_BY_SIDE_2_RL:
436 case SIDE_BY_SIDE_RL:
437 s->out.width = s->width * 2;
438 s->out.off_left = s->width;
440 case ABOVE_BELOW_2_LR:
443 s->out.height = s->height * 2;
444 s->out.row_right = s->height;
447 if (s->height != 720 && s->height != 1080) {
448 av_log(ctx, AV_LOG_ERROR, "Only 720 and 1080 height supported\n");
449 return AVERROR(EINVAL);
452 s->blanks = s->height / 24;
453 s->out.height = s->height * 2 + s->blanks;
454 s->out.row_right = s->height + s->blanks;
456 case ABOVE_BELOW_2_RL:
459 s->out.height = s->height * 2;
460 s->out.row_left = s->height;
462 case INTERLEAVE_ROWS_LR:
463 s->in.row_step = 1 + (s->in.format == INTERLEAVE_ROWS_RL);
465 s->out.height = s->height * 2;
466 s->out.off_rstep = 1;
467 s->in.off_rstep = s->in.format != INTERLEAVE_ROWS_RL;
469 case INTERLEAVE_ROWS_RL:
470 s->in.row_step = 1 + (s->in.format == INTERLEAVE_ROWS_LR);
472 s->out.height = s->height * 2;
473 s->out.off_lstep = 1;
474 s->in.off_lstep = s->in.format != INTERLEAVE_ROWS_LR;
477 s->in.off_left = s->in.off_right;
478 s->in.row_left = s->in.row_right;
479 if (s->in.format == INTERLEAVE_ROWS_LR)
480 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
483 if (s->in.format == INTERLEAVE_ROWS_RL)
484 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
491 case CHECKERBOARD_LR:
492 case CHECKERBOARD_RL:
493 s->out.width = s->width * 2;
495 case INTERLEAVE_COLS_LR:
496 case INTERLEAVE_COLS_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 static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
535 ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
536 int width, int height,
537 const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
541 for (y = 0; y < height; y++) {
542 for (o = 0, x = 0; x < width; x++, o+= 3) {
543 dst[o ] = ana_convert(ana_matrix_r, lsrc + o, rsrc + o);
544 dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o, rsrc + o);
545 dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o, rsrc + o);
554 typedef struct ThreadData {
555 AVFrame *ileft, *iright;
559 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
561 Stereo3DContext *s = ctx->priv;
562 ThreadData *td = arg;
563 AVFrame *ileft = td->ileft;
564 AVFrame *iright = td->iright;
565 AVFrame *out = td->out;
566 int height = s->out.height;
567 int start = (height * jobnr ) / nb_jobs;
568 int end = (height * (jobnr+1)) / nb_jobs;
569 const int **ana_matrix = s->ana_matrix;
571 anaglyph(out->data[0] + out->linesize[0] * start,
572 ileft ->data[0] + s->in_off_left [0] + ileft->linesize[0] * start * s->in.row_step,
573 iright->data[0] + s->in_off_right[0] + iright->linesize[0] * start * s->in.row_step,
575 ileft->linesize[0] * s->in.row_step,
576 iright->linesize[0] * s->in.row_step,
577 s->out.width, end - start,
578 ana_matrix[0], ana_matrix[1], ana_matrix[2]);
583 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
585 AVFilterContext *ctx = inlink->dst;
586 Stereo3DContext *s = ctx->priv;
587 AVFilterLink *outlink = ctx->outputs[0];
588 AVFrame *out, *oleft, *oright, *ileft, *iright;
589 int out_off_left[4], out_off_right[4];
592 if (s->in.format == s->out.format)
593 return ff_filter_frame(outlink, inpicref);
595 switch (s->in.format) {
604 if (s->in.format == ALTERNATING_RL)
605 FFSWAP(AVFrame *, ileft, iright);
608 ileft = iright = inpicref;
611 out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
613 av_frame_free(&s->prev);
614 av_frame_free(&inpicref);
615 return AVERROR(ENOMEM);
617 av_frame_copy_props(out, inpicref);
619 if (s->out.format == ALTERNATING_LR ||
620 s->out.format == ALTERNATING_RL) {
621 oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
623 av_frame_free(&oleft);
624 av_frame_free(&s->prev);
625 av_frame_free(&inpicref);
626 return AVERROR(ENOMEM);
628 av_frame_copy_props(oright, inpicref);
631 for (i = 0; i < 4; i++) {
632 int hsub = i == 1 || i == 2 ? s->hsub : 0;
633 int vsub = i == 1 || i == 2 ? s->vsub : 0;
634 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);
635 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);
636 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);
637 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);
640 switch (s->out.format) {
642 for (i = 0; i < s->nb_planes; i++) {
643 int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
644 int b = (s->blanks) >> ((i == 1 || i == 2) ? s->vsub : 0);
646 for (j = h; j < h + b; j++)
647 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
651 case SIDE_BY_SIDE_LR:
652 case SIDE_BY_SIDE_RL:
653 case SIDE_BY_SIDE_2_LR:
654 case SIDE_BY_SIDE_2_RL:
657 case ABOVE_BELOW_2_LR:
658 case ABOVE_BELOW_2_RL:
659 case INTERLEAVE_ROWS_LR:
660 case INTERLEAVE_ROWS_RL:
661 for (i = 0; i < s->nb_planes; i++) {
662 av_image_copy_plane(oleft->data[i] + out_off_left[i],
663 oleft->linesize[i] * s->out.row_step,
664 ileft->data[i] + s->in_off_left[i],
665 ileft->linesize[i] * s->in.row_step,
666 s->linesize[i], s->pheight[i]);
667 av_image_copy_plane(oright->data[i] + out_off_right[i],
668 oright->linesize[i] * s->out.row_step,
669 iright->data[i] + s->in_off_right[i],
670 iright->linesize[i] * s->in.row_step,
671 s->linesize[i], s->pheight[i]);
677 for (i = 0; i < s->nb_planes; i++) {
678 av_image_copy_plane(out->data[i], out->linesize[i],
679 iright->data[i] + s->in_off_left[i],
680 iright->linesize[i] * s->in.row_step,
681 s->linesize[i], s->pheight[i]);
684 case ANAGLYPH_RB_GRAY:
685 case ANAGLYPH_RG_GRAY:
686 case ANAGLYPH_RC_GRAY:
687 case ANAGLYPH_RC_HALF:
688 case ANAGLYPH_RC_COLOR:
689 case ANAGLYPH_RC_DUBOIS:
690 case ANAGLYPH_GM_GRAY:
691 case ANAGLYPH_GM_HALF:
692 case ANAGLYPH_GM_COLOR:
693 case ANAGLYPH_GM_DUBOIS:
694 case ANAGLYPH_YB_GRAY:
695 case ANAGLYPH_YB_HALF:
696 case ANAGLYPH_YB_COLOR:
697 case ANAGLYPH_YB_DUBOIS: {
700 td.ileft = ileft; td.iright = iright; td.out = out;
701 ctx->internal->execute(ctx, filter_slice, &td, NULL,
702 FFMIN(s->out.height, ctx->graph->nb_threads));
705 case CHECKERBOARD_RL:
706 case CHECKERBOARD_LR:
707 for (i = 0; i < s->nb_planes; i++) {
710 for (y = 0; y < s->pheight[i]; y++) {
711 uint8_t *dst = out->data[i] + out->linesize[i] * y;
712 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y + s->in_off_left[i];
713 uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i];
716 if (s->out.format == CHECKERBOARD_RL)
717 FFSWAP(uint8_t*, left, right);
718 switch (s->pixstep[i]) {
720 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
721 dst[x ] = (b&1) == (y&1) ? left[p] : right[p];
722 dst[x+1] = (b&1) != (y&1) ? left[p] : right[p];
726 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
727 AV_WN16(&dst[x ], (b&1) == (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
728 AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
732 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
733 AV_WB24(&dst[x ], (b&1) == (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
734 AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
738 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
739 AV_WN32(&dst[x ], (b&1) == (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
740 AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
744 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
745 AV_WB48(&dst[x ], (b&1) == (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
746 AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
750 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
751 AV_WN64(&dst[x ], (b&1) == (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
752 AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
759 case INTERLEAVE_COLS_LR:
760 case INTERLEAVE_COLS_RL:
761 for (i = 0; i < s->nb_planes; i++) {
764 for (y = 0; y < s->pheight[i]; y++) {
765 uint8_t *dst = out->data[i] + out->linesize[i] * y;
766 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i];
767 uint8_t *right = iright->data[i] + iright->linesize[i] * y * s->in.row_step + s->in_off_right[i];
770 if (s->out.format == INTERLEAVE_COLS_LR)
771 FFSWAP(uint8_t*, left, right);
773 switch (s->pixstep[i]) {
775 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
776 dst[x ] = b&1 ? left[p] : right[p];
777 dst[x+1] = !(b&1) ? left[p] : right[p];
781 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
782 AV_WN16(&dst[x ], b&1 ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
783 AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
787 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
788 AV_WB24(&dst[x ], b&1 ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
789 AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
793 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
794 AV_WN32(&dst[x ], b&1 ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
795 AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
799 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
800 AV_WB48(&dst[x ], b&1 ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
801 AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
805 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
806 AV_WN64(&dst[x ], b&1 ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
807 AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
818 av_frame_free(&inpicref);
819 av_frame_free(&s->prev);
820 if (oright != oleft) {
821 if (s->out.format == ALTERNATING_LR)
822 FFSWAP(AVFrame *, oleft, oright);
823 oright->pts = outlink->frame_count * s->ts_unit;
824 ff_filter_frame(outlink, oright);
826 oleft->pts = outlink->frame_count * s->ts_unit;
827 } else if (s->in.format == ALTERNATING_LR ||
828 s->in.format == ALTERNATING_RL) {
829 out->pts = outlink->frame_count * s->ts_unit;
831 return ff_filter_frame(outlink, out);
834 static av_cold void uninit(AVFilterContext *ctx)
836 Stereo3DContext *s = ctx->priv;
838 av_frame_free(&s->prev);
841 static const AVFilterPad stereo3d_inputs[] = {
844 .type = AVMEDIA_TYPE_VIDEO,
845 .filter_frame = filter_frame,
850 static const AVFilterPad stereo3d_outputs[] = {
853 .type = AVMEDIA_TYPE_VIDEO,
854 .config_props = config_output,
859 AVFilter ff_vf_stereo3d = {
861 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
862 .priv_size = sizeof(Stereo3DContext),
864 .query_formats = query_formats,
865 .inputs = stereo3d_inputs,
866 .outputs = stereo3d_outputs,
867 .priv_class = &stereo3d_class,
868 .flags = AVFILTER_FLAG_SLICE_THREADS,