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"
36 ANAGLYPH_RC_GRAY, // anaglyph red/cyan gray
37 ANAGLYPH_RC_HALF, // anaglyph red/cyan half colored
38 ANAGLYPH_RC_COLOR, // anaglyph red/cyan colored
39 ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
40 ANAGLYPH_GM_GRAY, // anaglyph green/magenta gray
41 ANAGLYPH_GM_HALF, // anaglyph green/magenta half colored
42 ANAGLYPH_GM_COLOR, // anaglyph green/magenta colored
43 ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
44 ANAGLYPH_YB_GRAY, // anaglyph yellow/blue gray
45 ANAGLYPH_YB_HALF, // anaglyph yellow/blue half colored
46 ANAGLYPH_YB_COLOR, // anaglyph yellow/blue colored
47 ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
48 ANAGLYPH_RB_GRAY, // anaglyph red/blue gray
49 ANAGLYPH_RG_GRAY, // anaglyph red/green gray
50 MONO_L, // mono output for debugging (left eye only)
51 MONO_R, // mono output for debugging (right eye only)
52 INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
53 INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
54 SIDE_BY_SIDE_LR, // side by side parallel (left eye left, right eye right)
55 SIDE_BY_SIDE_RL, // side by side crosseye (right eye left, left eye right)
56 SIDE_BY_SIDE_2_LR, // side by side parallel with half width resolution
57 SIDE_BY_SIDE_2_RL, // side by side crosseye with half width resolution
58 ABOVE_BELOW_LR, // above-below (left eye above, right eye below)
59 ABOVE_BELOW_RL, // above-below (right eye above, left eye below)
60 ABOVE_BELOW_2_LR, // above-below with half height resolution
61 ABOVE_BELOW_2_RL, // above-below with half height resolution
62 ALTERNATING_LR, // alternating frames (left eye first, right eye second)
63 ALTERNATING_RL, // alternating frames (right eye first, left eye second)
64 CHECKERBOARD_LR, // checkerboard pattern (left eye first, right eye second)
65 CHECKERBOARD_RL, // checkerboard pattern (right eye first, left eye second)
66 INTERLEAVE_COLS_LR, // column-interleave (left eye first, right eye second)
67 INTERLEAVE_COLS_RL, // column-interleave (right eye first, left eye second)
68 HDMI, // HDMI frame pack (left eye first, right eye second)
69 STEREO_CODE_COUNT // TODO: needs autodetection
72 typedef struct StereoComponent {
73 int format; ///< StereoCode
75 int off_left, off_right;
76 int off_lstep, off_rstep;
77 int row_left, row_right;
81 static const int ana_coeff[][3][6] = {
83 {{19595, 38470, 7471, 0, 0, 0},
85 { 0, 0, 0, 19595, 38470, 7471}},
87 {{19595, 38470, 7471, 0, 0, 0},
88 { 0, 0, 0, 19595, 38470, 7471},
91 {{19595, 38470, 7471, 0, 0, 0},
92 { 0, 0, 0, 19595, 38470, 7471},
93 { 0, 0, 0, 19595, 38470, 7471}},
95 {{19595, 38470, 7471, 0, 0, 0},
96 { 0, 0, 0, 0, 65536, 0},
97 { 0, 0, 0, 0, 0, 65536}},
99 {{65536, 0, 0, 0, 0, 0},
100 { 0, 0, 0, 0, 65536, 0},
101 { 0, 0, 0, 0, 0, 65536}},
102 [ANAGLYPH_RC_DUBOIS] =
103 {{29891, 32800, 11559, -2849, -5763, -102},
104 {-2627, -2479, -1033, 24804, 48080, -1209},
105 { -997, -1350, -358, -4729, -7403, 80373}},
107 {{ 0, 0, 0, 19595, 38470, 7471},
108 {19595, 38470, 7471, 0, 0, 0},
109 { 0, 0, 0, 19595, 38470, 7471}},
111 {{ 0, 0, 0, 65536, 0, 0},
112 {19595, 38470, 7471, 0, 0, 0},
113 { 0, 0, 0, 0, 0, 65536}},
114 [ANAGLYPH_GM_COLOR] =
115 {{ 0, 0, 0, 65536, 0, 0},
116 { 0, 65536, 0, 0, 0, 0},
117 { 0, 0, 0, 0, 0, 65536}},
118 [ANAGLYPH_GM_DUBOIS] =
119 {{-4063,-10354, -2556, 34669, 46203, 1573},
120 {18612, 43778, 9372, -1049, -983, -4260},
121 { -983, -1769, 1376, 590, 4915, 61407}},
123 {{ 0, 0, 0, 19595, 38470, 7471},
124 { 0, 0, 0, 19595, 38470, 7471},
125 {19595, 38470, 7471, 0, 0, 0}},
127 {{ 0, 0, 0, 65536, 0, 0},
128 { 0, 0, 0, 0, 65536, 0},
129 {19595, 38470, 7471, 0, 0, 0}},
130 [ANAGLYPH_YB_COLOR] =
131 {{ 0, 0, 0, 65536, 0, 0},
132 { 0, 0, 0, 0, 65536, 0},
133 { 0, 0, 65536, 0, 0, 0}},
134 [ANAGLYPH_YB_DUBOIS] =
135 {{65535,-12650,18451, -987, -7590, -1049},
136 {-1604, 56032, 4196, 370, 3826, -1049},
137 {-2345,-10676, 1358, 5801, 11416, 56217}},
140 typedef struct Stereo3DContext {
141 const AVClass *class;
142 StereoComponent in, out;
144 const int *ana_matrix[3];
153 int in_off_left[4], in_off_right[4];
154 Stereo3DDSPContext dsp;
157 #define OFFSET(x) offsetof(Stereo3DContext, x)
158 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
160 static const AVOption stereo3d_options[] = {
161 { "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"},
162 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
163 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
164 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
165 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
166 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "in" },
167 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "in" },
168 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
169 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
170 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
171 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
172 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "in" },
173 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "in" },
174 { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
175 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
176 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
177 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
178 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
179 { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
180 { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
181 { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
182 { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
183 { "al", "alternating frames left first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_LR}, 0, 0, FLAGS, "out" },
184 { "ar", "alternating frames right first", 0, AV_OPT_TYPE_CONST, {.i64=ALTERNATING_RL}, 0, 0, FLAGS, "out" },
185 { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
186 { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
187 { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
188 { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
189 { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
190 { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
191 { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
192 { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
193 { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
194 { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
195 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
196 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
197 { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
198 { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
199 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
200 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
201 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
202 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
203 { "chl", "checkerboard left first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_LR}, 0, 0, FLAGS, "out" },
204 { "chr", "checkerboard right first", 0, AV_OPT_TYPE_CONST, {.i64=CHECKERBOARD_RL}, 0, 0, FLAGS, "out" },
205 { "icl", "interleave columns left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_LR}, 0, 0, FLAGS, "out" },
206 { "icr", "interleave columns right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_COLS_RL}, 0, 0, FLAGS, "out" },
207 { "hdmi", "HDMI frame pack", 0, AV_OPT_TYPE_CONST, {.i64=HDMI}, 0, 0, FLAGS, "out" },
211 AVFILTER_DEFINE_CLASS(stereo3d);
213 static const enum AVPixelFormat anaglyph_pix_fmts[] = {
214 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
218 static const enum AVPixelFormat other_pix_fmts[] = {
219 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
220 AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
221 AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
222 AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
223 AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
224 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
225 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
226 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
227 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
229 AV_PIX_FMT_GBRP9BE, AV_PIX_FMT_GBRP9LE,
230 AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
231 AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
232 AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
233 AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
236 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
237 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
239 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
245 AV_PIX_FMT_YUV420P9LE, AV_PIX_FMT_YUVA420P9LE,
246 AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUVA420P9BE,
247 AV_PIX_FMT_YUV422P9LE, AV_PIX_FMT_YUVA422P9LE,
248 AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUVA422P9BE,
249 AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUVA444P9LE,
250 AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUVA444P9BE,
251 AV_PIX_FMT_YUV420P10LE, AV_PIX_FMT_YUVA420P10LE,
252 AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUVA420P10BE,
253 AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUVA422P10LE,
254 AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUVA422P10BE,
255 AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
256 AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
257 AV_PIX_FMT_YUV420P12BE, AV_PIX_FMT_YUV420P12LE,
258 AV_PIX_FMT_YUV422P12BE, AV_PIX_FMT_YUV422P12LE,
259 AV_PIX_FMT_YUV444P12BE, AV_PIX_FMT_YUV444P12LE,
260 AV_PIX_FMT_YUV420P14BE, AV_PIX_FMT_YUV420P14LE,
261 AV_PIX_FMT_YUV422P14BE, AV_PIX_FMT_YUV422P14LE,
262 AV_PIX_FMT_YUV444P14BE, AV_PIX_FMT_YUV444P14LE,
263 AV_PIX_FMT_YUV420P16LE, AV_PIX_FMT_YUVA420P16LE,
264 AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUVA420P16BE,
265 AV_PIX_FMT_YUV422P16LE, AV_PIX_FMT_YUVA422P16LE,
266 AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUVA422P16BE,
267 AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
268 AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
272 static int query_formats(AVFilterContext *ctx)
274 Stereo3DContext *s = ctx->priv;
275 const enum AVPixelFormat *pix_fmts;
276 AVFilterFormats *fmts_list;
278 switch (s->out.format) {
279 case ANAGLYPH_GM_COLOR:
280 case ANAGLYPH_GM_DUBOIS:
281 case ANAGLYPH_GM_GRAY:
282 case ANAGLYPH_GM_HALF:
283 case ANAGLYPH_RB_GRAY:
284 case ANAGLYPH_RC_COLOR:
285 case ANAGLYPH_RC_DUBOIS:
286 case ANAGLYPH_RC_GRAY:
287 case ANAGLYPH_RC_HALF:
288 case ANAGLYPH_RG_GRAY:
289 case ANAGLYPH_YB_COLOR:
290 case ANAGLYPH_YB_DUBOIS:
291 case ANAGLYPH_YB_GRAY:
292 case ANAGLYPH_YB_HALF:
293 pix_fmts = anaglyph_pix_fmts;
296 pix_fmts = other_pix_fmts;
299 fmts_list = ff_make_format_list(pix_fmts);
301 return AVERROR(ENOMEM);
302 return ff_set_common_formats(ctx, fmts_list);
305 static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
309 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
310 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
311 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
313 return av_clip_uint8(sum >> 16);
316 static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
317 ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
318 int width, int height,
319 const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
323 for (y = 0; y < height; y++) {
324 for (o = 0, x = 0; x < width; x++, o+= 3) {
325 dst[o ] = ana_convert(ana_matrix_r, lsrc + o, rsrc + o);
326 dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o, rsrc + o);
327 dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o, rsrc + o);
336 static int config_output(AVFilterLink *outlink)
338 AVFilterContext *ctx = outlink->src;
339 AVFilterLink *inlink = ctx->inputs[0];
340 Stereo3DContext *s = ctx->priv;
341 AVRational aspect = inlink->sample_aspect_ratio;
342 AVRational fps = inlink->frame_rate;
343 AVRational tb = inlink->time_base;
344 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
347 switch (s->in.format) {
348 case SIDE_BY_SIDE_2_LR:
349 case SIDE_BY_SIDE_LR:
350 case SIDE_BY_SIDE_2_RL:
351 case SIDE_BY_SIDE_RL:
353 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
354 return AVERROR_INVALIDDATA;
357 case INTERLEAVE_ROWS_LR:
358 case INTERLEAVE_ROWS_RL:
359 case ABOVE_BELOW_2_LR:
361 case ABOVE_BELOW_2_RL:
364 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
365 return AVERROR_INVALIDDATA;
371 s->width = inlink->w;
373 s->height = inlink->h;
382 switch (s->in.format) {
383 case SIDE_BY_SIDE_2_LR:
385 case SIDE_BY_SIDE_LR:
386 s->width = inlink->w / 2;
387 s->in.off_right = s->width;
389 case SIDE_BY_SIDE_2_RL:
391 case SIDE_BY_SIDE_RL:
392 s->width = inlink->w / 2;
393 s->in.off_left = s->width;
395 case ABOVE_BELOW_2_LR:
399 s->height = inlink->h / 2;
401 case ABOVE_BELOW_2_RL:
405 s->height = inlink->h / 2;
412 case INTERLEAVE_ROWS_LR:
413 case INTERLEAVE_ROWS_RL:
415 if (s->in.format == INTERLEAVE_ROWS_RL)
419 if (s->out.format != CHECKERBOARD_LR &&
420 s->out.format != CHECKERBOARD_RL)
421 s->height = inlink->h / 2;
424 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
425 return AVERROR(EINVAL);
428 s->out.width = s->width;
429 s->out.height = s->height;
435 s->out.row_right = 0;
438 switch (s->out.format) {
439 case ANAGLYPH_RB_GRAY:
440 case ANAGLYPH_RG_GRAY:
441 case ANAGLYPH_RC_GRAY:
442 case ANAGLYPH_RC_HALF:
443 case ANAGLYPH_RC_COLOR:
444 case ANAGLYPH_RC_DUBOIS:
445 case ANAGLYPH_GM_GRAY:
446 case ANAGLYPH_GM_HALF:
447 case ANAGLYPH_GM_COLOR:
448 case ANAGLYPH_GM_DUBOIS:
449 case ANAGLYPH_YB_GRAY:
450 case ANAGLYPH_YB_HALF:
451 case ANAGLYPH_YB_COLOR:
452 case ANAGLYPH_YB_DUBOIS: {
455 ff_fill_rgba_map(rgba_map, outlink->format);
456 s->ana_matrix[rgba_map[0]] = &ana_coeff[s->out.format][0][0];
457 s->ana_matrix[rgba_map[1]] = &ana_coeff[s->out.format][1][0];
458 s->ana_matrix[rgba_map[2]] = &ana_coeff[s->out.format][2][0];
461 case SIDE_BY_SIDE_2_LR:
463 case SIDE_BY_SIDE_LR:
464 s->out.width = s->width * 2;
465 s->out.off_right = s->width;
467 case SIDE_BY_SIDE_2_RL:
469 case SIDE_BY_SIDE_RL:
470 s->out.width = s->width * 2;
471 s->out.off_left = s->width;
473 case ABOVE_BELOW_2_LR:
476 s->out.height = s->height * 2;
477 s->out.row_right = s->height;
480 if (s->height != 720 && s->height != 1080) {
481 av_log(ctx, AV_LOG_ERROR, "Only 720 and 1080 height supported\n");
482 return AVERROR(EINVAL);
485 s->blanks = s->height / 24;
486 s->out.height = s->height * 2 + s->blanks;
487 s->out.row_right = s->height + s->blanks;
489 case ABOVE_BELOW_2_RL:
492 s->out.height = s->height * 2;
493 s->out.row_left = s->height;
495 case INTERLEAVE_ROWS_LR:
496 s->in.row_step = 1 + (s->in.format == INTERLEAVE_ROWS_RL);
498 s->out.height = s->height * 2;
499 s->out.off_rstep = 1;
500 s->in.off_rstep = s->in.format != INTERLEAVE_ROWS_RL;
502 case INTERLEAVE_ROWS_RL:
503 s->in.row_step = 1 + (s->in.format == INTERLEAVE_ROWS_LR);
505 s->out.height = s->height * 2;
506 s->out.off_lstep = 1;
507 s->in.off_lstep = s->in.format != INTERLEAVE_ROWS_LR;
510 s->in.off_left = s->in.off_right;
511 s->in.row_left = s->in.row_right;
512 if (s->in.format == INTERLEAVE_ROWS_LR)
513 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
516 if (s->in.format == INTERLEAVE_ROWS_RL)
517 FFSWAP(int, s->in.off_lstep, s->in.off_rstep);
524 case CHECKERBOARD_LR:
525 case CHECKERBOARD_RL:
526 s->out.width = s->width * 2;
528 case INTERLEAVE_COLS_LR:
529 case INTERLEAVE_COLS_RL:
530 s->out.width = s->width * 2;
533 av_log(ctx, AV_LOG_ERROR, "output format %d is not supported\n", s->out.format);
534 return AVERROR(EINVAL);
537 outlink->w = s->out.width;
538 outlink->h = s->out.height;
539 outlink->frame_rate = fps;
540 outlink->time_base = tb;
541 outlink->sample_aspect_ratio = aspect;
543 if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
545 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
546 av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
547 s->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
548 s->pheight[1] = s->pheight[2] = FF_CEIL_RSHIFT(s->height, desc->log2_chroma_h);
549 s->pheight[0] = s->pheight[3] = s->height;
550 s->hsub = desc->log2_chroma_w;
551 s->vsub = desc->log2_chroma_h;
553 s->dsp.anaglyph = anaglyph;
555 ff_stereo3d_init_x86(&s->dsp);
560 typedef struct ThreadData {
561 AVFrame *ileft, *iright;
565 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
567 Stereo3DContext *s = ctx->priv;
568 ThreadData *td = arg;
569 AVFrame *ileft = td->ileft;
570 AVFrame *iright = td->iright;
571 AVFrame *out = td->out;
572 int height = s->out.height;
573 int start = (height * jobnr ) / nb_jobs;
574 int end = (height * (jobnr+1)) / nb_jobs;
575 const int **ana_matrix = s->ana_matrix;
577 s->dsp.anaglyph(out->data[0] + out->linesize[0] * start,
578 ileft ->data[0] + s->in_off_left [0] + ileft->linesize[0] * start * s->in.row_step,
579 iright->data[0] + s->in_off_right[0] + iright->linesize[0] * start * s->in.row_step,
581 ileft->linesize[0] * s->in.row_step,
582 iright->linesize[0] * s->in.row_step,
583 s->out.width, end - start,
584 ana_matrix[0], ana_matrix[1], ana_matrix[2]);
589 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
591 AVFilterContext *ctx = inlink->dst;
592 Stereo3DContext *s = ctx->priv;
593 AVFilterLink *outlink = ctx->outputs[0];
594 AVFrame *out, *oleft, *oright, *ileft, *iright;
595 int out_off_left[4], out_off_right[4];
598 if (s->in.format == s->out.format)
599 return ff_filter_frame(outlink, inpicref);
601 switch (s->in.format) {
610 if (s->in.format == ALTERNATING_RL)
611 FFSWAP(AVFrame *, ileft, iright);
614 ileft = iright = inpicref;
617 out = oleft = oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
619 av_frame_free(&s->prev);
620 av_frame_free(&inpicref);
621 return AVERROR(ENOMEM);
623 av_frame_copy_props(out, inpicref);
625 if (s->out.format == ALTERNATING_LR ||
626 s->out.format == ALTERNATING_RL) {
627 oright = ff_get_video_buffer(outlink, outlink->w, outlink->h);
629 av_frame_free(&oleft);
630 av_frame_free(&s->prev);
631 av_frame_free(&inpicref);
632 return AVERROR(ENOMEM);
634 av_frame_copy_props(oright, inpicref);
637 for (i = 0; i < 4; i++) {
638 int hsub = i == 1 || i == 2 ? s->hsub : 0;
639 int vsub = i == 1 || i == 2 ? s->vsub : 0;
640 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);
641 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);
642 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);
643 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);
646 switch (s->out.format) {
648 for (i = 0; i < s->nb_planes; i++) {
649 int j, h = s->height >> ((i == 1 || i == 2) ? s->vsub : 0);
650 int b = (s->blanks) >> ((i == 1 || i == 2) ? s->vsub : 0);
652 for (j = h; j < h + b; j++)
653 memset(oleft->data[i] + j * s->linesize[i], 0, s->linesize[i]);
657 case SIDE_BY_SIDE_LR:
658 case SIDE_BY_SIDE_RL:
659 case SIDE_BY_SIDE_2_LR:
660 case SIDE_BY_SIDE_2_RL:
663 case ABOVE_BELOW_2_LR:
664 case ABOVE_BELOW_2_RL:
665 case INTERLEAVE_ROWS_LR:
666 case INTERLEAVE_ROWS_RL:
667 for (i = 0; i < s->nb_planes; i++) {
668 av_image_copy_plane(oleft->data[i] + out_off_left[i],
669 oleft->linesize[i] * s->out.row_step,
670 ileft->data[i] + s->in_off_left[i],
671 ileft->linesize[i] * s->in.row_step,
672 s->linesize[i], s->pheight[i]);
673 av_image_copy_plane(oright->data[i] + out_off_right[i],
674 oright->linesize[i] * s->out.row_step,
675 iright->data[i] + s->in_off_right[i],
676 iright->linesize[i] * s->in.row_step,
677 s->linesize[i], s->pheight[i]);
683 for (i = 0; i < s->nb_planes; i++) {
684 av_image_copy_plane(out->data[i], out->linesize[i],
685 iright->data[i] + s->in_off_left[i],
686 iright->linesize[i] * s->in.row_step,
687 s->linesize[i], s->pheight[i]);
690 case ANAGLYPH_RB_GRAY:
691 case ANAGLYPH_RG_GRAY:
692 case ANAGLYPH_RC_GRAY:
693 case ANAGLYPH_RC_HALF:
694 case ANAGLYPH_RC_COLOR:
695 case ANAGLYPH_RC_DUBOIS:
696 case ANAGLYPH_GM_GRAY:
697 case ANAGLYPH_GM_HALF:
698 case ANAGLYPH_GM_COLOR:
699 case ANAGLYPH_GM_DUBOIS:
700 case ANAGLYPH_YB_GRAY:
701 case ANAGLYPH_YB_HALF:
702 case ANAGLYPH_YB_COLOR:
703 case ANAGLYPH_YB_DUBOIS: {
706 td.ileft = ileft; td.iright = iright; td.out = out;
707 ctx->internal->execute(ctx, filter_slice, &td, NULL,
708 FFMIN(s->out.height, ctx->graph->nb_threads));
711 case CHECKERBOARD_RL:
712 case CHECKERBOARD_LR:
713 for (i = 0; i < s->nb_planes; i++) {
716 for (y = 0; y < s->pheight[i]; y++) {
717 uint8_t *dst = out->data[i] + out->linesize[i] * y;
718 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y + s->in_off_left[i];
719 uint8_t *right = iright->data[i] + iright->linesize[i] * y + s->in_off_right[i];
722 if (s->out.format == CHECKERBOARD_RL)
723 FFSWAP(uint8_t*, left, right);
724 switch (s->pixstep[i]) {
726 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
727 dst[x ] = (b&1) == (y&1) ? left[p] : right[p];
728 dst[x+1] = (b&1) != (y&1) ? left[p] : right[p];
732 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
733 AV_WN16(&dst[x ], (b&1) == (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
734 AV_WN16(&dst[x+2], (b&1) != (y&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
738 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
739 AV_WB24(&dst[x ], (b&1) == (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
740 AV_WB24(&dst[x+3], (b&1) != (y&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
744 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
745 AV_WN32(&dst[x ], (b&1) == (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
746 AV_WN32(&dst[x+4], (b&1) != (y&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
750 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
751 AV_WB48(&dst[x ], (b&1) == (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
752 AV_WB48(&dst[x+6], (b&1) != (y&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
756 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
757 AV_WN64(&dst[x ], (b&1) == (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
758 AV_WN64(&dst[x+8], (b&1) != (y&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
765 case INTERLEAVE_COLS_LR:
766 case INTERLEAVE_COLS_RL:
767 for (i = 0; i < s->nb_planes; i++) {
770 for (y = 0; y < s->pheight[i]; y++) {
771 uint8_t *dst = out->data[i] + out->linesize[i] * y;
772 uint8_t *left = ileft->data[i] + ileft->linesize[i] * y * s->in.row_step + s->in_off_left[i];
773 uint8_t *right = iright->data[i] + iright->linesize[i] * y * s->in.row_step + s->in_off_right[i];
776 if (s->out.format == INTERLEAVE_COLS_LR)
777 FFSWAP(uint8_t*, left, right);
779 switch (s->pixstep[i]) {
781 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=2, p++, b++) {
782 dst[x ] = b&1 ? left[p] : right[p];
783 dst[x+1] = !(b&1) ? left[p] : right[p];
787 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=4, p+=2, b++) {
788 AV_WN16(&dst[x ], b&1 ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
789 AV_WN16(&dst[x+2], !(b&1) ? AV_RN16(&left[p]) : AV_RN16(&right[p]));
793 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=6, p+=3, b++) {
794 AV_WB24(&dst[x ], b&1 ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
795 AV_WB24(&dst[x+3], !(b&1) ? AV_RB24(&left[p]) : AV_RB24(&right[p]));
799 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=8, p+=4, b++) {
800 AV_WN32(&dst[x ], b&1 ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
801 AV_WN32(&dst[x+4], !(b&1) ? AV_RN32(&left[p]) : AV_RN32(&right[p]));
805 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=12, p+=6, b++) {
806 AV_WB48(&dst[x ], b&1 ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
807 AV_WB48(&dst[x+6], !(b&1) ? AV_RB48(&left[p]) : AV_RB48(&right[p]));
811 for (x = 0, b = 0, p = 0; x < s->linesize[i] * 2; x+=16, p+=8, b++) {
812 AV_WN64(&dst[x ], b&1 ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
813 AV_WN64(&dst[x+8], !(b&1) ? AV_RN64(&left[p]) : AV_RN64(&right[p]));
824 av_frame_free(&inpicref);
825 av_frame_free(&s->prev);
826 if (oright != oleft) {
827 if (s->out.format == ALTERNATING_LR)
828 FFSWAP(AVFrame *, oleft, oright);
829 oright->pts = outlink->frame_count * s->ts_unit;
830 ff_filter_frame(outlink, oright);
832 oleft->pts = outlink->frame_count * s->ts_unit;
833 } else if (s->in.format == ALTERNATING_LR ||
834 s->in.format == ALTERNATING_RL) {
835 out->pts = outlink->frame_count * s->ts_unit;
837 return ff_filter_frame(outlink, out);
840 static av_cold void uninit(AVFilterContext *ctx)
842 Stereo3DContext *s = ctx->priv;
844 av_frame_free(&s->prev);
847 static const AVFilterPad stereo3d_inputs[] = {
850 .type = AVMEDIA_TYPE_VIDEO,
851 .filter_frame = filter_frame,
856 static const AVFilterPad stereo3d_outputs[] = {
859 .type = AVMEDIA_TYPE_VIDEO,
860 .config_props = config_output,
865 AVFilter ff_vf_stereo3d = {
867 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
868 .priv_size = sizeof(Stereo3DContext),
870 .query_formats = query_formats,
871 .inputs = stereo3d_inputs,
872 .outputs = stereo3d_outputs,
873 .priv_class = &stereo3d_class,
874 .flags = AVFILTER_FLAG_SLICE_THREADS,