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;
200 s->width = inlink->w;
202 s->height = inlink->h;
209 switch (s->in.format) {
210 case SIDE_BY_SIDE_2_LR:
212 case SIDE_BY_SIDE_LR:
213 s->width = inlink->w / 2;
214 s->in.off_right = s->width * 3;
216 case SIDE_BY_SIDE_2_RL:
218 case SIDE_BY_SIDE_RL:
219 s->width = inlink->w / 2;
220 s->in.off_left = s->width * 3;
222 case ABOVE_BELOW_2_LR:
226 s->height = inlink->h / 2;
228 case ABOVE_BELOW_2_RL:
232 s->height = inlink->h / 2;
235 av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
236 return AVERROR(EINVAL);
239 s->out.width = s->width;
240 s->out.height = s->height;
244 s->out.row_right = 0;
246 switch (s->out.format) {
247 case ANAGLYPH_RB_GRAY:
248 case ANAGLYPH_RG_GRAY:
249 case ANAGLYPH_RC_GRAY:
250 case ANAGLYPH_RC_HALF:
251 case ANAGLYPH_RC_COLOR:
252 case ANAGLYPH_RC_DUBOIS:
253 case ANAGLYPH_GM_GRAY:
254 case ANAGLYPH_GM_HALF:
255 case ANAGLYPH_GM_COLOR:
256 case ANAGLYPH_GM_DUBOIS:
257 case ANAGLYPH_YB_GRAY:
258 case ANAGLYPH_YB_HALF:
259 case ANAGLYPH_YB_COLOR:
260 case ANAGLYPH_YB_DUBOIS:
261 memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
263 case SIDE_BY_SIDE_2_LR:
265 case SIDE_BY_SIDE_LR:
267 s->out.off_right = s->width * 3;
269 case SIDE_BY_SIDE_2_RL:
271 case SIDE_BY_SIDE_RL:
272 s->out.width = s->width * 2;
273 s->out.off_left = s->width * 3;
275 case ABOVE_BELOW_2_LR:
278 s->out.height = s->height * 2;
279 s->out.row_right = s->height;
281 case ABOVE_BELOW_2_RL:
284 s->out.height = s->height * 2;
285 s->out.row_left = s->height;
287 case INTERLEAVE_ROWS_LR:
289 s->height = s->height / 2;
290 s->out.off_right = s->width * 3;
291 s->in.off_right += s->in.width * 3;
293 case INTERLEAVE_ROWS_RL:
295 s->height = s->height / 2;
296 s->out.off_left = s->width * 3;
297 s->in.off_left += s->in.width * 3;
300 s->in.off_left = s->in.off_right;
301 s->in.row_left = s->in.row_right;
305 av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
306 return AVERROR(EINVAL);
309 outlink->w = s->out.width;
310 outlink->h = s->out.height;
311 outlink->sample_aspect_ratio = aspect;
316 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
320 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
321 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
322 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
324 return av_clip_uint8(sum >> 16);
327 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
329 AVFilterContext *ctx = inlink->dst;
330 Stereo3DContext *s = ctx->priv;
331 AVFilterLink *outlink = ctx->outputs[0];
333 int out_off_left, out_off_right;
334 int in_off_left, in_off_right;
336 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
338 av_frame_free(&inpicref);
339 return AVERROR(ENOMEM);
341 av_frame_copy_props(out, inpicref);
343 in_off_left = s->in.row_left * inpicref->linesize[0] + s->in.off_left;
344 in_off_right = s->in.row_right * inpicref->linesize[0] + s->in.off_right;
345 out_off_left = s->out.row_left * out->linesize[0] + s->out.off_left;
346 out_off_right = s->out.row_right * out->linesize[0] + s->out.off_right;
348 switch (s->out.format) {
349 case SIDE_BY_SIDE_LR:
350 case SIDE_BY_SIDE_RL:
351 case SIDE_BY_SIDE_2_LR:
352 case SIDE_BY_SIDE_2_RL:
355 case ABOVE_BELOW_2_LR:
356 case ABOVE_BELOW_2_RL:
357 case INTERLEAVE_ROWS_LR:
358 case INTERLEAVE_ROWS_RL:
359 av_image_copy_plane(out->data[0] + out_off_left,
360 out->linesize[0] * s->row_step,
361 inpicref->data[0] + in_off_left,
362 inpicref->linesize[0] * s->row_step,
363 3 * s->width, s->height);
364 av_image_copy_plane(out->data[0] + out_off_right,
365 out->linesize[0] * s->row_step,
366 inpicref->data[0] + in_off_right,
367 inpicref->linesize[0] * s->row_step,
368 3 * s->width, s->height);
372 av_image_copy_plane(out->data[0], out->linesize[0],
373 inpicref->data[0] + in_off_left,
374 inpicref->linesize[0],
375 3 * s->width, s->height);
377 case ANAGLYPH_RB_GRAY:
378 case ANAGLYPH_RG_GRAY:
379 case ANAGLYPH_RC_GRAY:
380 case ANAGLYPH_RC_HALF:
381 case ANAGLYPH_RC_COLOR:
382 case ANAGLYPH_RC_DUBOIS:
383 case ANAGLYPH_GM_GRAY:
384 case ANAGLYPH_GM_HALF:
385 case ANAGLYPH_GM_COLOR:
386 case ANAGLYPH_GM_DUBOIS:
387 case ANAGLYPH_YB_GRAY:
388 case ANAGLYPH_YB_HALF:
389 case ANAGLYPH_YB_COLOR:
390 case ANAGLYPH_YB_DUBOIS: {
391 int i, x, y, il, ir, o;
392 uint8_t *src = inpicref->data[0];
393 uint8_t *dst = out->data[0];
394 int out_width = s->out.width;
397 for (i = 0; i < 3; i++)
398 ana_matrix[i] = s->ana_matrix[i];
400 for (y = 0; y < s->out.height; y++) {
401 o = out->linesize[0] * y;
402 il = in_off_left + y * inpicref->linesize[0];
403 ir = in_off_right + y * inpicref->linesize[0];
404 for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
405 dst[o ] = ana_convert(ana_matrix[0], src + il, src + ir);
406 dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
407 dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
416 av_frame_free(&inpicref);
417 return ff_filter_frame(outlink, out);
420 static const AVFilterPad stereo3d_inputs[] = {
423 .type = AVMEDIA_TYPE_VIDEO,
424 .filter_frame = filter_frame,
429 static const AVFilterPad stereo3d_outputs[] = {
432 .type = AVMEDIA_TYPE_VIDEO,
433 .config_props = config_output,
438 AVFilter avfilter_vf_stereo3d = {
440 .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
441 .priv_size = sizeof(Stereo3DContext),
442 .query_formats = query_formats,
443 .inputs = stereo3d_inputs,
444 .outputs = stereo3d_outputs,
445 .priv_class = &stereo3d_class,