typo fixes
[mplayer/greg.git] / libvo / vo_yuv4mpeg.c
blob332a072f1d42cd20153a6a3121470b95f8b64d1d
1 /*
2 * vo_yuv4mpeg.c, yuv4mpeg (mjpegtools) interface
4 * Thrown together by
5 * Robert Kesterson <robertk@robertk.com>
6 * Based on the pgm output plugin, the rgb2rgb postproc filter, divxdec,
7 * and probably others.
9 * This is undoubtedly incomplete, inaccurate, or just plain wrong. :-)
11 * 2002/06/19 Klaus Stengel <Klaus.Stengel@asamnet.de>
12 * - added support for interlaced output
13 * Activate by using '-vo yuv4mpeg:interlaced'
14 * or '-vo yuv4mpeg:interlaced_bf' if your source has
15 * bottom fields first
16 * - added some additional checks to catch problems
18 * 2002/04/17 Juergen Hammelmann <juergen.hammelmann@gmx.de>
19 * - added support for output of subtitles
20 * best, if you give option '-osdlevel 0' to mplayer for
21 * no watching the seek+timer
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
31 #include "config.h"
32 #include "subopt-helper.h"
33 #include "video_out.h"
34 #include "video_out_internal.h"
36 #include "mp_msg.h"
37 #include "help_mp.h"
39 #include "sub.h"
41 #include "fastmemcpy.h"
42 #include "postproc/rgb2rgb.h"
43 #include "libmpcodecs/vf_scale.h"
45 static vo_info_t info =
47 "yuv4mpeg output for mjpegtools",
48 "yuv4mpeg",
49 "Robert Kesterson <robertk@robertk.com>",
53 LIBVO_EXTERN (yuv4mpeg)
55 static int image_width = 0;
56 static int image_height = 0;
57 static float image_fps = 0;
59 static uint8_t *image = NULL;
60 static uint8_t *image_y = NULL;
61 static uint8_t *image_u = NULL;
62 static uint8_t *image_v = NULL;
64 static uint8_t *rgb_buffer = NULL;
65 static uint8_t *rgb_line_buffer = NULL;
67 static char *yuv_filename = NULL;
69 static int using_format = 0;
70 static FILE *yuv_out;
71 static int write_bytes;
73 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
74 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
75 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
77 /* Set progressive mode as default */
78 static int config_interlace = Y4M_ILACE_NONE;
79 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
81 static int config(uint32_t width, uint32_t height, uint32_t d_width,
82 uint32_t d_height, uint32_t flags, char *title,
83 uint32_t format)
85 if (image_width == width && image_height == height &&
86 image_fps == vo_fps && vo_config_count)
87 return 0;
88 if (vo_config_count) {
89 mp_msg(MSGT_VO, MSGL_WARN,
90 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
91 "restarting output.\n",
92 image_width, width, image_height, height, image_fps, vo_fps);
93 uninit();
95 image_height = height;
96 image_width = width;
97 image_fps = vo_fps;
98 using_format = format;
100 if (Y4M_IS_INTERLACED)
102 if (height % 4)
104 mp_msg(MSGT_VO,MSGL_FATAL,
105 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4);
106 return -1;
109 rgb_line_buffer = malloc(image_width * 3);
110 if (!rgb_line_buffer)
112 mp_msg(MSGT_VO,MSGL_FATAL,
113 MSGTR_VO_YUV4MPEG_InterlacedLineBufAllocFail);
114 return -1;
117 if (using_format == IMGFMT_YV12)
118 mp_msg(MSGT_VO,MSGL_WARN,
119 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB);
122 if (width % 2)
124 mp_msg(MSGT_VO,MSGL_FATAL,
125 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2);
126 return -1;
129 if(using_format != IMGFMT_YV12)
131 sws_rgb2rgb_init(get_sws_cpuflags());
132 rgb_buffer = malloc(image_width * image_height * 3);
133 if (!rgb_buffer)
135 mp_msg(MSGT_VO,MSGL_FATAL,
136 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf);
137 return -1;
141 write_bytes = image_width * image_height * 3 / 2;
142 image = malloc(write_bytes);
144 yuv_out = fopen(yuv_filename, "wb");
145 if (!yuv_out || image == 0)
147 mp_msg(MSGT_VO,MSGL_FATAL,
148 MSGTR_VO_YUV4MPEG_OutFileOpenError,
149 yuv_filename);
150 return -1;
152 image_y = image;
153 image_u = image_y + image_width * image_height;
154 image_v = image_u + image_width * image_height / 4;
156 // This isn't right.
157 // But it should work as long as the file isn't interlaced
158 // or otherwise unusual (the "Ip A0:0" part).
160 /* At least the interlacing is ok now */
161 fprintf(yuv_out, "YUV4MPEG2 W%d H%d F%ld:%ld I%c A0:0\n",
162 image_width, image_height, (long)(image_fps * 1000000.0),
163 (long)1000000, config_interlace);
165 fflush(yuv_out);
166 return 0;
169 /* Only use when h divisable by 2! */
170 static void swap_fields(uint8_t *ptr, const int h, const int stride)
172 int i;
174 for (i=0; i<h; i +=2)
176 memcpy(rgb_line_buffer , ptr + stride * i , stride);
177 memcpy(ptr + stride * i , ptr + stride * (i+1), stride);
178 memcpy(ptr + stride * (i+1), rgb_line_buffer , stride);
182 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
183 unsigned char *srca, int stride) {
184 switch (using_format)
186 case IMGFMT_YV12:
187 vo_draw_alpha_yv12(w, h, src, srca, stride,
188 image + y0 * image_width + x0, image_width);
189 break;
191 case IMGFMT_BGR|24:
192 case IMGFMT_RGB|24:
193 if (config_interlace != Y4M_ILACE_BOTTOM_FIRST)
194 vo_draw_alpha_rgb24(w, h, src, srca, stride,
195 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
196 else
198 swap_fields (rgb_buffer, image_height, image_width * 3);
200 vo_draw_alpha_rgb24(w, h, src, srca, stride,
201 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
203 swap_fields (rgb_buffer, image_height, image_width * 3);
205 break;
209 static void draw_osd(void)
211 vo_draw_text(image_width, image_height, draw_alpha);
214 static void deinterleave_fields(uint8_t *ptr, const int stride,
215 const int img_height)
217 unsigned int i, j, k_start = 1, modv = img_height - 1;
218 unsigned char *line_state = malloc(modv);
220 for (i=0; i<modv; i++)
221 line_state[i] = 0;
223 line_state[0] = 1;
225 while(k_start < modv)
227 i = j = k_start;
228 memcpy(rgb_line_buffer, ptr + stride * i, stride);
230 while (!line_state[j])
232 line_state[j] = 1;
233 i = j;
234 j = j * 2 % modv;
235 memcpy(ptr + stride * i, ptr + stride * j, stride);
237 memcpy(ptr + stride * i, rgb_line_buffer, stride);
239 while(k_start < modv && line_state[k_start])
240 k_start++;
242 free(line_state);
245 static void vo_y4m_write(const void *ptr, const size_t num_bytes)
247 if (fwrite(ptr, 1, num_bytes, yuv_out) != num_bytes)
248 mp_msg(MSGT_VO,MSGL_ERR,
249 MSGTR_VO_YUV4MPEG_OutFileWriteError);
252 static int write_last_frame(void)
255 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
256 int rgb_stride, uv_stride, field_height;
257 unsigned int i, low_ofs;
259 fprintf(yuv_out, "FRAME\n");
261 if (using_format != IMGFMT_YV12)
263 rgb_stride = image_width * 3;
264 uv_stride = image_width / 2;
266 if (Y4M_IS_INTERLACED)
268 field_height = image_height / 2;
270 upper_y = image;
271 upper_u = upper_y + image_width * field_height;
272 upper_v = upper_u + image_width * field_height / 4;
273 low_ofs = image_width * field_height * 3 / 2;
274 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
276 /* Write Y plane */
277 for(i = 0; i < field_height; i++)
279 vo_y4m_write(upper_y + image_width * i, image_width);
280 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
283 /* Write U and V plane */
284 for(i = 0; i < field_height / 2; i++)
286 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
287 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
289 for(i = 0; i < field_height / 2; i++)
291 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
292 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
294 return VO_TRUE; /* Image written; We have to stop here */
297 /* Write progressive frame */
298 vo_y4m_write(image, write_bytes);
299 return VO_TRUE;
302 static void flip_page (void)
304 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
305 int rgb_stride, uv_stride, field_height;
306 unsigned int i, low_ofs;
308 fprintf(yuv_out, "FRAME\n");
310 if (using_format != IMGFMT_YV12)
312 rgb_stride = image_width * 3;
313 uv_stride = image_width / 2;
315 if (Y4M_IS_INTERLACED)
317 field_height = image_height / 2;
319 upper_y = image;
320 upper_u = upper_y + image_width * field_height;
321 upper_v = upper_u + image_width * field_height / 4;
322 low_ofs = image_width * field_height * 3 / 2;
323 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
325 deinterleave_fields(rgb_buffer, rgb_stride, image_height);
327 rgb24toyv12(rgb_buffer, upper_y, upper_u, upper_v,
328 image_width, field_height,
329 image_width, uv_stride, rgb_stride);
330 rgb24toyv12(rgb_buffer_lower, upper_y + low_ofs,
331 upper_u + low_ofs, upper_v + low_ofs,
332 image_width, field_height,
333 image_width, uv_stride, rgb_stride);
335 /* Write Y plane */
336 for(i = 0; i < field_height; i++)
338 vo_y4m_write(upper_y + image_width * i, image_width);
339 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
342 /* Write U and V plane */
343 for(i = 0; i < field_height / 2; i++)
345 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
346 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
348 for(i = 0; i < field_height / 2; i++)
350 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
351 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
353 return; /* Image written; We have to stop here */
356 rgb24toyv12(rgb_buffer, image_y, image_u, image_v,
357 image_width, image_height,
358 image_width, uv_stride, rgb_stride);
361 /* Write progressive frame */
362 vo_y4m_write(image, write_bytes);
365 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
367 int i;
368 uint8_t *dst, *src = srcimg[0];
370 switch (using_format)
372 case IMGFMT_YV12:
374 // copy Y:
375 dst = image_y + image_width * y + x;
376 for (i = 0; i < h; i++)
378 memcpy(dst, src, w);
379 src += stride[0];
380 dst += image_width;
383 // copy U + V:
384 int imgstride = image_width >> 1;
385 uint8_t *src1 = srcimg[1];
386 uint8_t *src2 = srcimg[2];
387 uint8_t *dstu = image_u + imgstride * (y >> 1) + (x >> 1);
388 uint8_t *dstv = image_v + imgstride * (y >> 1) + (x >> 1);
389 for (i = 0; i < h / 2; i++)
391 memcpy(dstu, src1 , w >> 1);
392 memcpy(dstv, src2, w >> 1);
393 src1 += stride[1];
394 src2 += stride[2];
395 dstu += imgstride;
396 dstv += imgstride;
399 break;
401 case IMGFMT_BGR24:
402 case IMGFMT_RGB24:
403 dst = rgb_buffer + (image_width * y + x) * 3;
404 for (i = 0; i < h; i++)
406 memcpy(dst, src, w * 3);
407 src += stride[0];
408 dst += image_width * 3;
410 break;
412 return 0;
415 static int draw_frame(uint8_t * src[])
417 switch(using_format)
419 case IMGFMT_YV12:
420 // gets done in draw_slice
421 break;
423 case IMGFMT_BGR24:
424 case IMGFMT_RGB24:
425 memcpy(rgb_buffer, src[0], image_width * image_height * 3);
426 break;
428 return 0;
431 static int query_format(uint32_t format)
434 if (Y4M_IS_INTERLACED)
436 /* When processing interlaced material we want to get the raw RGB
437 * data and do the YV12 conversion ourselves to have the chrominance
438 * information sampled correct. */
440 switch(format)
442 case IMGFMT_YV12:
443 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
444 case IMGFMT_BGR|24:
445 case IMGFMT_RGB|24:
446 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
449 else
452 switch(format)
454 case IMGFMT_YV12:
455 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
456 case IMGFMT_BGR|24:
457 case IMGFMT_RGB|24:
458 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
461 return 0;
464 // WARNING: config(...) also uses this
465 static void uninit(void)
467 if(image)
468 free(image);
469 image = NULL;
471 if(yuv_out)
472 fclose(yuv_out);
473 yuv_out = NULL;
475 if(rgb_buffer)
476 free(rgb_buffer);
477 rgb_buffer = NULL;
479 if(rgb_line_buffer)
480 free(rgb_line_buffer);
481 rgb_line_buffer = NULL;
483 if (yuv_filename)
484 free(yuv_filename);
485 yuv_filename = NULL;
486 image_width = 0;
487 image_height = 0;
488 image_fps = 0;
492 static void check_events(void)
496 static int preinit(const char *arg)
498 int il, il_bf;
499 opt_t subopts[] = {
500 {"interlaced", OPT_ARG_BOOL, &il, NULL},
501 {"interlaced_bf", OPT_ARG_BOOL, &il_bf, NULL},
502 {"file", OPT_ARG_MSTRZ, &yuv_filename, NULL},
503 {NULL}
506 il = 0;
507 il_bf = 0;
508 yuv_filename = strdup("stream.yuv");
509 if (subopt_parse(arg, subopts) != 0) {
510 mp_msg(MSGT_VO, MSGL_FATAL, MSGTR_VO_YUV4MPEG_UnknownSubDev, arg);
511 return -1;
514 config_interlace = Y4M_ILACE_NONE;
515 if (il)
516 config_interlace = Y4M_ILACE_TOP_FIRST;
517 if (il_bf)
518 config_interlace = Y4M_ILACE_BOTTOM_FIRST;
520 /* Inform user which output mode is used */
521 switch (config_interlace)
523 case Y4M_ILACE_TOP_FIRST:
524 mp_msg(MSGT_VO,MSGL_STATUS,
525 MSGTR_VO_YUV4MPEG_InterlacedTFFMode);
526 break;
527 case Y4M_ILACE_BOTTOM_FIRST:
528 mp_msg(MSGT_VO,MSGL_STATUS,
529 MSGTR_VO_YUV4MPEG_InterlacedBFFMode);
530 break;
531 default:
532 mp_msg(MSGT_VO,MSGL_STATUS,
533 MSGTR_VO_YUV4MPEG_ProgressiveMode);
534 break;
536 return 0;
539 static int control(uint32_t request, void *data, ...)
541 switch (request) {
542 case VOCTRL_QUERY_FORMAT:
543 return query_format(*((uint32_t*)data));
544 case VOCTRL_DUPLICATE_FRAME:
545 return write_last_frame();
547 return VO_NOTIMPL;