00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026 #include "config.h"
00027 #include "mp_msg.h"
00028 #include "help_mp.h"
00029
00030 #include "img_format.h"
00031 #include "mp_image.h"
00032 #include "vf.h"
00033
00034 #include "libavutil/common.h"
00035 #include "libvo/fastmemcpy.h"
00036
00037
00038 typedef enum stereo_code {
00039 ANAGLYPH_RC_GRAY,
00040 ANAGLYPH_RC_HALF,
00041 ANAGLYPH_RC_COLOR,
00042 ANAGLYPH_RC_DUBOIS,
00043 ANAGLYPH_GM_GRAY,
00044 ANAGLYPH_GM_HALF,
00045 ANAGLYPH_GM_COLOR,
00046 ANAGLYPH_YB_GRAY,
00047 ANAGLYPH_YB_HALF,
00048 ANAGLYPH_YB_COLOR,
00049 MONO_L,
00050 MONO_R,
00051 SIDE_BY_SIDE_LR,
00052 SIDE_BY_SIDE_RL,
00053 SIDE_BY_SIDE_2_LR,
00054 SIDE_BY_SIDE_2_RL,
00055 ABOVE_BELOW_LR,
00056 ABOVE_BELOW_RL,
00057 ABOVE_BELOW_2_LR,
00058 ABOVE_BELOW_2_RL,
00059 INTERLEAVE_ROWS_LR,
00060 INTERLEAVE_ROWS_RL,
00061 STEREO_CODE_COUNT
00062 } stereo_code;
00063
00064 typedef struct component {
00065 stereo_code fmt;
00066 unsigned int width;
00067 unsigned int height;
00068 unsigned int off_left;
00069 unsigned int off_right;
00070 unsigned int row_left;
00071 unsigned int row_right;
00072 } component;
00073
00074
00075 static const int ana_coeff[10][3][6] = {
00076 {{19595, 38470, 7471, 0, 0, 0},
00077 { 0, 0, 0, 19595, 38470, 7471},
00078 { 0, 0, 0, 19595, 38470, 7471}},
00079 {{19595, 38470, 7471, 0, 0, 0},
00080 { 0, 0, 0, 0, 65536, 0},
00081 { 0, 0, 0, 0, 0, 65536}},
00082 {{65536, 0, 0, 0, 0, 0},
00083 { 0, 0, 0, 0, 65536, 0},
00084 { 0, 0, 0, 0, 0, 65536}},
00085 {{29891, 32800, 11559, -2849, -5763, -102},
00086 {-2627, -2479, -1033, 24804, 48080, -1209},
00087 { -997, -1350, -358, -4729, -7403, 80373}},
00088 {{ 0, 0, 0, 19595, 38470, 7471},
00089 {19595, 38470, 7471, 0, 0, 0},
00090 { 0, 0, 0, 19595, 38470, 7471}},
00091 {{ 0, 0, 0, 65536, 0, 0},
00092 {19595, 38470, 7471, 0, 0, 0},
00093 { 0, 0, 0, 0, 0, 65536}},
00094 {{ 0, 0, 0, 65536, 0, 0},
00095 { 0, 65536, 0, 0, 0, 0},
00096 { 0, 0, 0, 0, 0, 65536}},
00097 {{ 0, 0, 0, 19595, 38470, 7471},
00098 { 0, 0, 0, 19595, 38470, 7471},
00099 {19595, 38470, 7471, 0, 0, 0}},
00100 {{ 0, 0, 0, 65536, 0, 0},
00101 { 0, 0, 0, 0, 65536, 0},
00102 {19595, 38470, 7471, 0, 0, 0}},
00103 {{ 0, 0, 0, 65536, 0, 0},
00104 { 0, 0, 0, 0, 65536, 0},
00105 { 0, 0, 65536, 0, 0, 0}}
00106 };
00107
00108 struct vf_priv_s {
00109 component in;
00110 component out;
00111 int ana_matrix[3][6];
00112 unsigned int width;
00113 unsigned int height;
00114 unsigned int row_step;
00115 } const vf_priv_default = {
00116 {SIDE_BY_SIDE_LR},
00117 {ANAGLYPH_RC_DUBOIS}
00118 };
00119
00120
00121 static inline uint8_t ana_convert(int coeff[6], uint8_t left[3], uint8_t right[3])
00122 {
00123 int sum;
00124
00125 sum = coeff[0] * left[0] + coeff[3] * right[0];
00126 sum += coeff[1] * left[1] + coeff[4] * right[1];
00127 sum += coeff[2] * left[2] + coeff[5] * right[2];
00128 return av_clip_uint8(sum >> 16);
00129 }
00130
00131 static int config(struct vf_instance *vf, int width, int height, int d_width,
00132 int d_height, unsigned int flags, unsigned int outfmt)
00133 {
00134 if ((width & 1) || (height & 1)) {
00135 mp_msg(MSGT_VFILTER, MSGL_WARN, "[stereo3d] invalid height or width\n");
00136 return 0;
00137 }
00138
00139 vf->priv->width = width;
00140 vf->priv->height = height;
00141 vf->priv->row_step = 1;
00142 vf->priv->in.width = width;
00143 vf->priv->in.height = height;
00144 vf->priv->in.off_left = 0;
00145 vf->priv->in.off_right = 0;
00146 vf->priv->in.row_left = 0;
00147 vf->priv->in.row_right = 0;
00148
00149
00150 switch (vf->priv->in.fmt) {
00151 case SIDE_BY_SIDE_2_LR:
00152 d_width *= 2;
00153 case SIDE_BY_SIDE_LR:
00154 vf->priv->width = width / 2;
00155 vf->priv->in.off_right = vf->priv->width * 3;
00156 break;
00157 case SIDE_BY_SIDE_2_RL:
00158 d_width *= 2;
00159 case SIDE_BY_SIDE_RL:
00160 vf->priv->width = width / 2;
00161 vf->priv->in.off_left = vf->priv->width * 3;
00162 break;
00163 case ABOVE_BELOW_2_LR:
00164 d_height *= 2;
00165 case ABOVE_BELOW_LR:
00166 vf->priv->height = height / 2;
00167 vf->priv->in.row_right = vf->priv->height;
00168 break;
00169 case ABOVE_BELOW_2_RL:
00170 d_height *= 2;
00171 case ABOVE_BELOW_RL:
00172 vf->priv->height = height / 2;
00173 vf->priv->in.row_left = vf->priv->height;
00174 break;
00175 default:
00176 mp_msg(MSGT_VFILTER, MSGL_WARN,
00177 "[stereo3d] stereo format of input is not supported\n");
00178 return 0;
00179 break;
00180 }
00181
00182 vf->priv->out.width = vf->priv->width;
00183 vf->priv->out.height = vf->priv->height;
00184 vf->priv->out.off_left = 0;
00185 vf->priv->out.off_right = 0;
00186 vf->priv->out.row_left = 0;
00187 vf->priv->out.row_right = 0;
00188
00189
00190 switch (vf->priv->out.fmt) {
00191 case ANAGLYPH_RC_GRAY:
00192 case ANAGLYPH_RC_HALF:
00193 case ANAGLYPH_RC_COLOR:
00194 case ANAGLYPH_RC_DUBOIS:
00195 case ANAGLYPH_GM_GRAY:
00196 case ANAGLYPH_GM_HALF:
00197 case ANAGLYPH_GM_COLOR:
00198 case ANAGLYPH_YB_GRAY:
00199 case ANAGLYPH_YB_HALF:
00200 case ANAGLYPH_YB_COLOR:
00201 memcpy(vf->priv->ana_matrix, ana_coeff[vf->priv->out.fmt],
00202 sizeof(vf->priv->ana_matrix));
00203 break;
00204 case SIDE_BY_SIDE_2_LR:
00205 d_width /= 2;
00206 case SIDE_BY_SIDE_LR:
00207 vf->priv->out.width = vf->priv->width * 2;
00208 vf->priv->out.off_right = vf->priv->width * 3;
00209 break;
00210 case SIDE_BY_SIDE_2_RL:
00211 d_width /= 2;
00212 case SIDE_BY_SIDE_RL:
00213 vf->priv->out.width = vf->priv->width * 2;
00214 vf->priv->out.off_left = vf->priv->width * 3;
00215 break;
00216 case ABOVE_BELOW_2_LR:
00217 d_height /= 2;
00218 case ABOVE_BELOW_LR:
00219 vf->priv->out.height = vf->priv->height * 2;
00220 vf->priv->out.row_right = vf->priv->height;
00221 break;
00222 case ABOVE_BELOW_2_RL:
00223 d_height /= 2;
00224 case ABOVE_BELOW_RL:
00225 vf->priv->out.height = vf->priv->height * 2;
00226 vf->priv->out.row_left = vf->priv->height;
00227 break;
00228 case INTERLEAVE_ROWS_LR:
00229 vf->priv->row_step = 2;
00230 vf->priv->height = vf->priv->height / 2;
00231 vf->priv->out.off_right = vf->priv->width * 3;
00232 vf->priv->in.off_right += vf->priv->in.width * 3;
00233 break;
00234 case INTERLEAVE_ROWS_RL:
00235 vf->priv->row_step = 2;
00236 vf->priv->height = vf->priv->height / 2;
00237 vf->priv->out.off_left = vf->priv->width * 3;
00238 vf->priv->in.off_left += vf->priv->in.width * 3;
00239 break;
00240 case MONO_R:
00241
00242 vf->priv->in.off_left = vf->priv->in.off_right;
00243 vf->priv->in.row_left = vf->priv->in.row_right;
00244
00245 case MONO_L:
00246
00247 break;
00248 default:
00249 mp_msg(MSGT_VFILTER, MSGL_WARN,
00250 "[stereo3d] stereo format of output is not supported\n");
00251 return 0;
00252 break;
00253 }
00254
00255 d_width = d_width * vf->priv->out.width / width;
00256 d_height = d_height * vf->priv->out.height / height;
00257
00258
00259 return vf_next_config(vf, vf->priv->out.width, vf->priv->out.height,
00260 d_width, d_height, flags, outfmt);
00261 }
00262
00263 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
00264 {
00265 mp_image_t *dmpi;
00266 if (vf->priv->in.fmt == vf->priv->out.fmt) {
00267 dmpi = mpi;
00268 } else {
00269 int out_off_left, out_off_right;
00270 int in_off_left = vf->priv->in.row_left * mpi->stride[0] +
00271 vf->priv->in.off_left;
00272 int in_off_right = vf->priv->in.row_right * mpi->stride[0] +
00273 vf->priv->in.off_right;
00274
00275 dmpi = vf_get_image(vf->next, IMGFMT_RGB24, MP_IMGTYPE_TEMP,
00276 MP_IMGFLAG_ACCEPT_STRIDE,
00277 vf->priv->out.width, vf->priv->out.height);
00278 out_off_left = vf->priv->out.row_left * dmpi->stride[0] +
00279 vf->priv->out.off_left;
00280 out_off_right = vf->priv->out.row_right * dmpi->stride[0] +
00281 vf->priv->out.off_right;
00282
00283 switch (vf->priv->out.fmt) {
00284 case SIDE_BY_SIDE_LR:
00285 case SIDE_BY_SIDE_RL:
00286 case SIDE_BY_SIDE_2_LR:
00287 case SIDE_BY_SIDE_2_RL:
00288 case ABOVE_BELOW_LR:
00289 case ABOVE_BELOW_RL:
00290 case ABOVE_BELOW_2_LR:
00291 case ABOVE_BELOW_2_RL:
00292 case INTERLEAVE_ROWS_LR:
00293 case INTERLEAVE_ROWS_RL:
00294 memcpy_pic2(dmpi->planes[0] + out_off_left,
00295 mpi->planes[0] + in_off_left,
00296 3 * vf->priv->width,
00297 vf->priv->height,
00298 dmpi->stride[0] * vf->priv->row_step,
00299 mpi->stride[0] * vf->priv->row_step,
00300 vf->priv->row_step != 1);
00301 memcpy_pic2(dmpi->planes[0] + out_off_right,
00302 mpi->planes[0] + in_off_right,
00303 3 * vf->priv->width,
00304 vf->priv->height,
00305 dmpi->stride[0] * vf->priv->row_step,
00306 mpi->stride[0] * vf->priv->row_step,
00307 vf->priv->row_step != 1);
00308 break;
00309 case MONO_L:
00310 case MONO_R:
00311 memcpy_pic(dmpi->planes[0],
00312 mpi->planes[0] + in_off_left,
00313 3 * vf->priv->width,
00314 vf->priv->height,
00315 dmpi->stride[0],
00316 mpi->stride[0]);
00317 break;
00318 case ANAGLYPH_RC_GRAY:
00319 case ANAGLYPH_RC_HALF:
00320 case ANAGLYPH_RC_COLOR:
00321 case ANAGLYPH_RC_DUBOIS:
00322 case ANAGLYPH_GM_GRAY:
00323 case ANAGLYPH_GM_HALF:
00324 case ANAGLYPH_GM_COLOR:
00325 case ANAGLYPH_YB_GRAY:
00326 case ANAGLYPH_YB_HALF:
00327 case ANAGLYPH_YB_COLOR: {
00328 int i,x,y,il,ir,o;
00329 unsigned char *source = mpi->planes[0];
00330 unsigned char *dest = dmpi->planes[0];
00331 unsigned int out_width = vf->priv->out.width;
00332 int *ana_matrix[3];
00333
00334 for(i = 0; i < 3; i++)
00335 ana_matrix[i] = vf->priv->ana_matrix[i];
00336
00337 for (y = 0; y < vf->priv->out.height; y++) {
00338 o = dmpi->stride[0] * y;
00339 il = in_off_left + y * mpi->stride[0];
00340 ir = in_off_right + y * mpi->stride[0];
00341 for (x = 0; x < out_width; x++) {
00342 dest[o ] = ana_convert(
00343 ana_matrix[0], source + il, source + ir);
00344 dest[o + 1] = ana_convert(
00345 ana_matrix[1], source + il, source + ir);
00346 dest[o + 2] = ana_convert(
00347 ana_matrix[2], source + il, source + ir);
00348 il += 3;
00349 ir += 3;
00350 o += 3;
00351 }
00352 }
00353 break;
00354 }
00355 default:
00356 mp_msg(MSGT_VFILTER, MSGL_WARN,
00357 "[stereo3d] stereo format of output is not supported\n");
00358 return 0;
00359 break;
00360 }
00361 }
00362 return vf_next_put_image(vf, dmpi, pts);
00363 }
00364
00365 static int query_format(struct vf_instance *vf, unsigned int fmt)
00366 {
00367 switch (fmt)
00368 case IMGFMT_RGB24:
00369 return vf_next_query_format(vf, fmt);
00370 return 0;
00371 }
00372
00373 static void uninit(vf_instance_t *vf)
00374 {
00375 free(vf->priv);
00376 }
00377
00378 static int vf_open(vf_instance_t *vf, char *args)
00379 {
00380 vf->config = config;
00381 vf->uninit = uninit;
00382 vf->put_image = put_image;
00383 vf->query_format = query_format;
00384 vf->priv=malloc(sizeof(struct vf_priv_s));
00385 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00386
00387 vf->priv->in.fmt = SIDE_BY_SIDE_LR;
00388 vf->priv->out.fmt= ANAGLYPH_RC_DUBOIS;
00389 if (args) sscanf(args, "%d:%d", &vf->priv->in.fmt, &vf->priv->out.fmt);
00390
00391 return 1;
00392 }
00393 #if 0
00395 static const struct format_preset {
00396 char* name;
00397 stereo_code scode;
00398 } vf_format_presets_defs[] = {
00399 {"arcg", ANAGLYPH_RC_GRAY},
00400 {"anaglyph_red_cyan_gray", ANAGLYPH_RC_GRAY},
00401 {"arch", ANAGLYPH_RC_HALF},
00402 {"anaglyph_red_cyan_half_color", ANAGLYPH_RC_HALF},
00403 {"arcc", ANAGLYPH_RC_COLOR},
00404 {"anaglyph_red_cyan_color", ANAGLYPH_RC_COLOR},
00405 {"arcd", ANAGLYPH_RC_DUBOIS},
00406 {"anaglyph_red_cyan_dubios", ANAGLYPH_RC_DUBOIS},
00407 {"agmg", ANAGLYPH_GM_GRAY},
00408 {"anaglyph_green_magenta_gray", ANAGLYPH_GM_GRAY},
00409 {"agmh", ANAGLYPH_GM_HALF},
00410 {"anaglyph_green_magenta_half_color",ANAGLYPH_GM_HALF},
00411 {"agmc", ANAGLYPH_GM_COLOR},
00412 {"anaglyph_green_magenta_color", ANAGLYPH_GM_COLOR},
00413 {"aybg", ANAGLYPH_YB_GRAY},
00414 {"anaglyph_yellow_blue_gray", ANAGLYPH_YB_GRAY},
00415 {"aybh", ANAGLYPH_YB_HALF},
00416 {"anaglyph_yellow_blue_half_color", ANAGLYPH_YB_HALF},
00417 {"aybc", ANAGLYPH_YB_COLOR},
00418 {"anaglyph_yellow_blue_color", ANAGLYPH_YB_COLOR},
00419 {"ml", MONO_L},
00420 {"mono_left", MONO_L},
00421 {"mr", MONO_R},
00422 {"mono_right", MONO_R},
00423 {"sbsl", SIDE_BY_SIDE_LR},
00424 {"side_by_side_left_first", SIDE_BY_SIDE_LR},
00425 {"sbsr", SIDE_BY_SIDE_RL},
00426 {"side_by_side_right_first", SIDE_BY_SIDE_RL},
00427 {"sbs2l", SIDE_BY_SIDE_2_LR},
00428 {"side_by_side_half_width_left_first", SIDE_BY_SIDE_2_LR},
00429 {"sbs2r", SIDE_BY_SIDE_2_RL},
00430 {"side_by_side_half_width_right_first",SIDE_BY_SIDE_2_RL},
00431 {"abl", ABOVE_BELOW_LR},
00432 {"above_below_left_first", ABOVE_BELOW_LR},
00433 {"abr", ABOVE_BELOW_RL},
00434 {"above_below_right_first", ABOVE_BELOW_RL},
00435 {"ab2l", ABOVE_BELOW_2_LR},
00436 {"above_below_half_height_left_first", ABOVE_BELOW_2_LR},
00437 {"ab2r", ABOVE_BELOW_2_RL},
00438 {"above_below_half_height_right_first",ABOVE_BELOW_2_RL},
00439 {"irl", INTERLEAVE_ROWS_LR},
00440 {"interleave_rows_left_first", INTERLEAVE_ROWS_LR},
00441 {"irr", INTERLEAVE_ROWS_RL},
00442 {"interleave_rows_right_first", INTERLEAVE_ROWS_RL},
00443 { NULL, 0}
00444 };
00445
00446 #define ST_OFF(f) M_ST_OFF(struct format_preset,f)
00447 static const m_option_t vf_format_preset_fields_in[] = {
00448 {"in", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
00449 { NULL, NULL, 0, 0, 0, 0, NULL }
00450 };
00451 static const m_option_t vf_format_preset_fields_out[] = {
00452 {"out", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
00453 { NULL, NULL, 0, 0, 0, 0, NULL }
00454 };
00455
00456 static const m_struct_t vf_format_preset_in = {
00457 "stereo_format_preset_in",
00458 sizeof(struct format_preset),
00459 NULL,
00460 vf_format_preset_fields_in
00461 };
00462 static const m_struct_t vf_format_preset_out = {
00463 "stereo_format_preset_out",
00464 sizeof(struct format_preset),
00465 NULL,
00466 vf_format_preset_fields_out
00467 };
00468
00469 static const m_struct_t vf_opts;
00470 static const m_obj_presets_t format_preset_in = {
00471 (struct m_struct_st*)&vf_format_preset_in,
00472 (struct m_struct_st*)&vf_opts,
00473 (struct format_preset*)vf_format_presets_defs,
00474 ST_OFF(name)
00475 };
00476 static const m_obj_presets_t format_preset_out = {
00477 (struct m_struct_st*)&vf_format_preset_out,
00478 (struct m_struct_st*)&vf_opts,
00479 (struct format_preset*)vf_format_presets_defs,
00480 ST_OFF(name)
00481 };
00482
00484 #undef ST_OFF
00485 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
00486 static const m_option_t vf_opts_fields[] = {
00487 {"stereo_in", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
00488 (m_obj_presets_t*)&format_preset_in},
00489 {"stereo_out", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
00490 (m_obj_presets_t*)&format_preset_out},
00491 {"in", ST_OFF(in.fmt), CONF_TYPE_INT, 0,0,0, NULL},
00492 {"out", ST_OFF(out.fmt), CONF_TYPE_INT, 0,0,0, NULL},
00493 { NULL, NULL, 0, 0, 0, 0, NULL }
00494 };
00495
00496 static const m_struct_t vf_opts = {
00497 "stereo3d",
00498 sizeof(struct vf_priv_s),
00499 &vf_priv_default,
00500 vf_opts_fields
00501 };
00502 #endif
00503
00504
00505 const vf_info_t vf_info_stereo3d = {
00506 "stereoscopic 3d view",
00507 "stereo3d",
00508 "Gordon Schmidt",
00509 "view stereoscopic videos",
00510 vf_open,
00511
00512 };