FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_stereo3d.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
3  * Copyright (c) 2013 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
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"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 
32 enum StereoCode {
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
60 };
61 
62 typedef struct StereoComponent {
64  int width, height;
68 
69 static const int ana_coeff[][3][6] = {
71  {{19595, 38470, 7471, 0, 0, 0},
72  { 0, 0, 0, 0, 0, 0},
73  { 0, 0, 0, 19595, 38470, 7471}},
75  {{19595, 38470, 7471, 0, 0, 0},
76  { 0, 0, 0, 19595, 38470, 7471},
77  { 0, 0, 0, 0, 0, 0}},
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}},
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}},
103  {{ 0, 0, 0, 65536, 0, 0},
104  { 0, 65536, 0, 0, 0, 0},
105  { 0, 0, 0, 0, 0, 65536}},
107  {{-4063,-10354, -2556, 34669, 46203, 1573},
108  {18612, 43778, 9372, -1049, -983, -4260},
109  { -983, -1769, 1376, 590, 4915, 61407}},
110  [ANAGLYPH_YB_GRAY] =
111  {{ 0, 0, 0, 19595, 38470, 7471},
112  { 0, 0, 0, 19595, 38470, 7471},
113  {19595, 38470, 7471, 0, 0, 0}},
114  [ANAGLYPH_YB_HALF] =
115  {{ 0, 0, 0, 65536, 0, 0},
116  { 0, 0, 0, 0, 65536, 0},
117  {19595, 38470, 7471, 0, 0, 0}},
119  {{ 0, 0, 0, 65536, 0, 0},
120  { 0, 0, 0, 0, 65536, 0},
121  { 0, 0, 65536, 0, 0, 0}},
123  {{65535,-12650,18451, -987, -7590, -1049},
124  {-1604, 56032, 4196, 370, 3826, -1049},
125  {-2345,-10676, 1358, 5801, 11416, 56217}},
126 };
127 
128 typedef struct Stereo3DContext {
129  const AVClass *class;
131  int width, height;
132  int row_step;
133  int ana_matrix[3][6];
135 
136 #define OFFSET(x) offsetof(Stereo3DContext, x)
137 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
138 
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" },
176  {NULL},
177 };
178 
179 AVFILTER_DEFINE_CLASS(stereo3d);
180 
181 static av_cold int init(AVFilterContext *ctx, const char *args)
182 {
183  Stereo3DContext *s = ctx->priv;
184  static const char *shorthand[] = { "in", "out", NULL };
185  int ret;
186 
187  s->class = &stereo3d_class;
189 
190  if ((ret = av_opt_set_from_string(s, args, shorthand, "=", ":")) < 0)
191  return ret;
192 
193  return 0;
194 }
195 
197 {
198  static const enum AVPixelFormat pix_fmts[] = {
200  };
201 
203 
204  return 0;
205 }
206 
207 static int config_output(AVFilterLink *outlink)
208 {
209  AVFilterContext *ctx = outlink->src;
210  AVFilterLink *inlink = ctx->inputs[0];
211  Stereo3DContext *s = ctx->priv;
212  AVRational aspect = inlink->sample_aspect_ratio;
213 
214  s->in.width =
215  s->width = inlink->w;
216  s->in.height =
217  s->height = inlink->h;
218  s->row_step = 1;
219  s->in.off_left =
220  s->in.off_right =
221  s->in.row_left =
222  s->in.row_right = 0;
223 
224  switch (s->in.format) {
225  case SIDE_BY_SIDE_2_LR:
226  aspect.num *= 2;
227  case SIDE_BY_SIDE_LR:
228  s->width = inlink->w / 2;
229  s->in.off_right = s->width * 3;
230  break;
231  case SIDE_BY_SIDE_2_RL:
232  aspect.num *= 2;
233  case SIDE_BY_SIDE_RL:
234  s->width = inlink->w / 2;
235  s->in.off_left = s->width * 3;
236  break;
237  case ABOVE_BELOW_2_LR:
238  aspect.den *= 2;
239  case ABOVE_BELOW_LR:
240  s->in.row_right =
241  s->height = inlink->h / 2;
242  break;
243  case ABOVE_BELOW_2_RL:
244  aspect.den *= 2;
245  case ABOVE_BELOW_RL:
246  s->in.row_left =
247  s->height = inlink->h / 2;
248  break;
249  default:
250  av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
251  return AVERROR(EINVAL);
252  }
253 
254  s->out.width = s->width;
255  s->out.height = s->height;
256  s->out.off_left =
257  s->out.off_right =
258  s->out.row_left =
259  s->out.row_right = 0;
260 
261  switch (s->out.format) {
262  case ANAGLYPH_RB_GRAY:
263  case ANAGLYPH_RG_GRAY:
264  case ANAGLYPH_RC_GRAY:
265  case ANAGLYPH_RC_HALF:
266  case ANAGLYPH_RC_COLOR:
267  case ANAGLYPH_RC_DUBOIS:
268  case ANAGLYPH_GM_GRAY:
269  case ANAGLYPH_GM_HALF:
270  case ANAGLYPH_GM_COLOR:
271  case ANAGLYPH_GM_DUBOIS:
272  case ANAGLYPH_YB_GRAY:
273  case ANAGLYPH_YB_HALF:
274  case ANAGLYPH_YB_COLOR:
275  case ANAGLYPH_YB_DUBOIS:
276  memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
277  break;
278  case SIDE_BY_SIDE_2_LR:
279  aspect.num /= 2;
280  case SIDE_BY_SIDE_LR:
281  s->out.width =
282  s->out.off_right = s->width * 3;
283  break;
284  case SIDE_BY_SIDE_2_RL:
285  aspect.num /= 2;
286  case SIDE_BY_SIDE_RL:
287  s->out.width = s->width * 2;
288  s->out.off_left = s->width * 3;
289  break;
290  case ABOVE_BELOW_2_LR:
291  aspect.den /= 2;
292  case ABOVE_BELOW_LR:
293  s->out.height = s->height * 2;
294  s->out.row_right = s->height;
295  break;
296  case ABOVE_BELOW_2_RL:
297  aspect.den /= 2;
298  case ABOVE_BELOW_RL:
299  s->out.height = s->height * 2;
300  s->out.row_left = s->height;
301  break;
302  case INTERLEAVE_ROWS_LR:
303  s->row_step = 2;
304  s->height = s->height / 2;
305  s->out.off_right = s->width * 3;
306  s->in.off_right += s->in.width * 3;
307  break;
308  case INTERLEAVE_ROWS_RL:
309  s->row_step = 2;
310  s->height = s->height / 2;
311  s->out.off_left = s->width * 3;
312  s->in.off_left += s->in.width * 3;
313  break;
314  case MONO_R:
315  s->in.off_left = s->in.off_right;
316  s->in.row_left = s->in.row_right;
317  case MONO_L:
318  break;
319  default:
320  av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
321  return AVERROR(EINVAL);
322  }
323 
324  outlink->w = s->out.width;
325  outlink->h = s->out.height;
326  outlink->sample_aspect_ratio = aspect;
327 
328  return 0;
329 }
330 
331 static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
332 {
333  int sum;
334 
335  sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
336  sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
337  sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
338 
339  return av_clip_uint8(sum >> 16);
340 }
341 
342 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
343 {
344  AVFilterContext *ctx = inlink->dst;
345  Stereo3DContext *s = ctx->priv;
346  AVFilterLink *outlink = ctx->outputs[0];
348  int out_off_left, out_off_right;
349  int in_off_left, in_off_right;
350  int ret;
351 
352  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
353  if (!out) {
354  avfilter_unref_bufferp(&inpicref);
355  return AVERROR(ENOMEM);
356  }
357 
358  out->pts = inpicref->pts;
359  out->pos = inpicref->pos;
360 
361  in_off_left = s->in.row_left * inpicref->linesize[0] + s->in.off_left;
362  in_off_right = s->in.row_right * inpicref->linesize[0] + s->in.off_right;
363  out_off_left = s->out.row_left * out->linesize[0] + s->out.off_left;
364  out_off_right = s->out.row_right * out->linesize[0] + s->out.off_right;
365 
366  switch (s->out.format) {
367  case SIDE_BY_SIDE_LR:
368  case SIDE_BY_SIDE_RL:
369  case SIDE_BY_SIDE_2_LR:
370  case SIDE_BY_SIDE_2_RL:
371  case ABOVE_BELOW_LR:
372  case ABOVE_BELOW_RL:
373  case ABOVE_BELOW_2_LR:
374  case ABOVE_BELOW_2_RL:
375  case INTERLEAVE_ROWS_LR:
376  case INTERLEAVE_ROWS_RL:
377  av_image_copy_plane(out->data[0] + out_off_left,
378  out->linesize[0] * s->row_step,
379  inpicref->data[0] + in_off_left,
380  inpicref->linesize[0] * s->row_step,
381  3 * s->width, s->height);
382  av_image_copy_plane(out->data[0] + out_off_right,
383  out->linesize[0] * s->row_step,
384  inpicref->data[0] + in_off_right,
385  inpicref->linesize[0] * s->row_step,
386  3 * s->width, s->height);
387  break;
388  case MONO_L:
389  case MONO_R:
390  av_image_copy_plane(out->data[0], out->linesize[0],
391  inpicref->data[0] + in_off_left,
392  inpicref->linesize[0],
393  3 * s->width, s->height);
394  break;
395  case ANAGLYPH_RB_GRAY:
396  case ANAGLYPH_RG_GRAY:
397  case ANAGLYPH_RC_GRAY:
398  case ANAGLYPH_RC_HALF:
399  case ANAGLYPH_RC_COLOR:
400  case ANAGLYPH_RC_DUBOIS:
401  case ANAGLYPH_GM_GRAY:
402  case ANAGLYPH_GM_HALF:
403  case ANAGLYPH_GM_COLOR:
404  case ANAGLYPH_GM_DUBOIS:
405  case ANAGLYPH_YB_GRAY:
406  case ANAGLYPH_YB_HALF:
407  case ANAGLYPH_YB_COLOR:
408  case ANAGLYPH_YB_DUBOIS: {
409  int i, x, y, il, ir, o;
410  uint8_t *src = inpicref->data[0];
411  uint8_t *dst = out->data[0];
412  int out_width = s->out.width;
413  int *ana_matrix[3];
414 
415  for (i = 0; i < 3; i++)
416  ana_matrix[i] = s->ana_matrix[i];
417 
418  for (y = 0; y < s->out.height; y++) {
419  o = out->linesize[0] * y;
420  il = in_off_left + y * inpicref->linesize[0];
421  ir = in_off_right + y * inpicref->linesize[0];
422  for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
423  dst[o ] = ana_convert(ana_matrix[0], src + il, src + ir);
424  dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
425  dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
426  }
427  }
428  break;
429  }
430  default:
431  av_assert0(0);
432  }
433 
434  ret = ff_filter_frame(outlink, out);
435  avfilter_unref_bufferp(&inpicref);
436  if (ret < 0)
437  return ret;
438  return 0;
439 }
440 
441 static av_cold void uninit(AVFilterContext *ctx)
442 {
443  Stereo3DContext *s = ctx->priv;
444 
445  av_opt_free(s);
446 }
447 
448 static const AVFilterPad stereo3d_inputs[] = {
449  {
450  .name = "default",
451  .type = AVMEDIA_TYPE_VIDEO,
452  .get_video_buffer = ff_null_get_video_buffer,
453  .filter_frame = filter_frame,
454  .min_perms = AV_PERM_READ,
455  },
456  { NULL }
457 };
458 
459 static const AVFilterPad stereo3d_outputs[] = {
460  {
461  .name = "default",
462  .type = AVMEDIA_TYPE_VIDEO,
463  .config_props = config_output,
464  .min_perms = AV_PERM_WRITE,
465  },
466  { NULL }
467 };
468 
470  .name = "stereo3d",
471  .description = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
472  .priv_size = sizeof(Stereo3DContext),
473  .init = init,
474  .uninit = uninit,
476  .inputs = stereo3d_inputs,
477  .outputs = stereo3d_outputs,
478  .priv_class = &stereo3d_class,
479 };