Mercurial > mplayer.hg
annotate libmpcodecs/vf_stereo3d.c @ 32502:9705236ce079
Change code to something understandable (but equivalent).
author | reimar |
---|---|
date | Sat, 06 Nov 2010 19:33:30 +0000 |
parents | 7ec524214684 |
children | 28f08e7c2a0a |
rev | line source |
---|---|
32441 | 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" | |
32445
7ec524214684
Use proper include instead of extern declarations in the .c file.
reimar
parents:
32441
diff
changeset
|
32 #include "vd.h" |
32441 | 33 #include "vf.h" |
34 #include "m_struct.h" | |
35 | |
36 #include "libavutil/common.h" | |
37 #include "libvo/fastmemcpy.h" | |
38 | |
39 //==types==// | |
40 typedef enum stereo_code { | |
41 ANAGLYPH_RC_GRAY, //anaglyph red/cyan gray | |
42 ANAGLYPH_RC_HALF, //anaglyph red/cyan half colored | |
43 ANAGLYPH_RC_COLOR, //anaglyph red/cyan colored | |
44 ANAGLYPH_RC_DUBOIS, //anaglyph red/cyan dubois | |
45 ANAGLYPH_GM_GRAY, //anaglyph green/magenta gray | |
46 ANAGLYPH_GM_HALF, //anaglyph green/magenta half colored | |
47 ANAGLYPH_GM_COLOR, //anaglyph green/magenta colored | |
48 ANAGLYPH_YB_GRAY, //anaglyph yellow/blue gray | |
49 ANAGLYPH_YB_HALF, //anaglyph yellow/blue half colored | |
50 ANAGLYPH_YB_COLOR, //anaglyph yellow/blue colored | |
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 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 //no value set - TODO: needs autodetection | |
60 } stereo_code; | |
61 | |
62 typedef struct component { | |
63 stereo_code fmt; | |
64 unsigned int width; | |
65 unsigned int height; | |
66 unsigned int off_left; | |
67 unsigned int off_right; | |
68 unsigned int stride; | |
69 unsigned int hres; | |
70 } component; | |
71 | |
72 //==global variables==// | |
73 static const int ana_coeff[10][3][6] = { | |
74 {{19595, 38470, 7471, 0, 0, 0}, //ANAGLYPH_RC_GRAY | |
75 { 0, 0, 0, 19595, 38470, 7471}, | |
76 { 0, 0, 0, 19595, 38470, 7471}}, | |
77 {{19595, 38470, 7471, 0, 0, 0}, //ANAGLYPH_RC_HALF | |
78 { 0, 0, 0, 0, 65536, 0}, | |
79 { 0, 0, 0, 0, 0, 65536}}, | |
80 {{65536, 0, 0, 0, 0, 0}, //ANAGLYPH_RC_COLOR | |
81 { 0, 0, 0, 0, 65536, 0}, | |
82 { 0, 0, 0, 0, 0, 65536}}, | |
83 {{29891, 32800, 11559, -2849, -5763, -102}, //ANAGLYPH_RC_DUBOIS | |
84 {-2627, -2479, -1033, 24804, 48080, -1209}, | |
85 { -997, -1350, -358, -4729, -7403, 80373}}, | |
86 {{ 0, 0, 0, 19595, 38470, 7471}, //ANAGLYPH_GM_GRAY | |
87 {19595, 38470, 7471, 0, 0, 0}, | |
88 { 0, 0, 0, 19595, 38470, 7471}}, | |
89 {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_GM_HALF | |
90 {19595, 38470, 7471, 0, 0, 0}, | |
91 { 0, 0, 0, 0, 0, 65536}}, | |
92 {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_GM_COLOR | |
93 { 0, 65536, 0, 0, 0, 0}, | |
94 { 0, 0, 0, 0, 0, 65536}}, | |
95 {{ 0, 0, 0, 19595, 38470, 7471}, //ANAGLYPH_YB_GRAY | |
96 { 0, 0, 0, 19595, 38470, 7471}, | |
97 {19595, 38470, 7471, 0, 0, 0}}, | |
98 {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_YB_HALF | |
99 { 0, 0, 0, 0, 65536, 0}, | |
100 {19595, 38470, 7471, 0, 0, 0}}, | |
101 {{ 0, 0, 0, 65536, 0, 0}, //ANAGLYPH_YB_COLOR | |
102 { 0, 0, 0, 0, 65536, 0}, | |
103 { 0, 0, 65536, 0, 0, 0}} | |
104 }; | |
105 | |
106 struct vf_priv_s { | |
107 component in; | |
108 component out; | |
109 int ana_matrix[3][6]; | |
110 unsigned int width; | |
111 unsigned int height; | |
112 } const vf_priv_default = { | |
113 {SIDE_BY_SIDE_LR}, | |
114 {ANAGLYPH_RC_DUBOIS} | |
115 }; | |
116 | |
117 //==functions==// | |
118 static inline uint8_t ana_convert(int coeff[6], uint8_t left[3], uint8_t right[3]) | |
119 { | |
120 int sum; | |
121 | |
122 sum = coeff[0] * left[0] + coeff[3] * right[0]; //red in | |
123 sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in | |
124 sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in | |
125 return av_clip_uint8(sum >> 16); | |
126 } | |
127 | |
128 static int config(struct vf_instance *vf, int width, int height, int d_width, | |
129 int d_height, unsigned int flags, unsigned int outfmt) | |
130 { | |
131 if ((width & 1) || (height & 1)) { | |
132 mp_msg(MSGT_VFILTER, MSGL_WARN, "[stereo3d] invalid height or width\n"); | |
133 return 0; | |
134 } | |
135 //default input values | |
136 vf->priv->width = width; | |
137 vf->priv->height = height; | |
138 vf->priv->in.width = width; | |
139 vf->priv->in.height = height; | |
140 vf->priv->in.hres = 1; | |
141 vf->priv->in.off_left = 0; | |
142 vf->priv->in.off_right = 0; | |
143 vf->priv->in.stride = vf->priv->width * 3; | |
144 | |
145 //check input format | |
146 switch (vf->priv->in.fmt) { | |
147 case SIDE_BY_SIDE_LR: | |
148 vf->priv->width = width / 2; | |
149 vf->priv->in.off_right = vf->priv->width * 3; | |
150 vf->priv->in.stride = vf->priv->width * 6; | |
151 break; | |
152 case SIDE_BY_SIDE_RL: | |
153 vf->priv->width = width / 2; | |
154 vf->priv->in.off_left = vf->priv->width * 3; | |
155 vf->priv->in.stride = vf->priv->width * 6; | |
156 break; | |
157 case ABOVE_BELOW_LR: | |
158 vf->priv->height = height / 2; | |
159 vf->priv->in.off_right = vf->priv->width * vf->priv->height * 3; | |
160 break; | |
161 case ABOVE_BELOW_RL: | |
162 vf->priv->height = height / 2; | |
163 vf->priv->in.off_left = vf->priv->width * vf->priv->height * 3; | |
164 break; | |
165 case ABOVE_BELOW_2_LR: | |
166 vf->priv->in.hres = 2; | |
167 vf->priv->in.off_right = vf->priv->width * vf->priv->height / 2 * 3; | |
168 break; | |
169 case ABOVE_BELOW_2_RL: | |
170 vf->priv->in.hres = 2; | |
171 vf->priv->in.off_left = vf->priv->width * vf->priv->height / 2 * 3; | |
172 break; | |
173 default: | |
174 mp_msg(MSGT_VFILTER, MSGL_WARN, | |
175 "[stereo3d] stereo format of input is not supported\n"); | |
176 return 0; | |
177 break; | |
178 } | |
179 //default output values | |
180 vf->priv->out.width = vf->priv->width; | |
181 vf->priv->out.height = vf->priv->height; | |
182 vf->priv->out.hres = 1; | |
183 vf->priv->out.off_left = 0; | |
184 vf->priv->out.off_right = 0; | |
185 vf->priv->out.stride = vf->priv->width * 3; | |
186 | |
187 //check output format | |
188 switch (vf->priv->out.fmt) { | |
189 case ANAGLYPH_RC_GRAY: | |
190 case ANAGLYPH_RC_HALF: | |
191 case ANAGLYPH_RC_COLOR: | |
192 case ANAGLYPH_RC_DUBOIS: | |
193 case ANAGLYPH_GM_GRAY: | |
194 case ANAGLYPH_GM_HALF: | |
195 case ANAGLYPH_GM_COLOR: | |
196 case ANAGLYPH_YB_GRAY: | |
197 case ANAGLYPH_YB_HALF: | |
198 case ANAGLYPH_YB_COLOR: | |
199 memcpy(vf->priv->ana_matrix, ana_coeff[vf->priv->out.fmt], | |
200 sizeof(vf->priv->ana_matrix)); | |
201 break; | |
202 case SIDE_BY_SIDE_LR: | |
203 vf->priv->out.width = vf->priv->width * 2; | |
204 vf->priv->out.off_right = vf->priv->width * 3; | |
205 vf->priv->out.stride = vf->priv->width * 6; | |
206 break; | |
207 case SIDE_BY_SIDE_RL: | |
208 vf->priv->out.width = vf->priv->width * 2; | |
209 vf->priv->out.off_left = vf->priv->width * 3; | |
210 vf->priv->out.stride = vf->priv->width * 6; | |
211 break; | |
212 case ABOVE_BELOW_LR: | |
213 vf->priv->out.height = vf->priv->height * 2; | |
214 vf->priv->out.off_right = vf->priv->width * vf->priv->height * 3; | |
215 break; | |
216 case ABOVE_BELOW_RL: | |
217 vf->priv->out.height = vf->priv->height * 2; | |
218 vf->priv->out.off_left = vf->priv->width * vf->priv->height * 3; | |
219 break; | |
220 case ABOVE_BELOW_2_LR: | |
221 vf->priv->out.hres = 2; | |
222 vf->priv->out.off_right = vf->priv->width * vf->priv->height / 2 * 3; | |
223 break; | |
224 case ABOVE_BELOW_2_RL: | |
225 vf->priv->out.hres = 2; | |
226 vf->priv->out.off_left = vf->priv->width * vf->priv->height / 2 * 3; | |
227 break; | |
228 case MONO_R: | |
229 //same as MONO_L only needs switching of input offsets | |
230 vf->priv->in.off_left = vf->priv->in.off_right; | |
231 //nobreak; | |
232 case MONO_L: | |
233 //use default settings | |
234 break; | |
235 default: | |
236 mp_msg(MSGT_VFILTER, MSGL_WARN, | |
237 "[stereo3d] stereo format of output is not supported\n"); | |
238 return 0; | |
239 break; | |
240 } | |
241 if (!opt_screen_size_x && !opt_screen_size_y) { | |
242 d_width = d_width * vf->priv->out.width / width; | |
243 d_height = d_height * vf->priv->out.height / height; | |
244 } | |
245 return vf_next_config(vf, vf->priv->out.width, vf->priv->out.height, | |
246 d_width, d_height, flags, outfmt); | |
247 } | |
248 | |
249 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts) | |
250 { | |
251 mp_image_t *dmpi; | |
252 if (vf->priv->in.fmt == vf->priv->out.fmt) { //nothing to do | |
253 dmpi = mpi; | |
254 } else { | |
255 dmpi = vf_get_image(vf->next, IMGFMT_RGB24, MP_IMGTYPE_TEMP, 0, | |
256 vf->priv->out.width, vf->priv->out.height); | |
257 dmpi->h = vf->priv->out.height; | |
258 dmpi->width = vf->priv->out.width; | |
259 switch (vf->priv->out.fmt) { | |
260 case SIDE_BY_SIDE_LR: | |
261 case SIDE_BY_SIDE_RL: | |
262 case ABOVE_BELOW_LR: | |
263 case ABOVE_BELOW_RL: | |
264 case ABOVE_BELOW_2_LR: | |
265 case ABOVE_BELOW_2_RL: | |
266 for (int i = 0; i < vf->priv->in.hres; i++) { | |
267 memcpy_pic(dmpi->planes[0] + vf->priv->out.off_left + | |
268 (i * vf->priv->out.stride), mpi->planes[0] + | |
269 vf->priv->in.off_left, 3 * vf->priv->width, | |
270 vf->priv->height / (vf->priv->in.hres * | |
271 vf->priv->out.hres), vf->priv->out.stride * | |
272 vf->priv->in.hres, vf->priv->in.stride * | |
273 vf->priv->out.hres); | |
274 memcpy_pic(dmpi->planes[0] + vf->priv->out.off_right + | |
275 (i * vf->priv->out.stride), mpi->planes[0] + | |
276 vf->priv->in.off_right, 3 * vf->priv->width, | |
277 vf->priv->height / (vf->priv->in.hres * | |
278 vf->priv->out.hres), vf->priv->out.stride * | |
279 vf->priv->in.hres, vf->priv->in.stride * | |
280 vf->priv->out.hres); | |
281 } | |
282 break; | |
283 case MONO_L: | |
284 case MONO_R: | |
285 for (int i = 0; i < vf->priv->in.hres; i++) { | |
286 memcpy_pic(dmpi->planes[0] + (i * vf->priv->out.stride), | |
287 mpi->planes[0] + vf->priv->in.off_left, 3 * | |
288 vf->priv->width, vf->priv->height / | |
289 vf->priv->in.hres, vf->priv->out.stride * | |
290 vf->priv->in.hres, vf->priv->in.stride); | |
291 } | |
292 break; | |
293 case ANAGLYPH_RC_GRAY: | |
294 case ANAGLYPH_RC_HALF: | |
295 case ANAGLYPH_RC_COLOR: | |
296 case ANAGLYPH_RC_DUBOIS: | |
297 case ANAGLYPH_GM_GRAY: | |
298 case ANAGLYPH_GM_HALF: | |
299 case ANAGLYPH_GM_COLOR: | |
300 case ANAGLYPH_YB_GRAY: | |
301 case ANAGLYPH_YB_HALF: | |
302 case ANAGLYPH_YB_COLOR: { | |
303 int x,y,il,ir,o; | |
304 unsigned char *source = mpi->planes[0]; | |
305 unsigned char *dest = dmpi->planes[0]; | |
306 unsigned int out_width = vf->priv->out.width; | |
307 int *ana_matrix[3]; | |
308 | |
309 for(int i = 0; i < 3; i++) | |
310 ana_matrix[i] = vf->priv->ana_matrix[i]; | |
311 | |
312 for (y = 0; y < vf->priv->out.height; y++) { | |
313 o = vf->priv->out.stride * y; | |
314 il = vf->priv->in.off_left + (y / vf->priv->in.hres) * | |
315 vf->priv->in.stride; | |
316 ir = vf->priv->in.off_right + (y / vf->priv->in.hres) * | |
317 vf->priv->in.stride; | |
318 for (x = 0; x < out_width; x++) { | |
319 dest[o ] = ana_convert( | |
320 ana_matrix[0], source + il, source + ir); //red out | |
321 dest[o + 1] = ana_convert( | |
322 ana_matrix[1], source + il, source + ir); //green out | |
323 dest[o + 2] = ana_convert( | |
324 ana_matrix[2], source + il, source + ir); //blue out | |
325 il += 3; | |
326 ir += 3; | |
327 o += 3; | |
328 } | |
329 } | |
330 break; | |
331 } | |
332 default: | |
333 mp_msg(MSGT_VFILTER, MSGL_WARN, | |
334 "[stereo3d] stereo format of output is not supported\n"); | |
335 return 0; | |
336 break; | |
337 } | |
338 } | |
339 return vf_next_put_image(vf, dmpi, pts); | |
340 } | |
341 | |
342 static int query_format(struct vf_instance *vf, unsigned int fmt) | |
343 { | |
344 switch (fmt) | |
345 case IMGFMT_RGB24: | |
346 return vf_next_query_format(vf, fmt); | |
347 return 0; | |
348 } | |
349 | |
350 static void uninit(vf_instance_t *vf) | |
351 { | |
352 free(vf->priv); | |
353 } | |
354 | |
355 static int vf_open(vf_instance_t *vf, char *args) | |
356 { | |
357 vf->config = config; | |
358 vf->uninit = uninit; | |
359 vf->put_image = put_image; | |
360 vf->query_format = query_format; | |
361 vf->default_reqs = VFCAP_ACCEPT_STRIDE; | |
362 | |
363 return 1; | |
364 } | |
365 | |
366 ///Presets usage | |
367 static const struct format_preset { | |
368 char* name; | |
369 stereo_code scode; | |
370 } vf_format_presets_defs[] = { | |
371 {"arcg", ANAGLYPH_RC_GRAY}, | |
372 {"anaglyph_red_cyan_gray", ANAGLYPH_RC_GRAY}, | |
373 {"arch", ANAGLYPH_RC_HALF}, | |
374 {"anaglyph_red_cyan_half_color", ANAGLYPH_RC_HALF}, | |
375 {"arcc", ANAGLYPH_RC_COLOR}, | |
376 {"anaglyph_red_cyan_color", ANAGLYPH_RC_COLOR}, | |
377 {"arcd", ANAGLYPH_RC_DUBOIS}, | |
378 {"anaglyph_red_cyan_dubios", ANAGLYPH_RC_DUBOIS}, | |
379 {"agmg", ANAGLYPH_GM_GRAY}, | |
380 {"anaglyph_green_magenta_gray", ANAGLYPH_GM_GRAY}, | |
381 {"agmh", ANAGLYPH_GM_HALF}, | |
382 {"anaglyph_green_magenta_half_color",ANAGLYPH_GM_HALF}, | |
383 {"agmc", ANAGLYPH_GM_COLOR}, | |
384 {"anaglyph_green_magenta_color", ANAGLYPH_GM_COLOR}, | |
385 {"aybg", ANAGLYPH_YB_GRAY}, | |
386 {"anaglyph_yellow_blue_gray", ANAGLYPH_YB_GRAY}, | |
387 {"aybh", ANAGLYPH_YB_HALF}, | |
388 {"anaglyph_yellow_blue_half_color", ANAGLYPH_YB_HALF}, | |
389 {"aybc", ANAGLYPH_YB_COLOR}, | |
390 {"anaglyph_yellow_blue_color", ANAGLYPH_YB_COLOR}, | |
391 {"ml", MONO_L}, | |
392 {"mono_left", MONO_L}, | |
393 {"mr", MONO_R}, | |
394 {"mono_right", MONO_R}, | |
395 {"sbsl", SIDE_BY_SIDE_LR}, | |
396 {"side_by_side_left_first", SIDE_BY_SIDE_LR}, | |
397 {"sbsr", SIDE_BY_SIDE_RL}, | |
398 {"side_by_side_right_first", SIDE_BY_SIDE_RL}, | |
399 {"abl", ABOVE_BELOW_LR}, | |
400 {"above_below_left_first", ABOVE_BELOW_LR}, | |
401 {"abr", ABOVE_BELOW_RL}, | |
402 {"above_below_right_first", ABOVE_BELOW_RL}, | |
403 {"ab2l", ABOVE_BELOW_2_LR}, | |
404 {"above_below_half_height_left_first", ABOVE_BELOW_2_LR}, | |
405 {"ab2r", ABOVE_BELOW_2_RL}, | |
406 {"above_below_half_height_right_first",ABOVE_BELOW_2_RL}, | |
407 { NULL, 0} | |
408 }; | |
409 | |
410 #define ST_OFF(f) M_ST_OFF(struct format_preset,f) | |
411 static const m_option_t vf_format_preset_fields_in[] = { | |
412 {"in", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL}, | |
413 { NULL, NULL, 0, 0, 0, 0, NULL } | |
414 }; | |
415 static const m_option_t vf_format_preset_fields_out[] = { | |
416 {"out", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL}, | |
417 { NULL, NULL, 0, 0, 0, 0, NULL } | |
418 }; | |
419 | |
420 static const m_struct_t vf_format_preset_in = { | |
421 "stereo_format_preset_in", | |
422 sizeof(struct format_preset), | |
423 NULL, | |
424 vf_format_preset_fields_in | |
425 }; | |
426 static const m_struct_t vf_format_preset_out = { | |
427 "stereo_format_preset_out", | |
428 sizeof(struct format_preset), | |
429 NULL, | |
430 vf_format_preset_fields_out | |
431 }; | |
432 | |
433 static const m_struct_t vf_opts; | |
434 static const m_obj_presets_t format_preset_in = { | |
435 (struct m_struct_st*)&vf_format_preset_in, | |
436 (struct m_struct_st*)&vf_opts, | |
437 (struct format_preset*)vf_format_presets_defs, | |
438 ST_OFF(name) | |
439 }; | |
440 static const m_obj_presets_t format_preset_out = { | |
441 (struct m_struct_st*)&vf_format_preset_out, | |
442 (struct m_struct_st*)&vf_opts, | |
443 (struct format_preset*)vf_format_presets_defs, | |
444 ST_OFF(name) | |
445 }; | |
446 | |
447 /// Now the options | |
448 #undef ST_OFF | |
449 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f) | |
450 static const m_option_t vf_opts_fields[] = { | |
451 {"stereo_in", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, | |
452 (m_obj_presets_t*)&format_preset_in}, | |
453 {"stereo_out", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0, | |
454 (m_obj_presets_t*)&format_preset_out}, | |
455 {"in", ST_OFF(in.fmt), CONF_TYPE_INT, 0,0,0, NULL}, | |
456 {"out", ST_OFF(out.fmt), CONF_TYPE_INT, 0,0,0, NULL}, | |
457 { NULL, NULL, 0, 0, 0, 0, NULL } | |
458 }; | |
459 | |
460 static const m_struct_t vf_opts = { | |
461 "stereo3d", | |
462 sizeof(struct vf_priv_s), | |
463 &vf_priv_default, | |
464 vf_opts_fields | |
465 }; | |
466 | |
467 | |
468 //==info struct==// | |
469 const vf_info_t vf_info_stereo3d = { | |
470 "stereoscopic 3d view", | |
471 "stereo3d", | |
472 "Gordon Schmidt", | |
473 "view stereoscopic videos", | |
474 vf_open, | |
475 &vf_opts | |
476 }; |