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  *
4  * This file is part of MPlayer.
5  *
6  * MPlayer is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * MPlayer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 //==includes==//
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "help_mp.h"
29 
30 #include "img_format.h"
31 #include "mp_image.h"
32 #include "vf.h"
33 
34 #include "libavutil/common.h"
35 #include "libvo/fastmemcpy.h"
36 
37 //==types==//
38 typedef enum stereo_code {
39  ANAGLYPH_RC_GRAY, //anaglyph red/cyan gray
40  ANAGLYPH_RC_HALF, //anaglyph red/cyan half colored
41  ANAGLYPH_RC_COLOR, //anaglyph red/cyan colored
42  ANAGLYPH_RC_DUBOIS, //anaglyph red/cyan dubois
43  ANAGLYPH_GM_GRAY, //anaglyph green/magenta gray
44  ANAGLYPH_GM_HALF, //anaglyph green/magenta half colored
45  ANAGLYPH_GM_COLOR, //anaglyph green/magenta colored
46  ANAGLYPH_YB_GRAY, //anaglyph yellow/blue gray
47  ANAGLYPH_YB_HALF, //anaglyph yellow/blue half colored
48  ANAGLYPH_YB_COLOR, //anaglyph yellow/blue colored
49  MONO_L, //mono output for debugging (left eye only)
50  MONO_R, //mono output for debugging (right eye only)
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  INTERLEAVE_ROWS_LR, //row-interleave (left eye has top row)
60  INTERLEAVE_ROWS_RL, //row-interleave (right eye has top row)
61  STEREO_CODE_COUNT //no value set - TODO: needs autodetection
62 } stereo_code;
63 
64 typedef struct component {
66  unsigned int width;
67  unsigned int height;
68  unsigned int off_left;
69  unsigned int off_right;
70  unsigned int row_left;
71  unsigned int row_right;
72 } component;
73 
74 //==global variables==//
75 static const int ana_coeff[10][3][6] = {
76  {{19595, 38470, 7471, 0, 0, 0}, //ANAGLYPH_RC_GRAY
77  { 0, 0, 0, 19595, 38470, 7471},
78  { 0, 0, 0, 19595, 38470, 7471}},
79  {{19595, 38470, 7471, 0, 0, 0}, //ANAGLYPH_RC_HALF
80  { 0, 0, 0, 0, 65536, 0},
81  { 0, 0, 0, 0, 0, 65536}},
82  {{65536, 0, 0, 0, 0, 0}, //ANAGLYPH_RC_COLOR
83  { 0, 0, 0, 0, 65536, 0},
84  { 0, 0, 0, 0, 0, 65536}},
85  {{29891, 32800, 11559, -2849, -5763, -102}, //ANAGLYPH_RC_DUBOIS
86  {-2627, -2479, -1033, 24804, 48080, -1209},
87  { -997, -1350, -358, -4729, -7403, 80373}},
88  {{ 0, 0, 0, 19595, 38470, 7471}, //ANAGLYPH_GM_GRAY
89  {19595, 38470, 7471, 0, 0, 0},
90  { 0, 0, 0, 19595, 38470, 7471}},
91  {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_GM_HALF
92  {19595, 38470, 7471, 0, 0, 0},
93  { 0, 0, 0, 0, 0, 65536}},
94  {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_GM_COLOR
95  { 0, 65536, 0, 0, 0, 0},
96  { 0, 0, 0, 0, 0, 65536}},
97  {{ 0, 0, 0, 19595, 38470, 7471}, //ANAGLYPH_YB_GRAY
98  { 0, 0, 0, 19595, 38470, 7471},
99  {19595, 38470, 7471, 0, 0, 0}},
100  {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_YB_HALF
101  { 0, 0, 0, 0, 65536, 0},
102  {19595, 38470, 7471, 0, 0, 0}},
103  {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_YB_COLOR
104  { 0, 0, 0, 0, 65536, 0},
105  { 0, 0, 65536, 0, 0, 0}}
106 };
107 
108 struct vf_priv_s {
111  int ana_matrix[3][6];
112  unsigned int width;
113  unsigned int height;
114  unsigned int row_step;
115 } const ff_vf_priv_default = {
116  {SIDE_BY_SIDE_LR},
118 };
119 
120 //==functions==//
121 static inline uint8_t ana_convert(int coeff[6], uint8_t left[3], uint8_t right[3])
122 {
123  int sum;
124 
125  sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
126  sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
127  sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
128  return av_clip_uint8(sum >> 16);
129 }
130 
131 static int config(struct vf_instance *vf, int width, int height, int d_width,
132  int d_height, unsigned int flags, unsigned int outfmt)
133 {
134  if ((width & 1) || (height & 1)) {
135  ff_mp_msg(MSGT_VFILTER, MSGL_WARN, "[stereo3d] invalid height or width\n");
136  return 0;
137  }
138  //default input values
139  vf->priv->width = width;
140  vf->priv->height = height;
141  vf->priv->row_step = 1;
142  vf->priv->in.width = width;
143  vf->priv->in.height = height;
144  vf->priv->in.off_left = 0;
145  vf->priv->in.off_right = 0;
146  vf->priv->in.row_left = 0;
147  vf->priv->in.row_right = 0;
148 
149  //check input format
150  switch (vf->priv->in.fmt) {
151  case SIDE_BY_SIDE_2_LR:
152  d_width *= 2;
153  case SIDE_BY_SIDE_LR:
154  vf->priv->width = width / 2;
155  vf->priv->in.off_right = vf->priv->width * 3;
156  break;
157  case SIDE_BY_SIDE_2_RL:
158  d_width *= 2;
159  case SIDE_BY_SIDE_RL:
160  vf->priv->width = width / 2;
161  vf->priv->in.off_left = vf->priv->width * 3;
162  break;
163  case ABOVE_BELOW_2_LR:
164  d_height *= 2;
165  case ABOVE_BELOW_LR:
166  vf->priv->height = height / 2;
167  vf->priv->in.row_right = vf->priv->height;
168  break;
169  case ABOVE_BELOW_2_RL:
170  d_height *= 2;
171  case ABOVE_BELOW_RL:
172  vf->priv->height = height / 2;
173  vf->priv->in.row_left = vf->priv->height;
174  break;
175  default:
177  "[stereo3d] stereo format of input is not supported\n");
178  return 0;
179  break;
180  }
181  //default output values
182  vf->priv->out.width = vf->priv->width;
183  vf->priv->out.height = vf->priv->height;
184  vf->priv->out.off_left = 0;
185  vf->priv->out.off_right = 0;
186  vf->priv->out.row_left = 0;
187  vf->priv->out.row_right = 0;
188 
189  //check output format
190  switch (vf->priv->out.fmt) {
191  case ANAGLYPH_RC_GRAY:
192  case ANAGLYPH_RC_HALF:
193  case ANAGLYPH_RC_COLOR:
194  case ANAGLYPH_RC_DUBOIS:
195  case ANAGLYPH_GM_GRAY:
196  case ANAGLYPH_GM_HALF:
197  case ANAGLYPH_GM_COLOR:
198  case ANAGLYPH_YB_GRAY:
199  case ANAGLYPH_YB_HALF:
200  case ANAGLYPH_YB_COLOR:
201  memcpy(vf->priv->ana_matrix, ana_coeff[vf->priv->out.fmt],
202  sizeof(vf->priv->ana_matrix));
203  break;
204  case SIDE_BY_SIDE_2_LR:
205  d_width /= 2;
206  case SIDE_BY_SIDE_LR:
207  vf->priv->out.width = vf->priv->width * 2;
208  vf->priv->out.off_right = vf->priv->width * 3;
209  break;
210  case SIDE_BY_SIDE_2_RL:
211  d_width /= 2;
212  case SIDE_BY_SIDE_RL:
213  vf->priv->out.width = vf->priv->width * 2;
214  vf->priv->out.off_left = vf->priv->width * 3;
215  break;
216  case ABOVE_BELOW_2_LR:
217  d_height /= 2;
218  case ABOVE_BELOW_LR:
219  vf->priv->out.height = vf->priv->height * 2;
220  vf->priv->out.row_right = vf->priv->height;
221  break;
222  case ABOVE_BELOW_2_RL:
223  d_height /= 2;
224  case ABOVE_BELOW_RL:
225  vf->priv->out.height = vf->priv->height * 2;
226  vf->priv->out.row_left = vf->priv->height;
227  break;
228  case INTERLEAVE_ROWS_LR:
229  vf->priv->row_step = 2;
230  vf->priv->height = vf->priv->height / 2;
231  vf->priv->out.off_right = vf->priv->width * 3;
232  vf->priv->in.off_right += vf->priv->in.width * 3;
233  break;
234  case INTERLEAVE_ROWS_RL:
235  vf->priv->row_step = 2;
236  vf->priv->height = vf->priv->height / 2;
237  vf->priv->out.off_left = vf->priv->width * 3;
238  vf->priv->in.off_left += vf->priv->in.width * 3;
239  break;
240  case MONO_R:
241  //same as MONO_L only needs switching of input offsets
242  vf->priv->in.off_left = vf->priv->in.off_right;
243  vf->priv->in.row_left = vf->priv->in.row_right;
244  //nobreak;
245  case MONO_L:
246  //use default settings
247  break;
248  default:
250  "[stereo3d] stereo format of output is not supported\n");
251  return 0;
252  break;
253  }
254 // if (!opt_screen_size_x && !opt_screen_size_y) {
255  d_width = d_width * vf->priv->out.width / width;
256  d_height = d_height * vf->priv->out.height / height;
257 // }
258 
259  return ff_vf_next_config(vf, vf->priv->out.width, vf->priv->out.height,
260  d_width, d_height, flags, outfmt);
261 }
262 
263 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
264 {
265  mp_image_t *dmpi;
266  if (vf->priv->in.fmt == vf->priv->out.fmt) { //nothing to do
267  dmpi = mpi;
268  } else {
269  int out_off_left, out_off_right;
270  int in_off_left = vf->priv->in.row_left * mpi->stride[0] +
271  vf->priv->in.off_left;
272  int in_off_right = vf->priv->in.row_right * mpi->stride[0] +
273  vf->priv->in.off_right;
274 
275  dmpi = ff_vf_get_image(vf->next, IMGFMT_RGB24, MP_IMGTYPE_TEMP,
277  vf->priv->out.width, vf->priv->out.height);
278  out_off_left = vf->priv->out.row_left * dmpi->stride[0] +
279  vf->priv->out.off_left;
280  out_off_right = vf->priv->out.row_right * dmpi->stride[0] +
281  vf->priv->out.off_right;
282 
283  switch (vf->priv->out.fmt) {
284  case SIDE_BY_SIDE_LR:
285  case SIDE_BY_SIDE_RL:
286  case SIDE_BY_SIDE_2_LR:
287  case SIDE_BY_SIDE_2_RL:
288  case ABOVE_BELOW_LR:
289  case ABOVE_BELOW_RL:
290  case ABOVE_BELOW_2_LR:
291  case ABOVE_BELOW_2_RL:
292  case INTERLEAVE_ROWS_LR:
293  case INTERLEAVE_ROWS_RL:
294  memcpy_pic2(dmpi->planes[0] + out_off_left,
295  mpi->planes[0] + in_off_left,
296  3 * vf->priv->width,
297  vf->priv->height,
298  dmpi->stride[0] * vf->priv->row_step,
299  mpi->stride[0] * vf->priv->row_step,
300  vf->priv->row_step != 1);
301  memcpy_pic2(dmpi->planes[0] + out_off_right,
302  mpi->planes[0] + in_off_right,
303  3 * vf->priv->width,
304  vf->priv->height,
305  dmpi->stride[0] * vf->priv->row_step,
306  mpi->stride[0] * vf->priv->row_step,
307  vf->priv->row_step != 1);
308  break;
309  case MONO_L:
310  case MONO_R:
311  memcpy_pic(dmpi->planes[0],
312  mpi->planes[0] + in_off_left,
313  3 * vf->priv->width,
314  vf->priv->height,
315  dmpi->stride[0],
316  mpi->stride[0]);
317  break;
318  case ANAGLYPH_RC_GRAY:
319  case ANAGLYPH_RC_HALF:
320  case ANAGLYPH_RC_COLOR:
321  case ANAGLYPH_RC_DUBOIS:
322  case ANAGLYPH_GM_GRAY:
323  case ANAGLYPH_GM_HALF:
324  case ANAGLYPH_GM_COLOR:
325  case ANAGLYPH_YB_GRAY:
326  case ANAGLYPH_YB_HALF:
327  case ANAGLYPH_YB_COLOR: {
328  int i,x,y,il,ir,o;
329  unsigned char *source = mpi->planes[0];
330  unsigned char *dest = dmpi->planes[0];
331  unsigned int out_width = vf->priv->out.width;
332  int *ana_matrix[3];
333 
334  for(i = 0; i < 3; i++)
335  ana_matrix[i] = vf->priv->ana_matrix[i];
336 
337  for (y = 0; y < vf->priv->out.height; y++) {
338  o = dmpi->stride[0] * y;
339  il = in_off_left + y * mpi->stride[0];
340  ir = in_off_right + y * mpi->stride[0];
341  for (x = 0; x < out_width; x++) {
342  dest[o ] = ana_convert(
343  ana_matrix[0], source + il, source + ir); //red out
344  dest[o + 1] = ana_convert(
345  ana_matrix[1], source + il, source + ir); //green out
346  dest[o + 2] = ana_convert(
347  ana_matrix[2], source + il, source + ir); //blue out
348  il += 3;
349  ir += 3;
350  o += 3;
351  }
352  }
353  break;
354  }
355  default:
357  "[stereo3d] stereo format of output is not supported\n");
358  return 0;
359  break;
360  }
361  }
362  return ff_vf_next_put_image(vf, dmpi, pts);
363 }
364 
365 static int query_format(struct vf_instance *vf, unsigned int fmt)
366 {
367  switch (fmt)
368  case IMGFMT_RGB24:
369  return ff_vf_next_query_format(vf, fmt);
370  return 0;
371 }
372 
373 static void uninit(vf_instance_t *vf)
374 {
375  free(vf->priv);
376 }
377 
378 static int vf_open(vf_instance_t *vf, char *args)
379 {
380  vf->config = config;
381  vf->uninit = uninit;
382  vf->put_image = put_image;
384  vf->priv=malloc(sizeof(struct vf_priv_s));
385  memset(vf->priv, 0, sizeof(struct vf_priv_s));
386 
387  vf->priv->in.fmt = SIDE_BY_SIDE_LR;
388  vf->priv->out.fmt= ANAGLYPH_RC_DUBOIS;
389  if (args) sscanf(args, "%d:%d", &vf->priv->in.fmt, &vf->priv->out.fmt);
390 
391  return 1;
392 }
393 #if 0
394 ///Presets usage
395 static const struct format_preset {
396  char* name;
397  stereo_code scode;
398 } vf_format_presets_defs[] = {
399  {"arcg", ANAGLYPH_RC_GRAY},
400  {"anaglyph_red_cyan_gray", ANAGLYPH_RC_GRAY},
401  {"arch", ANAGLYPH_RC_HALF},
402  {"anaglyph_red_cyan_half_color", ANAGLYPH_RC_HALF},
403  {"arcc", ANAGLYPH_RC_COLOR},
404  {"anaglyph_red_cyan_color", ANAGLYPH_RC_COLOR},
405  {"arcd", ANAGLYPH_RC_DUBOIS},
406  {"anaglyph_red_cyan_dubios", ANAGLYPH_RC_DUBOIS},
407  {"agmg", ANAGLYPH_GM_GRAY},
408  {"anaglyph_green_magenta_gray", ANAGLYPH_GM_GRAY},
409  {"agmh", ANAGLYPH_GM_HALF},
410  {"anaglyph_green_magenta_half_color",ANAGLYPH_GM_HALF},
411  {"agmc", ANAGLYPH_GM_COLOR},
412  {"anaglyph_green_magenta_color", ANAGLYPH_GM_COLOR},
413  {"aybg", ANAGLYPH_YB_GRAY},
414  {"anaglyph_yellow_blue_gray", ANAGLYPH_YB_GRAY},
415  {"aybh", ANAGLYPH_YB_HALF},
416  {"anaglyph_yellow_blue_half_color", ANAGLYPH_YB_HALF},
417  {"aybc", ANAGLYPH_YB_COLOR},
418  {"anaglyph_yellow_blue_color", ANAGLYPH_YB_COLOR},
419  {"ml", MONO_L},
420  {"mono_left", MONO_L},
421  {"mr", MONO_R},
422  {"mono_right", MONO_R},
423  {"sbsl", SIDE_BY_SIDE_LR},
424  {"side_by_side_left_first", SIDE_BY_SIDE_LR},
425  {"sbsr", SIDE_BY_SIDE_RL},
426  {"side_by_side_right_first", SIDE_BY_SIDE_RL},
427  {"sbs2l", SIDE_BY_SIDE_2_LR},
428  {"side_by_side_half_width_left_first", SIDE_BY_SIDE_2_LR},
429  {"sbs2r", SIDE_BY_SIDE_2_RL},
430  {"side_by_side_half_width_right_first",SIDE_BY_SIDE_2_RL},
431  {"abl", ABOVE_BELOW_LR},
432  {"above_below_left_first", ABOVE_BELOW_LR},
433  {"abr", ABOVE_BELOW_RL},
434  {"above_below_right_first", ABOVE_BELOW_RL},
435  {"ab2l", ABOVE_BELOW_2_LR},
436  {"above_below_half_height_left_first", ABOVE_BELOW_2_LR},
437  {"ab2r", ABOVE_BELOW_2_RL},
438  {"above_below_half_height_right_first",ABOVE_BELOW_2_RL},
439  {"irl", INTERLEAVE_ROWS_LR},
440  {"interleave_rows_left_first", INTERLEAVE_ROWS_LR},
441  {"irr", INTERLEAVE_ROWS_RL},
442  {"interleave_rows_right_first", INTERLEAVE_ROWS_RL},
443  { NULL, 0}
444 };
445 
446 #define ST_OFF(f) M_ST_OFF(struct format_preset,f)
447 static const m_option_t vf_format_preset_fields_in[] = {
448  {"in", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
449  { NULL, NULL, 0, 0, 0, 0, NULL }
450 };
451 static const m_option_t vf_format_preset_fields_out[] = {
452  {"out", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
453  { NULL, NULL, 0, 0, 0, 0, NULL }
454 };
455 
456 static const m_struct_t vf_format_preset_in = {
457  "stereo_format_preset_in",
458  sizeof(struct format_preset),
459  NULL,
460  vf_format_preset_fields_in
461 };
462 static const m_struct_t vf_format_preset_out = {
463  "stereo_format_preset_out",
464  sizeof(struct format_preset),
465  NULL,
466  vf_format_preset_fields_out
467 };
468 
469 static const m_struct_t vf_opts;
470 static const m_obj_presets_t format_preset_in = {
471  (struct m_struct_st*)&vf_format_preset_in,
472  (struct m_struct_st*)&vf_opts,
473  (struct format_preset*)vf_format_presets_defs,
474  ST_OFF(name)
475 };
476 static const m_obj_presets_t format_preset_out = {
477  (struct m_struct_st*)&vf_format_preset_out,
478  (struct m_struct_st*)&vf_opts,
479  (struct format_preset*)vf_format_presets_defs,
480  ST_OFF(name)
481 };
482 
483 /// Now the options
484 #undef ST_OFF
485 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
486 static const m_option_t vf_opts_fields[] = {
487  {"stereo_in", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
488  (m_obj_presets_t*)&format_preset_in},
489  {"stereo_out", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
490  (m_obj_presets_t*)&format_preset_out},
491  {"in", ST_OFF(in.fmt), CONF_TYPE_INT, 0,0,0, NULL},
492  {"out", ST_OFF(out.fmt), CONF_TYPE_INT, 0,0,0, NULL},
493  { NULL, NULL, 0, 0, 0, 0, NULL }
494 };
495 
496 static const m_struct_t vf_opts = {
497  "stereo3d",
498  sizeof(struct vf_priv_s),
500  vf_opts_fields
501 };
502 #endif
503 
504 //==info struct==//
506  "stereoscopic 3d view",
507  "stereo3d",
508  "Gordon Schmidt",
509  "view stereoscopic videos",
510  vf_open,
511 // &vf_opts
512 };