Merge svn changes up to r30448
[mplayer/glamo.git] / libvo / vo_yuv4mpeg.c
blob0cc7f9a62cba2a5046a9953cafeea98152b7d0da
1 /*
2 * yuv4mpeg (mjpegtools) interface
4 * Thrown together by Robert Kesterson <robertk@robertk.com>
5 * Based on the pgm output plugin, the rgb2rgb postproc filter, divxdec,
6 * and probably others.
8 * This is undoubtedly incomplete, inaccurate, or just plain wrong. :-)
10 * 2002/06/19 Klaus Stengel <Klaus.Stengel@asamnet.de>
11 * - added support for interlaced output
12 * Activate by using '-vo yuv4mpeg:interlaced'
13 * or '-vo yuv4mpeg:interlaced_bf' if your source has
14 * bottom fields first
15 * - added some additional checks to catch problems
17 * 2002/04/17 Juergen Hammelmann <juergen.hammelmann@gmx.de>
18 * - added support for output of subtitles
19 * best, if you give option '-osdlevel 0' to mplayer for
20 * no watching the seek+timer
22 * This file is part of MPlayer.
24 * MPlayer is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * MPlayer is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License along
35 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <limits.h>
47 #include "config.h"
48 #include "subopt-helper.h"
49 #include "video_out.h"
50 #include "video_out_internal.h"
52 #include "mp_msg.h"
53 #include "help_mp.h"
55 #include "sub.h"
57 #include "fastmemcpy.h"
58 #include "libswscale/swscale.h"
59 #ifdef CONFIG_LIBSWSCALE_INTERNALS
60 #include "libswscale/rgb2rgb.h"
61 #endif
62 #include "libmpcodecs/vf_scale.h"
63 #include "libavutil/rational.h"
65 static const vo_info_t info =
67 "yuv4mpeg output for mjpegtools",
68 "yuv4mpeg",
69 "Robert Kesterson <robertk@robertk.com>",
73 const LIBVO_EXTERN (yuv4mpeg)
75 static int image_width = 0;
76 static int image_height = 0;
77 static float image_fps = 0;
79 static uint8_t *image = NULL;
80 static uint8_t *image_y = NULL;
81 static uint8_t *image_u = NULL;
82 static uint8_t *image_v = NULL;
84 static uint8_t *rgb_buffer = NULL;
85 static uint8_t *rgb_line_buffer = NULL;
87 static char *yuv_filename = NULL;
89 static int using_format = 0;
90 static FILE *yuv_out;
91 static int write_bytes;
93 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
94 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
95 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
97 /* Set progressive mode as default */
98 static int config_interlace = Y4M_ILACE_NONE;
99 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
101 static int config(uint32_t width, uint32_t height, uint32_t d_width,
102 uint32_t d_height, uint32_t flags, char *title,
103 uint32_t format)
105 AVRational pixelaspect = av_div_q((AVRational){d_width, d_height},
106 (AVRational){width, height});
107 AVRational fps_frac = av_d2q(vo_fps, vo_fps * 1001 + 2);
108 if (image_width == width && image_height == height &&
109 image_fps == vo_fps && vo_config_count)
110 return 0;
111 if (vo_config_count) {
112 mp_msg(MSGT_VO, MSGL_WARN,
113 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
114 "restarting output.\n",
115 image_width, width, image_height, height, image_fps, vo_fps);
116 uninit();
118 image_height = height;
119 image_width = width;
120 image_fps = vo_fps;
121 using_format = format;
123 if (Y4M_IS_INTERLACED)
125 if (height % 4)
127 mp_tmsg(MSGT_VO,MSGL_FATAL,
128 "Interlaced mode requires image height to be divisible by 4.");
129 return -1;
132 rgb_line_buffer = malloc(image_width * 3);
133 if (!rgb_line_buffer)
135 mp_tmsg(MSGT_VO,MSGL_FATAL,
136 "Unable to allocate line buffer for interlaced mode.");
137 return -1;
140 if (using_format == IMGFMT_YV12)
141 mp_tmsg(MSGT_VO,MSGL_WARN,
142 "Input not RGB, can't separate chrominance by fields!");
145 if (width % 2)
147 mp_tmsg(MSGT_VO,MSGL_FATAL,
148 "Image width must be divisible by 2.");
149 return -1;
152 #ifdef CONFIG_LIBSWSCALE_INTERNALS
153 if(using_format != IMGFMT_YV12)
155 sws_rgb2rgb_init(get_sws_cpuflags());
156 rgb_buffer = malloc(image_width * image_height * 3);
157 if (!rgb_buffer)
159 mp_tmsg(MSGT_VO,MSGL_FATAL,
160 "Not enough memory to allocate RGB framebuffer.");
161 return -1;
164 #endif
166 write_bytes = image_width * image_height * 3 / 2;
167 image = malloc(write_bytes);
169 yuv_out = fopen(yuv_filename, "wb");
170 if (!yuv_out || image == 0)
172 mp_tmsg(MSGT_VO,MSGL_FATAL,
173 "Can't get memory or file handle to write \"%s\"!",
174 yuv_filename);
175 return -1;
177 image_y = image;
178 image_u = image_y + image_width * image_height;
179 image_v = image_u + image_width * image_height / 4;
181 fprintf(yuv_out, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
182 image_width, image_height, fps_frac.num, fps_frac.den,
183 config_interlace,
184 pixelaspect.num, pixelaspect.den);
186 fflush(yuv_out);
187 return 0;
190 /* Only use when h divisable by 2! */
191 static void swap_fields(uint8_t *ptr, const int h, const int stride)
193 int i;
195 for (i=0; i<h; i +=2)
197 fast_memcpy(rgb_line_buffer , ptr + stride * i , stride);
198 fast_memcpy(ptr + stride * i , ptr + stride * (i+1), stride);
199 fast_memcpy(ptr + stride * (i+1), rgb_line_buffer , stride);
203 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
204 unsigned char *srca, int stride) {
205 switch (using_format)
207 case IMGFMT_YV12:
208 vo_draw_alpha_yv12(w, h, src, srca, stride,
209 image + y0 * image_width + x0, image_width);
210 break;
212 case IMGFMT_BGR|24:
213 case IMGFMT_RGB|24:
214 if (config_interlace != Y4M_ILACE_BOTTOM_FIRST)
215 vo_draw_alpha_rgb24(w, h, src, srca, stride,
216 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
217 else
219 swap_fields (rgb_buffer, image_height, image_width * 3);
221 vo_draw_alpha_rgb24(w, h, src, srca, stride,
222 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
224 swap_fields (rgb_buffer, image_height, image_width * 3);
226 break;
230 static void draw_osd(void)
232 vo_draw_text(image_width, image_height, draw_alpha);
235 #ifdef CONFIG_LIBSWSCALE_INTERNALS
236 static void deinterleave_fields(uint8_t *ptr, const int stride,
237 const int img_height)
239 unsigned int i, j, k_start = 1, modv = img_height - 1;
240 unsigned char *line_state = malloc(modv);
242 for (i=0; i<modv; i++)
243 line_state[i] = 0;
245 line_state[0] = 1;
247 while(k_start < modv)
249 i = j = k_start;
250 fast_memcpy(rgb_line_buffer, ptr + stride * i, stride);
252 while (!line_state[j])
254 line_state[j] = 1;
255 i = j;
256 j = j * 2 % modv;
257 fast_memcpy(ptr + stride * i, ptr + stride * j, stride);
259 fast_memcpy(ptr + stride * i, rgb_line_buffer, stride);
261 while(k_start < modv && line_state[k_start])
262 k_start++;
264 free(line_state);
266 #endif
268 static void vo_y4m_write(const void *ptr, const size_t num_bytes)
270 if (fwrite(ptr, 1, num_bytes, yuv_out) != num_bytes)
271 mp_tmsg(MSGT_VO,MSGL_ERR,
272 "Error writing image to output!");
275 static int write_last_frame(void)
278 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
279 int rgb_stride, uv_stride, field_height;
280 unsigned int i, low_ofs;
282 fprintf(yuv_out, "FRAME\n");
284 if (using_format != IMGFMT_YV12)
286 rgb_stride = image_width * 3;
287 uv_stride = image_width / 2;
289 if (Y4M_IS_INTERLACED)
291 field_height = image_height / 2;
293 upper_y = image;
294 upper_u = upper_y + image_width * field_height;
295 upper_v = upper_u + image_width * field_height / 4;
296 low_ofs = image_width * field_height * 3 / 2;
297 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
299 /* Write Y plane */
300 for(i = 0; i < field_height; i++)
302 vo_y4m_write(upper_y + image_width * i, image_width);
303 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
306 /* Write U and V plane */
307 for(i = 0; i < field_height / 2; i++)
309 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
310 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
312 for(i = 0; i < field_height / 2; i++)
314 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
315 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
317 return VO_TRUE; /* Image written; We have to stop here */
320 /* Write progressive frame */
321 vo_y4m_write(image, write_bytes);
322 return VO_TRUE;
325 static void flip_page (void)
327 fprintf(yuv_out, "FRAME\n");
329 #ifdef CONFIG_LIBSWSCALE_INTERNALS
330 if (using_format != IMGFMT_YV12)
332 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
333 int rgb_stride, uv_stride, field_height;
334 unsigned int i, low_ofs;
336 rgb_stride = image_width * 3;
337 uv_stride = image_width / 2;
339 if (Y4M_IS_INTERLACED)
341 field_height = image_height / 2;
343 upper_y = image;
344 upper_u = upper_y + image_width * field_height;
345 upper_v = upper_u + image_width * field_height / 4;
346 low_ofs = image_width * field_height * 3 / 2;
347 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
349 deinterleave_fields(rgb_buffer, rgb_stride, image_height);
351 rgb24toyv12(rgb_buffer, upper_y, upper_u, upper_v,
352 image_width, field_height,
353 image_width, uv_stride, rgb_stride);
354 rgb24toyv12(rgb_buffer_lower, upper_y + low_ofs,
355 upper_u + low_ofs, upper_v + low_ofs,
356 image_width, field_height,
357 image_width, uv_stride, rgb_stride);
359 /* Write Y plane */
360 for(i = 0; i < field_height; i++)
362 vo_y4m_write(upper_y + image_width * i, image_width);
363 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
366 /* Write U and V plane */
367 for(i = 0; i < field_height / 2; i++)
369 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
370 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
372 for(i = 0; i < field_height / 2; i++)
374 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
375 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
377 return; /* Image written; We have to stop here */
380 rgb24toyv12(rgb_buffer, image_y, image_u, image_v,
381 image_width, image_height,
382 image_width, uv_stride, rgb_stride);
384 #endif
386 /* Write progressive frame */
387 vo_y4m_write(image, write_bytes);
390 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
392 int i;
393 uint8_t *dst, *src = srcimg[0];
395 switch (using_format)
397 case IMGFMT_YV12:
399 // copy Y:
400 dst = image_y + image_width * y + x;
401 for (i = 0; i < h; i++)
403 fast_memcpy(dst, src, w);
404 src += stride[0];
405 dst += image_width;
408 // copy U + V:
409 int imgstride = image_width >> 1;
410 uint8_t *src1 = srcimg[1];
411 uint8_t *src2 = srcimg[2];
412 uint8_t *dstu = image_u + imgstride * (y >> 1) + (x >> 1);
413 uint8_t *dstv = image_v + imgstride * (y >> 1) + (x >> 1);
414 for (i = 0; i < h / 2; i++)
416 fast_memcpy(dstu, src1 , w >> 1);
417 fast_memcpy(dstv, src2, w >> 1);
418 src1 += stride[1];
419 src2 += stride[2];
420 dstu += imgstride;
421 dstv += imgstride;
424 break;
426 case IMGFMT_BGR24:
427 case IMGFMT_RGB24:
428 dst = rgb_buffer + (image_width * y + x) * 3;
429 for (i = 0; i < h; i++)
431 fast_memcpy(dst, src, w * 3);
432 src += stride[0];
433 dst += image_width * 3;
435 break;
437 return 0;
440 static int draw_frame(uint8_t * src[])
442 switch(using_format)
444 case IMGFMT_YV12:
445 // gets done in draw_slice
446 break;
448 case IMGFMT_BGR24:
449 case IMGFMT_RGB24:
450 fast_memcpy(rgb_buffer, src[0], image_width * image_height * 3);
451 break;
453 return 0;
456 static int query_format(uint32_t format)
459 if (Y4M_IS_INTERLACED)
461 /* When processing interlaced material we want to get the raw RGB
462 * data and do the YV12 conversion ourselves to have the chrominance
463 * information sampled correct. */
465 switch(format)
467 case IMGFMT_YV12:
468 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
469 #ifdef CONFIG_LIBSWSCALE_INTERNALS
470 case IMGFMT_BGR|24:
471 case IMGFMT_RGB|24:
472 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
473 #endif
476 else
479 switch(format)
481 case IMGFMT_YV12:
482 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
483 #ifdef CONFIG_LIBSWSCALE_INTERNALS
484 case IMGFMT_BGR|24:
485 case IMGFMT_RGB|24:
486 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
487 #endif
490 return 0;
493 // WARNING: config(...) also uses this
494 static void uninit(void)
496 if(image)
497 free(image);
498 image = NULL;
500 if(yuv_out)
501 fclose(yuv_out);
502 yuv_out = NULL;
504 if(rgb_buffer)
505 free(rgb_buffer);
506 rgb_buffer = NULL;
508 if(rgb_line_buffer)
509 free(rgb_line_buffer);
510 rgb_line_buffer = NULL;
512 if (yuv_filename)
513 free(yuv_filename);
514 yuv_filename = NULL;
515 image_width = 0;
516 image_height = 0;
517 image_fps = 0;
521 static void check_events(void)
525 static int preinit(const char *arg)
527 int il, il_bf;
528 const opt_t subopts[] = {
529 {"interlaced", OPT_ARG_BOOL, &il, NULL},
530 {"interlaced_bf", OPT_ARG_BOOL, &il_bf, NULL},
531 {"file", OPT_ARG_MSTRZ, &yuv_filename, NULL},
532 {NULL}
535 il = 0;
536 il_bf = 0;
537 yuv_filename = strdup("stream.yuv");
538 if (subopt_parse(arg, subopts) != 0) {
539 mp_tmsg(MSGT_VO, MSGL_FATAL, "Unknown subdevice: %s", arg);
540 return -1;
543 config_interlace = Y4M_ILACE_NONE;
544 if (il)
545 config_interlace = Y4M_ILACE_TOP_FIRST;
546 if (il_bf)
547 config_interlace = Y4M_ILACE_BOTTOM_FIRST;
549 /* Inform user which output mode is used */
550 switch (config_interlace)
552 case Y4M_ILACE_TOP_FIRST:
553 mp_tmsg(MSGT_VO,MSGL_STATUS,
554 "Using interlaced output mode, top-field first.");
555 break;
556 case Y4M_ILACE_BOTTOM_FIRST:
557 mp_tmsg(MSGT_VO,MSGL_STATUS,
558 "Using interlaced output mode, bottom-field first.");
559 break;
560 default:
561 mp_tmsg(MSGT_VO,MSGL_STATUS,
562 "Using (default) progressive frame mode.");
563 break;
565 return 0;
568 static int control(uint32_t request, void *data)
570 switch (request) {
571 case VOCTRL_QUERY_FORMAT:
572 return query_format(*((uint32_t*)data));
573 case VOCTRL_DUPLICATE_FRAME:
574 return write_last_frame();
576 return VO_NOTIMPL;