2 * Copyright (C) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
4 * This file is part of MPlayer.
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.
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.
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.
30 #include "img_format.h"
34 #include "libavutil/common.h"
35 #include "libvo/fastmemcpy.h"
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_GM_DUBOIS, //anaglyph green/magenta dubois
47 ANAGLYPH_YB_GRAY, //anaglyph yellow/blue gray
48 ANAGLYPH_YB_HALF, //anaglyph yellow/blue half colored
49 ANAGLYPH_YB_COLOR, //anaglyph yellow/blue colored
50 ANAGLYPH_YB_DUBOIS, //anaglyph yellow/blue dubois
51 MONO_L, //mono output for debugging (left eye only)
52 MONO_R, //mono output for debugging (right eye only)
53 SIDE_BY_SIDE_LR, //side by side parallel (left eye left, right eye right)
54 SIDE_BY_SIDE_RL, //side by side crosseye (right eye left, left eye right)
55 SIDE_BY_SIDE_2_LR, //side by side parallel with half width resolution
56 SIDE_BY_SIDE_2_RL, //side by side crosseye with half width resolution
57 ABOVE_BELOW_LR, //above-below (left eye above, right eye below)
58 ABOVE_BELOW_RL, //above-below (right eye above, left eye below)
59 ABOVE_BELOW_2_LR, //above-below with half height resolution
60 ABOVE_BELOW_2_RL, //above-below with half height resolution
61 INTERLEAVE_ROWS_LR, //row-interleave (left eye has top row)
62 INTERLEAVE_ROWS_RL, //row-interleave (right eye has top row)
63 STEREO_CODE_COUNT //no value set - TODO: needs autodetection
66 typedef struct component {
70 unsigned int off_left;
71 unsigned int off_right;
72 unsigned int row_left;
73 unsigned int row_right;
76 //==global variables==//
77 static const int ana_coeff[][3][6] = {
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}},
131 int ana_matrix[3][6];
134 unsigned int row_step;
135 } const ff_vf_priv_default = {
141 static inline uint8_t ana_convert(int coeff[6], uint8_t left[3], uint8_t right[3])
145 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in
146 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
147 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
148 return av_clip_uint8(sum >> 16);
151 static int config(struct vf_instance *vf, int width, int height, int d_width,
152 int d_height, unsigned int flags, unsigned int outfmt)
154 if ((width & 1) || (height & 1)) {
155 ff_mp_msg(MSGT_VFILTER, MSGL_WARN, "[stereo3d] invalid height or width\n");
158 //default input values
159 vf->priv->width = width;
160 vf->priv->height = height;
161 vf->priv->row_step = 1;
162 vf->priv->in.width = width;
163 vf->priv->in.height = height;
164 vf->priv->in.off_left = 0;
165 vf->priv->in.off_right = 0;
166 vf->priv->in.row_left = 0;
167 vf->priv->in.row_right = 0;
170 switch (vf->priv->in.fmt) {
171 case SIDE_BY_SIDE_2_LR:
173 case SIDE_BY_SIDE_LR:
174 vf->priv->width = width / 2;
175 vf->priv->in.off_right = vf->priv->width * 3;
177 case SIDE_BY_SIDE_2_RL:
179 case SIDE_BY_SIDE_RL:
180 vf->priv->width = width / 2;
181 vf->priv->in.off_left = vf->priv->width * 3;
183 case ABOVE_BELOW_2_LR:
186 vf->priv->height = height / 2;
187 vf->priv->in.row_right = vf->priv->height;
189 case ABOVE_BELOW_2_RL:
192 vf->priv->height = height / 2;
193 vf->priv->in.row_left = vf->priv->height;
196 ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
197 "[stereo3d] stereo format of input is not supported\n");
201 //default output values
202 vf->priv->out.width = vf->priv->width;
203 vf->priv->out.height = vf->priv->height;
204 vf->priv->out.off_left = 0;
205 vf->priv->out.off_right = 0;
206 vf->priv->out.row_left = 0;
207 vf->priv->out.row_right = 0;
209 //check output format
210 switch (vf->priv->out.fmt) {
211 case ANAGLYPH_RC_GRAY:
212 case ANAGLYPH_RC_HALF:
213 case ANAGLYPH_RC_COLOR:
214 case ANAGLYPH_RC_DUBOIS:
215 case ANAGLYPH_GM_GRAY:
216 case ANAGLYPH_GM_HALF:
217 case ANAGLYPH_GM_COLOR:
218 case ANAGLYPH_GM_DUBOIS:
219 case ANAGLYPH_YB_GRAY:
220 case ANAGLYPH_YB_HALF:
221 case ANAGLYPH_YB_COLOR:
222 case ANAGLYPH_YB_DUBOIS:
223 memcpy(vf->priv->ana_matrix, ana_coeff[vf->priv->out.fmt],
224 sizeof(vf->priv->ana_matrix));
226 case SIDE_BY_SIDE_2_LR:
228 case SIDE_BY_SIDE_LR:
229 vf->priv->out.width = vf->priv->width * 2;
230 vf->priv->out.off_right = vf->priv->width * 3;
232 case SIDE_BY_SIDE_2_RL:
234 case SIDE_BY_SIDE_RL:
235 vf->priv->out.width = vf->priv->width * 2;
236 vf->priv->out.off_left = vf->priv->width * 3;
238 case ABOVE_BELOW_2_LR:
241 vf->priv->out.height = vf->priv->height * 2;
242 vf->priv->out.row_right = vf->priv->height;
244 case ABOVE_BELOW_2_RL:
247 vf->priv->out.height = vf->priv->height * 2;
248 vf->priv->out.row_left = vf->priv->height;
250 case INTERLEAVE_ROWS_LR:
251 vf->priv->row_step = 2;
252 vf->priv->height = vf->priv->height / 2;
253 vf->priv->out.off_right = vf->priv->width * 3;
254 vf->priv->in.off_right += vf->priv->in.width * 3;
256 case INTERLEAVE_ROWS_RL:
257 vf->priv->row_step = 2;
258 vf->priv->height = vf->priv->height / 2;
259 vf->priv->out.off_left = vf->priv->width * 3;
260 vf->priv->in.off_left += vf->priv->in.width * 3;
263 //same as MONO_L only needs switching of input offsets
264 vf->priv->in.off_left = vf->priv->in.off_right;
265 vf->priv->in.row_left = vf->priv->in.row_right;
268 //use default settings
271 ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
272 "[stereo3d] stereo format of output is not supported\n");
276 // if (!opt_screen_size_x && !opt_screen_size_y) {
277 d_width = d_width * vf->priv->out.width / width;
278 d_height = d_height * vf->priv->out.height / height;
281 return ff_vf_next_config(vf, vf->priv->out.width, vf->priv->out.height,
282 d_width, d_height, flags, outfmt);
285 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
288 if (vf->priv->in.fmt == vf->priv->out.fmt) { //nothing to do
291 int out_off_left, out_off_right;
292 int in_off_left = vf->priv->in.row_left * mpi->stride[0] +
293 vf->priv->in.off_left;
294 int in_off_right = vf->priv->in.row_right * mpi->stride[0] +
295 vf->priv->in.off_right;
297 dmpi = ff_vf_get_image(vf->next, IMGFMT_RGB24, MP_IMGTYPE_TEMP,
298 MP_IMGFLAG_ACCEPT_STRIDE,
299 vf->priv->out.width, vf->priv->out.height);
300 out_off_left = vf->priv->out.row_left * dmpi->stride[0] +
301 vf->priv->out.off_left;
302 out_off_right = vf->priv->out.row_right * dmpi->stride[0] +
303 vf->priv->out.off_right;
305 switch (vf->priv->out.fmt) {
306 case SIDE_BY_SIDE_LR:
307 case SIDE_BY_SIDE_RL:
308 case SIDE_BY_SIDE_2_LR:
309 case SIDE_BY_SIDE_2_RL:
312 case ABOVE_BELOW_2_LR:
313 case ABOVE_BELOW_2_RL:
314 case INTERLEAVE_ROWS_LR:
315 case INTERLEAVE_ROWS_RL:
316 memcpy_pic2(dmpi->planes[0] + out_off_left,
317 mpi->planes[0] + in_off_left,
320 dmpi->stride[0] * vf->priv->row_step,
321 mpi->stride[0] * vf->priv->row_step,
322 vf->priv->row_step != 1);
323 memcpy_pic2(dmpi->planes[0] + out_off_right,
324 mpi->planes[0] + in_off_right,
327 dmpi->stride[0] * vf->priv->row_step,
328 mpi->stride[0] * vf->priv->row_step,
329 vf->priv->row_step != 1);
333 memcpy_pic(dmpi->planes[0],
334 mpi->planes[0] + in_off_left,
340 case ANAGLYPH_RC_GRAY:
341 case ANAGLYPH_RC_HALF:
342 case ANAGLYPH_RC_COLOR:
343 case ANAGLYPH_RC_DUBOIS:
344 case ANAGLYPH_GM_GRAY:
345 case ANAGLYPH_GM_HALF:
346 case ANAGLYPH_GM_COLOR:
347 case ANAGLYPH_GM_DUBOIS:
348 case ANAGLYPH_YB_GRAY:
349 case ANAGLYPH_YB_HALF:
350 case ANAGLYPH_YB_COLOR:
351 case ANAGLYPH_YB_DUBOIS: {
353 unsigned char *source = mpi->planes[0];
354 unsigned char *dest = dmpi->planes[0];
355 unsigned int out_width = vf->priv->out.width;
358 for(i = 0; i < 3; i++)
359 ana_matrix[i] = vf->priv->ana_matrix[i];
361 for (y = 0; y < vf->priv->out.height; y++) {
362 o = dmpi->stride[0] * y;
363 il = in_off_left + y * mpi->stride[0];
364 ir = in_off_right + y * mpi->stride[0];
365 for (x = 0; x < out_width; x++) {
366 dest[o ] = ana_convert(
367 ana_matrix[0], source + il, source + ir); //red out
368 dest[o + 1] = ana_convert(
369 ana_matrix[1], source + il, source + ir); //green out
370 dest[o + 2] = ana_convert(
371 ana_matrix[2], source + il, source + ir); //blue out
380 ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
381 "[stereo3d] stereo format of output is not supported\n");
386 return ff_vf_next_put_image(vf, dmpi, pts);
389 static int query_format(struct vf_instance *vf, unsigned int fmt)
393 return ff_vf_next_query_format(vf, fmt);
397 static void uninit(vf_instance_t *vf)
402 static int vf_open(vf_instance_t *vf, char *args)
406 vf->put_image = put_image;
407 vf->query_format = query_format;
408 vf->priv=malloc(sizeof(struct vf_priv_s));
409 memset(vf->priv, 0, sizeof(struct vf_priv_s));
411 vf->priv->in.fmt = SIDE_BY_SIDE_LR;
412 vf->priv->out.fmt= ANAGLYPH_RC_DUBOIS;
413 if (args) sscanf(args, "%d:%d", &vf->priv->in.fmt, &vf->priv->out.fmt);
419 static const struct format_preset {
422 } vf_format_presets_defs[] = {
423 {"arcg", ANAGLYPH_RC_GRAY},
424 {"anaglyph_red_cyan_gray", ANAGLYPH_RC_GRAY},
425 {"arch", ANAGLYPH_RC_HALF},
426 {"anaglyph_red_cyan_half_color", ANAGLYPH_RC_HALF},
427 {"arcc", ANAGLYPH_RC_COLOR},
428 {"anaglyph_red_cyan_color", ANAGLYPH_RC_COLOR},
429 {"arcd", ANAGLYPH_RC_DUBOIS},
430 {"anaglyph_red_cyan_dubios", ANAGLYPH_RC_DUBOIS},
431 {"agmg", ANAGLYPH_GM_GRAY},
432 {"anaglyph_green_magenta_gray", ANAGLYPH_GM_GRAY},
433 {"agmh", ANAGLYPH_GM_HALF},
434 {"anaglyph_green_magenta_half_color",ANAGLYPH_GM_HALF},
435 {"agmc", ANAGLYPH_GM_COLOR},
436 {"anaglyph_green_magenta_color", ANAGLYPH_GM_COLOR},
437 {"agmd", ANAGLYPH_GM_DUBOIS},
438 {"anaglyph_green_magenta_dubois", ANAGLYPH_GM_DUBOIS},
439 {"aybg", ANAGLYPH_YB_GRAY},
440 {"anaglyph_yellow_blue_gray", ANAGLYPH_YB_GRAY},
441 {"aybh", ANAGLYPH_YB_HALF},
442 {"anaglyph_yellow_blue_half_color", ANAGLYPH_YB_HALF},
443 {"aybc", ANAGLYPH_YB_COLOR},
444 {"anaglyph_yellow_blue_color", ANAGLYPH_YB_COLOR},
445 {"aybd", ANAGLYPH_YB_DUBOIS},
446 {"anaglyph_yellow_blue_dubois", ANAGLYPH_YB_DUBOIS},
448 {"mono_left", MONO_L},
450 {"mono_right", MONO_R},
451 {"sbsl", SIDE_BY_SIDE_LR},
452 {"side_by_side_left_first", SIDE_BY_SIDE_LR},
453 {"sbsr", SIDE_BY_SIDE_RL},
454 {"side_by_side_right_first", SIDE_BY_SIDE_RL},
455 {"sbs2l", SIDE_BY_SIDE_2_LR},
456 {"side_by_side_half_width_left_first", SIDE_BY_SIDE_2_LR},
457 {"sbs2r", SIDE_BY_SIDE_2_RL},
458 {"side_by_side_half_width_right_first",SIDE_BY_SIDE_2_RL},
459 {"abl", ABOVE_BELOW_LR},
460 {"above_below_left_first", ABOVE_BELOW_LR},
461 {"abr", ABOVE_BELOW_RL},
462 {"above_below_right_first", ABOVE_BELOW_RL},
463 {"ab2l", ABOVE_BELOW_2_LR},
464 {"above_below_half_height_left_first", ABOVE_BELOW_2_LR},
465 {"ab2r", ABOVE_BELOW_2_RL},
466 {"above_below_half_height_right_first",ABOVE_BELOW_2_RL},
467 {"irl", INTERLEAVE_ROWS_LR},
468 {"interleave_rows_left_first", INTERLEAVE_ROWS_LR},
469 {"irr", INTERLEAVE_ROWS_RL},
470 {"interleave_rows_right_first", INTERLEAVE_ROWS_RL},
474 #define ST_OFF(f) M_ST_OFF(struct format_preset,f)
475 static const m_option_t vf_format_preset_fields_in[] = {
476 {"in", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
477 { NULL, NULL, 0, 0, 0, 0, NULL }
479 static const m_option_t vf_format_preset_fields_out[] = {
480 {"out", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
481 { NULL, NULL, 0, 0, 0, 0, NULL }
484 static const m_struct_t vf_format_preset_in = {
485 "stereo_format_preset_in",
486 sizeof(struct format_preset),
488 vf_format_preset_fields_in
490 static const m_struct_t vf_format_preset_out = {
491 "stereo_format_preset_out",
492 sizeof(struct format_preset),
494 vf_format_preset_fields_out
497 static const m_struct_t vf_opts;
498 static const m_obj_presets_t format_preset_in = {
499 (struct m_struct_st*)&vf_format_preset_in,
500 (struct m_struct_st*)&vf_opts,
501 (struct format_preset*)vf_format_presets_defs,
504 static const m_obj_presets_t format_preset_out = {
505 (struct m_struct_st*)&vf_format_preset_out,
506 (struct m_struct_st*)&vf_opts,
507 (struct format_preset*)vf_format_presets_defs,
513 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
514 static const m_option_t vf_opts_fields[] = {
515 {"stereo_in", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
516 (m_obj_presets_t*)&format_preset_in},
517 {"stereo_out", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
518 (m_obj_presets_t*)&format_preset_out},
519 {"in", ST_OFF(in.fmt), CONF_TYPE_INT, 0,0,0, NULL},
520 {"out", ST_OFF(out.fmt), CONF_TYPE_INT, 0,0,0, NULL},
521 { NULL, NULL, 0, 0, 0, 0, NULL }
524 static const m_struct_t vf_opts = {
526 sizeof(struct vf_priv_s),
533 const vf_info_t ff_vf_info_stereo3d = {
534 "stereoscopic 3d view",
537 "view stereoscopic videos",