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 row_left, row_right;
69 static const int ana_coeff[][3][6] = {
71 {{19595, 38470, 7471, 0, 0, 0},
73 { 0, 0, 0, 19595, 38470, 7471}},
75 {{19595, 38470, 7471, 0, 0, 0},
76 { 0, 0, 0, 19595, 38470, 7471},
79 {{19595, 38470, 7471, 0, 0, 0},
80 { 0, 0, 0, 19595, 38470, 7471},
81 { 0, 0, 0, 19595, 38470, 7471}},
83 {{19595, 38470, 7471, 0, 0, 0},
84 { 0, 0, 0, 0, 65536, 0},
85 { 0, 0, 0, 0, 0, 65536}},
87 {{65536, 0, 0, 0, 0, 0},
88 { 0, 0, 0, 0, 65536, 0},
89 { 0, 0, 0, 0, 0, 65536}},
90 [ANAGLYPH_RC_DUBOIS] =
91 {{29891, 32800, 11559, -2849, -5763, -102},
92 {-2627, -2479, -1033, 24804, 48080, -1209},
93 { -997, -1350, -358, -4729, -7403, 80373}},
95 {{ 0, 0, 0, 19595, 38470, 7471},
96 {19595, 38470, 7471, 0, 0, 0},
97 { 0, 0, 0, 19595, 38470, 7471}},
99 {{ 0, 0, 0, 65536, 0, 0},
100 {19595, 38470, 7471, 0, 0, 0},
101 { 0, 0, 0, 0, 0, 65536}},
102 [ANAGLYPH_GM_COLOR] =
103 {{ 0, 0, 0, 65536, 0, 0},
104 { 0, 65536, 0, 0, 0, 0},
105 { 0, 0, 0, 0, 0, 65536}},
106 [ANAGLYPH_GM_DUBOIS] =
107 {{-4063,-10354, -2556, 34669, 46203, 1573},
108 {18612, 43778, 9372, -1049, -983, -4260},
109 { -983, -1769, 1376, 590, 4915, 61407}},
111 {{ 0, 0, 0, 19595, 38470, 7471},
112 { 0, 0, 0, 19595, 38470, 7471},
113 {19595, 38470, 7471, 0, 0, 0}},
115 {{ 0, 0, 0, 65536, 0, 0},
116 { 0, 0, 0, 0, 65536, 0},
117 {19595, 38470, 7471, 0, 0, 0}},
118 [ANAGLYPH_YB_COLOR] =
119 {{ 0, 0, 0, 65536, 0, 0},
120 { 0, 0, 0, 0, 65536, 0},
121 { 0, 0, 65536, 0, 0, 0}},
122 [ANAGLYPH_YB_DUBOIS] =
123 {{65535,-12650,18451, -987, -7590, -1049},
124 {-1604, 56032, 4196, 370, 3826, -1049},
125 {-2345,-10676, 1358, 5801, 11416, 56217}},
128 typedef struct Stereo3DContext {
129 const AVClass *class;
130 StereoComponent in, out;
133 int ana_matrix[3][6];
136 #define OFFSET(x) offsetof(Stereo3DContext, x)
137 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
139 static const AVOption stereo3d_options[] = {
140 { "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"},
141 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "in" },
142 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "in" },
143 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "in" },
144 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "in" },
145 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
146 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
147 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "in" },
148 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "in" },
149 { "out", "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
150 { "ab2l", "above below half height left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR}, 0, 0, FLAGS, "out" },
151 { "ab2r", "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL}, 0, 0, FLAGS, "out" },
152 { "abl", "above below left first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR}, 0, 0, FLAGS, "out" },
153 { "abr", "above below right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL}, 0, 0, FLAGS, "out" },
154 { "agmc", "anaglyph green magenta color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR}, 0, 0, FLAGS, "out" },
155 { "agmd", "anaglyph green magenta dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
156 { "agmg", "anaglyph green magenta gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY}, 0, 0, FLAGS, "out" },
157 { "agmh", "anaglyph green magenta half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF}, 0, 0, FLAGS, "out" },
158 { "arbg", "anaglyph red blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY}, 0, 0, FLAGS, "out" },
159 { "arcc", "anaglyph red cyan color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR}, 0, 0, FLAGS, "out" },
160 { "arcd", "anaglyph red cyan dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
161 { "arcg", "anaglyph red cyan gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY}, 0, 0, FLAGS, "out" },
162 { "arch", "anaglyph red cyan half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF}, 0, 0, FLAGS, "out" },
163 { "argg", "anaglyph red green gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY}, 0, 0, FLAGS, "out" },
164 { "aybc", "anaglyph yellow blue color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR}, 0, 0, FLAGS, "out" },
165 { "aybd", "anaglyph yellow blue dubois", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
166 { "aybg", "anaglyph yellow blue gray", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY}, 0, 0, FLAGS, "out" },
167 { "aybh", "anaglyph yellow blue half color", 0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF}, 0, 0, FLAGS, "out" },
168 { "irl", "interleave rows left first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
169 { "irr", "interleave rows right first", 0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
170 { "ml", "mono left", 0, AV_OPT_TYPE_CONST, {.i64=MONO_L}, 0, 0, FLAGS, "out" },
171 { "mr", "mono right", 0, AV_OPT_TYPE_CONST, {.i64=MONO_R}, 0, 0, FLAGS, "out" },
172 { "sbs2l", "side by side half width left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "out" },
173 { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "out" },
174 { "sbsl", "side by side left first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR}, 0, 0, FLAGS, "out" },
175 { "sbsr", "side by side right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL}, 0, 0, FLAGS, "out" },
179 AVFILTER_DEFINE_CLASS(stereo3d);
181 static int query_formats(AVFilterContext *ctx)
183 static const enum AVPixelFormat pix_fmts[] = {
184 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
187 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
192 static int config_output(AVFilterLink *outlink)
194 AVFilterContext *ctx = outlink->src;
195 AVFilterLink *inlink = ctx->inputs[0];
196 Stereo3DContext *s = ctx->priv;
197 AVRational aspect = inlink->sample_aspect_ratio;
199 switch (s->in.format) {
200 case SIDE_BY_SIDE_2_LR:
201 case SIDE_BY_SIDE_LR:
202 case SIDE_BY_SIDE_2_RL:
203 case SIDE_BY_SIDE_RL:
205 av_log(ctx, AV_LOG_ERROR, "width must be even\n");
206 return AVERROR_INVALIDDATA;
209 case ABOVE_BELOW_2_LR:
211 case ABOVE_BELOW_2_RL:
213 if (s->out.format == INTERLEAVE_ROWS_LR ||
214 s->out.format == INTERLEAVE_ROWS_RL) {
216 av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
217 return AVERROR_INVALIDDATA;
221 av_log(ctx, AV_LOG_ERROR, "height must be even\n");
222 return AVERROR_INVALIDDATA;
228 s->width = inlink->w;
230 s->height = inlink->h;
237 switch (s->in.format) {
238 case SIDE_BY_SIDE_2_LR:
240 case SIDE_BY_SIDE_LR:
241 s->width = inlink->w / 2;
242 s->in.off_right = s->width * 3;
244 case SIDE_BY_SIDE_2_RL:
246 case SIDE_BY_SIDE_RL:
247 s->width = inlink->w / 2;
248 s->in.off_left = s->width * 3;
250 case ABOVE_BELOW_2_LR:
254 s->height = inlink->h / 2;
256 case ABOVE_BELOW_2_RL:
260 s->height = inlink->h / 2;
263 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
264 return AVERROR(EINVAL);
267 s->out.width = s->width;
268 s->out.height = s->height;
272 s->out.row_right = 0;
274 switch (s->out.format) {
275 case ANAGLYPH_RB_GRAY:
276 case ANAGLYPH_RG_GRAY:
277 case ANAGLYPH_RC_GRAY:
278 case ANAGLYPH_RC_HALF:
279 case ANAGLYPH_RC_COLOR:
280 case ANAGLYPH_RC_DUBOIS:
281 case ANAGLYPH_GM_GRAY:
282 case ANAGLYPH_GM_HALF:
283 case ANAGLYPH_GM_COLOR:
284 case ANAGLYPH_GM_DUBOIS:
285 case ANAGLYPH_YB_GRAY:
286 case ANAGLYPH_YB_HALF:
287 case ANAGLYPH_YB_COLOR:
288 case ANAGLYPH_YB_DUBOIS:
289 memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
291 case SIDE_BY_SIDE_2_LR:
293 case SIDE_BY_SIDE_LR:
294 s->out.width = s->width * 2;
295 s->out.off_right = s->width * 3;
297 case SIDE_BY_SIDE_2_RL:
299 case SIDE_BY_SIDE_RL:
300 s->out.width = s->width * 2;
301 s->out.off_left = s->width * 3;
303 case ABOVE_BELOW_2_LR:
306 s->out.height = s->height * 2;
307 s->out.row_right = s->height;
309 case ABOVE_BELOW_2_RL:
312 s->out.height = s->height * 2;
313 s->out.row_left = s->height;
315 case INTERLEAVE_ROWS_LR:
317 s->height = s->height / 2;
318 s->out.off_right = s->width * 3;
319 s->in.off_right += s->in.width * 3;
321 case INTERLEAVE_ROWS_RL:
323 s->height = s->height / 2;
324 s->out.off_left = s->width * 3;
325 s->in.off_left += s->in.width * 3;
328 s->in.off_left = s->in.off_right;
329 s->in.row_left = s->in.row_right;
333 av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
334 return AVERROR(EINVAL);
337 outlink->w = s->out.width;
338 outlink->h = s->out.height;
339 outlink->sample_aspect_ratio = aspect;
344 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
348 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
349 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
350 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
352 return av_clip_uint8(sum >> 16);
355 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
357 AVFilterContext *ctx = inlink->dst;
358 Stereo3DContext *s = ctx->priv;
359 AVFilterLink *outlink = ctx->outputs[0];
361 int out_off_left, out_off_right;
362 int in_off_left, in_off_right;
364 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
366 av_frame_free(&inpicref);
367 return AVERROR(ENOMEM);
369 av_frame_copy_props(out, inpicref);
371 in_off_left = s->in.row_left * inpicref->linesize[0] + s->in.off_left;
372 in_off_right = s->in.row_right * inpicref->linesize[0] + s->in.off_right;
373 out_off_left = s->out.row_left * out->linesize[0] + s->out.off_left;
374 out_off_right = s->out.row_right * out->linesize[0] + s->out.off_right;
376 switch (s->out.format) {
377 case SIDE_BY_SIDE_LR:
378 case SIDE_BY_SIDE_RL:
379 case SIDE_BY_SIDE_2_LR:
380 case SIDE_BY_SIDE_2_RL:
383 case ABOVE_BELOW_2_LR:
384 case ABOVE_BELOW_2_RL:
385 case INTERLEAVE_ROWS_LR:
386 case INTERLEAVE_ROWS_RL:
387 av_image_copy_plane(out->data[0] + out_off_left,
388 out->linesize[0] * s->row_step,
389 inpicref->data[0] + in_off_left,
390 inpicref->linesize[0] * s->row_step,
391 3 * s->width, s->height);
392 av_image_copy_plane(out->data[0] + out_off_right,
393 out->linesize[0] * s->row_step,
394 inpicref->data[0] + in_off_right,
395 inpicref->linesize[0] * s->row_step,
396 3 * s->width, s->height);
400 av_image_copy_plane(out->data[0], out->linesize[0],
401 inpicref->data[0] + in_off_left,
402 inpicref->linesize[0],
403 3 * s->width, s->height);
405 case ANAGLYPH_RB_GRAY:
406 case ANAGLYPH_RG_GRAY:
407 case ANAGLYPH_RC_GRAY:
408 case ANAGLYPH_RC_HALF:
409 case ANAGLYPH_RC_COLOR:
410 case ANAGLYPH_RC_DUBOIS:
411 case ANAGLYPH_GM_GRAY:
412 case ANAGLYPH_GM_HALF:
413 case ANAGLYPH_GM_COLOR:
414 case ANAGLYPH_GM_DUBOIS:
415 case ANAGLYPH_YB_GRAY:
416 case ANAGLYPH_YB_HALF:
417 case ANAGLYPH_YB_COLOR:
418 case ANAGLYPH_YB_DUBOIS: {
419 int i, x, y, il, ir, o;
420 uint8_t *src = inpicref->data[0];
421 uint8_t *dst = out->data[0];
422 int out_width = s->out.width;
425 for (i = 0; i < 3; i++)
426 ana_matrix[i] = s->ana_matrix[i];
428 for (y = 0; y < s->out.height; y++) {
429 o = out->linesize[0] * y;
430 il = in_off_left + y * inpicref->linesize[0];
431 ir = in_off_right + y * inpicref->linesize[0];
432 for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
433 dst[o ] = ana_convert(ana_matrix[0], src + il, src + ir);
434 dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
435 dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
444 av_frame_free(&inpicref);
445 return ff_filter_frame(outlink, out);
448 static const AVFilterPad stereo3d_inputs[] = {
451 .type = AVMEDIA_TYPE_VIDEO,
452 .filter_frame = filter_frame,
457 static const AVFilterPad stereo3d_outputs[] = {
460 .type = AVMEDIA_TYPE_VIDEO,
461 .config_props = config_output,
466 AVFilter avfilter_vf_stereo3d = {
468 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
469 .priv_size = sizeof(Stereo3DContext),
470 .query_formats = query_formats,
471 .inputs = stereo3d_inputs,
472 .outputs = stereo3d_outputs,
473 .priv_class = &stereo3d_class,