2 * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/pixdesc.h"
33 ANAGLYPH_RC_GRAY, // anaglyph red/cyan gray
34 ANAGLYPH_RC_HALF, // anaglyph red/cyan half colored
35 ANAGLYPH_RC_COLOR, // anaglyph red/cyan colored
36 ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
37 ANAGLYPH_GM_GRAY, // anaglyph green/magenta gray
38 ANAGLYPH_GM_HALF, // anaglyph green/magenta half colored
39 ANAGLYPH_GM_COLOR, // anaglyph green/magenta colored
40 ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
41 ANAGLYPH_YB_GRAY, // anaglyph yellow/blue gray
42 ANAGLYPH_YB_HALF, // anaglyph yellow/blue half colored
43 ANAGLYPH_YB_COLOR, // anaglyph yellow/blue colored
44 ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
45 ANAGLYPH_RB_GRAY, // anaglyph red/blue gray
46 ANAGLYPH_RG_GRAY, // anaglyph red/green gray
47 MONO_L, // mono output for debugging (left eye only)
48 MONO_R, // mono output for debugging (right eye only)
49 INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
50 INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
51 SIDE_BY_SIDE_LR, // side by side parallel (left eye left, right eye right)
52 SIDE_BY_SIDE_RL, // side by side crosseye (right eye left, left eye right)
53 SIDE_BY_SIDE_2_LR, // side by side parallel with half width resolution
54 SIDE_BY_SIDE_2_RL, // side by side crosseye with half width resolution
55 ABOVE_BELOW_LR, // above-below (left eye above, right eye below)
56 ABOVE_BELOW_RL, // above-below (right eye above, left eye below)
57 ABOVE_BELOW_2_LR, // above-below with half height resolution
58 ABOVE_BELOW_2_RL, // above-below with half height resolution
59 STEREO_CODE_COUNT // TODO: needs autodetection
62 typedef struct StereoComponent {
63 enum StereoCode format;
65 int off_left, off_right;
66 int off_lstep, off_rstep;
67 int row_left, row_right;
70 static const int ana_coeff[][3][6] = {
72 {{19595, 38470, 7471, 0, 0, 0},
74 { 0, 0, 0, 19595, 38470, 7471}},
76 {{19595, 38470, 7471, 0, 0, 0},
77 { 0, 0, 0, 19595, 38470, 7471},
80 {{19595, 38470, 7471, 0, 0, 0},
81 { 0, 0, 0, 19595, 38470, 7471},
82 { 0, 0, 0, 19595, 38470, 7471}},
84 {{19595, 38470, 7471, 0, 0, 0},
85 { 0, 0, 0, 0, 65536, 0},
86 { 0, 0, 0, 0, 0, 65536}},
88 {{65536, 0, 0, 0, 0, 0},
89 { 0, 0, 0, 0, 65536, 0},
90 { 0, 0, 0, 0, 0, 65536}},
91 [ANAGLYPH_RC_DUBOIS] =
92 {{29891, 32800, 11559, -2849, -5763, -102},
93 {-2627, -2479, -1033, 24804, 48080, -1209},
94 { -997, -1350, -358, -4729, -7403, 80373}},
96 {{ 0, 0, 0, 19595, 38470, 7471},
97 {19595, 38470, 7471, 0, 0, 0},
98 { 0, 0, 0, 19595, 38470, 7471}},
100 {{ 0, 0, 0, 65536, 0, 0},
101 {19595, 38470, 7471, 0, 0, 0},
102 { 0, 0, 0, 0, 0, 65536}},
103 [ANAGLYPH_GM_COLOR] =
104 {{ 0, 0, 0, 65536, 0, 0},
105 { 0, 65536, 0, 0, 0, 0},
106 { 0, 0, 0, 0, 0, 65536}},
107 [ANAGLYPH_GM_DUBOIS] =
108 {{-4063,-10354, -2556, 34669, 46203, 1573},
109 {18612, 43778, 9372, -1049, -983, -4260},
110 { -983, -1769, 1376, 590, 4915, 61407}},
112 {{ 0, 0, 0, 19595, 38470, 7471},
113 { 0, 0, 0, 19595, 38470, 7471},
114 {19595, 38470, 7471, 0, 0, 0}},
116 {{ 0, 0, 0, 65536, 0, 0},
117 { 0, 0, 0, 0, 65536, 0},
118 {19595, 38470, 7471, 0, 0, 0}},
119 [ANAGLYPH_YB_COLOR] =
120 {{ 0, 0, 0, 65536, 0, 0},
121 { 0, 0, 0, 0, 65536, 0},
122 { 0, 0, 65536, 0, 0, 0}},
123 [ANAGLYPH_YB_DUBOIS] =
124 {{65535,-12650,18451, -987, -7590, -1049},
125 {-1604, 56032, 4196, 370, 3826, -1049},
126 {-2345,-10676, 1358, 5801, 11416, 56217}},
129 typedef struct Stereo3DContext {
130 const AVClass *class;
131 StereoComponent in, out;
134 int ana_matrix[3][6];
137 #define OFFSET(x) offsetof(Stereo3DContext, x)
138 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
140 static const AVOption stereo3d_options[] = {
141 { "in", "set input format", OFFSET(in.format), AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, ABOVE_BELOW_2_RL, FLAGS, "in"},
142 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
143 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
144 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
145 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
146 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
147 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
148 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
149 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
150 { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
151 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
152 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
153 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
154 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
155 { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
156 { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
157 { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
158 { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
159 { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
160 { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
161 { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
162 { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
163 { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
164 { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
165 { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
166 { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
167 { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
168 { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
169 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
170 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
171 { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
172 { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
173 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
174 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
175 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
176 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
180 AVFILTER_DEFINE_CLASS(stereo3d);
182 static int query_formats(AVFilterContext *ctx)
184 static const enum AVPixelFormat pix_fmts[] = {
185 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
188 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
193 static int config_output(AVFilterLink *outlink)
195 AVFilterContext *ctx = outlink->src;
196 AVFilterLink *inlink = ctx->inputs[0];
197 Stereo3DContext *s = ctx->priv;
198 AVRational aspect = inlink->sample_aspect_ratio;
200 switch (s->in.format) {
201 case SIDE_BY_SIDE_2_LR:
202 case SIDE_BY_SIDE_LR:
203 case SIDE_BY_SIDE_2_RL:
204 case SIDE_BY_SIDE_RL:
206 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
207 return AVERROR_INVALIDDATA;
210 case ABOVE_BELOW_2_LR:
212 case ABOVE_BELOW_2_RL:
214 if (s->out.format == INTERLEAVE_ROWS_LR ||
215 s->out.format == INTERLEAVE_ROWS_RL) {
217 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
218 return AVERROR_INVALIDDATA;
222 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
223 return AVERROR_INVALIDDATA;
229 s->width = inlink->w;
231 s->height = inlink->h;
240 switch (s->in.format) {
241 case SIDE_BY_SIDE_2_LR:
243 case SIDE_BY_SIDE_LR:
244 s->width = inlink->w / 2;
245 s->in.off_right = s->width * 3;
247 case SIDE_BY_SIDE_2_RL:
249 case SIDE_BY_SIDE_RL:
250 s->width = inlink->w / 2;
251 s->in.off_left = s->width * 3;
253 case ABOVE_BELOW_2_LR:
257 s->height = inlink->h / 2;
259 case ABOVE_BELOW_2_RL:
263 s->height = inlink->h / 2;
266 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
267 return AVERROR(EINVAL);
270 s->out.width = s->width;
271 s->out.height = s->height;
277 s->out.row_right = 0;
279 switch (s->out.format) {
280 case ANAGLYPH_RB_GRAY:
281 case ANAGLYPH_RG_GRAY:
282 case ANAGLYPH_RC_GRAY:
283 case ANAGLYPH_RC_HALF:
284 case ANAGLYPH_RC_COLOR:
285 case ANAGLYPH_RC_DUBOIS:
286 case ANAGLYPH_GM_GRAY:
287 case ANAGLYPH_GM_HALF:
288 case ANAGLYPH_GM_COLOR:
289 case ANAGLYPH_GM_DUBOIS:
290 case ANAGLYPH_YB_GRAY:
291 case ANAGLYPH_YB_HALF:
292 case ANAGLYPH_YB_COLOR:
293 case ANAGLYPH_YB_DUBOIS:
294 memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
296 case SIDE_BY_SIDE_2_LR:
298 case SIDE_BY_SIDE_LR:
299 s->out.width = s->width * 2;
300 s->out.off_right = s->width * 3;
302 case SIDE_BY_SIDE_2_RL:
304 case SIDE_BY_SIDE_RL:
305 s->out.width = s->width * 2;
306 s->out.off_left = s->width * 3;
308 case ABOVE_BELOW_2_LR:
311 s->out.height = s->height * 2;
312 s->out.row_right = s->height;
314 case ABOVE_BELOW_2_RL:
317 s->out.height = s->height * 2;
318 s->out.row_left = s->height;
320 case INTERLEAVE_ROWS_LR:
322 s->height = s->height / 2;
326 case INTERLEAVE_ROWS_RL:
328 s->height = s->height / 2;
333 s->in.off_left = s->in.off_right;
334 s->in.row_left = s->in.row_right;
338 av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
339 return AVERROR(EINVAL);
342 outlink->w = s->out.width;
343 outlink->h = s->out.height;
344 outlink->sample_aspect_ratio = aspect;
349 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
353 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
354 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
355 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
357 return av_clip_uint8(sum >> 16);
360 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
362 AVFilterContext *ctx = inlink->dst;
363 Stereo3DContext *s = ctx->priv;
364 AVFilterLink *outlink = ctx->outputs[0];
366 int out_off_left, out_off_right;
367 int in_off_left, in_off_right;
369 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
371 av_frame_free(&inpicref);
372 return AVERROR(ENOMEM);
374 av_frame_copy_props(out, inpicref);
376 in_off_left = (s->in.row_left + s->in.off_lstep) * inpicref->linesize[0] + s->in.off_left;
377 in_off_right = (s->in.row_right + s->in.off_rstep) * inpicref->linesize[0] + s->in.off_right;
378 out_off_left = (s->out.row_left + s->out.off_lstep) * out->linesize[0] + s->out.off_left;
379 out_off_right = (s->out.row_right + s->out.off_rstep) * out->linesize[0] + s->out.off_right;
381 switch (s->out.format) {
382 case SIDE_BY_SIDE_LR:
383 case SIDE_BY_SIDE_RL:
384 case SIDE_BY_SIDE_2_LR:
385 case SIDE_BY_SIDE_2_RL:
388 case ABOVE_BELOW_2_LR:
389 case ABOVE_BELOW_2_RL:
390 case INTERLEAVE_ROWS_LR:
391 case INTERLEAVE_ROWS_RL:
392 av_image_copy_plane(out->data[0] + out_off_left,
393 out->linesize[0] * s->row_step,
394 inpicref->data[0] + in_off_left,
395 inpicref->linesize[0] * s->row_step,
396 3 * s->width, s->height);
397 av_image_copy_plane(out->data[0] + out_off_right,
398 out->linesize[0] * s->row_step,
399 inpicref->data[0] + in_off_right,
400 inpicref->linesize[0] * s->row_step,
401 3 * s->width, s->height);
405 av_image_copy_plane(out->data[0], out->linesize[0],
406 inpicref->data[0] + in_off_left,
407 inpicref->linesize[0],
408 3 * s->width, s->height);
410 case ANAGLYPH_RB_GRAY:
411 case ANAGLYPH_RG_GRAY:
412 case ANAGLYPH_RC_GRAY:
413 case ANAGLYPH_RC_HALF:
414 case ANAGLYPH_RC_COLOR:
415 case ANAGLYPH_RC_DUBOIS:
416 case ANAGLYPH_GM_GRAY:
417 case ANAGLYPH_GM_HALF:
418 case ANAGLYPH_GM_COLOR:
419 case ANAGLYPH_GM_DUBOIS:
420 case ANAGLYPH_YB_GRAY:
421 case ANAGLYPH_YB_HALF:
422 case ANAGLYPH_YB_COLOR:
423 case ANAGLYPH_YB_DUBOIS: {
424 int i, x, y, il, ir, o;
425 uint8_t *src = inpicref->data[0];
426 uint8_t *dst = out->data[0];
427 int out_width = s->out.width;
430 for (i = 0; i < 3; i++)
431 ana_matrix[i] = s->ana_matrix[i];
433 for (y = 0; y < s->out.height; y++) {
434 o = out->linesize[0] * y;
435 il = in_off_left + y * inpicref->linesize[0];
436 ir = in_off_right + y * inpicref->linesize[0];
437 for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
438 dst[o ] = ana_convert(ana_matrix[0], src + il, src + ir);
439 dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
440 dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
449 av_frame_free(&inpicref);
450 return ff_filter_frame(outlink, out);
453 static const AVFilterPad stereo3d_inputs[] = {
456 .type = AVMEDIA_TYPE_VIDEO,
457 .filter_frame = filter_frame,
462 static const AVFilterPad stereo3d_outputs[] = {
465 .type = AVMEDIA_TYPE_VIDEO,
466 .config_props = config_output,
471 AVFilter avfilter_vf_stereo3d = {
473 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
474 .priv_size = sizeof(Stereo3DContext),
475 .query_formats = query_formats,
476 .inputs = stereo3d_inputs,
477 .outputs = stereo3d_outputs,
478 .priv_class = &stereo3d_class,