Comment out the correct #endif directive.
[mplayer/greg.git] / libvo / vo_yuv4mpeg.c
blob285846278b092de80f53c86c15f84039fff95b11
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>
30 #include <limits.h>
32 #include "config.h"
33 #include "subopt-helper.h"
34 #include "video_out.h"
35 #include "video_out_internal.h"
37 #include "mp_msg.h"
38 #include "help_mp.h"
40 #include "sub.h"
42 #include "fastmemcpy.h"
43 #include "libswscale/swscale.h"
44 #include "libswscale/rgb2rgb.h"
45 #include "libmpcodecs/vf_scale.h"
46 #include "libavutil/rational.h"
48 static const vo_info_t info =
50 "yuv4mpeg output for mjpegtools",
51 "yuv4mpeg",
52 "Robert Kesterson <robertk@robertk.com>",
56 const LIBVO_EXTERN (yuv4mpeg)
58 static int image_width = 0;
59 static int image_height = 0;
60 static float image_fps = 0;
62 static uint8_t *image = NULL;
63 static uint8_t *image_y = NULL;
64 static uint8_t *image_u = NULL;
65 static uint8_t *image_v = NULL;
67 static uint8_t *rgb_buffer = NULL;
68 static uint8_t *rgb_line_buffer = NULL;
70 static char *yuv_filename = NULL;
72 static int using_format = 0;
73 static FILE *yuv_out;
74 static int write_bytes;
76 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
77 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
78 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
80 /* Set progressive mode as default */
81 static int config_interlace = Y4M_ILACE_NONE;
82 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
84 static int config(uint32_t width, uint32_t height, uint32_t d_width,
85 uint32_t d_height, uint32_t flags, char *title,
86 uint32_t format)
88 AVRational pixelaspect = av_div_q((AVRational){d_width, d_height},
89 (AVRational){width, height});
90 AVRational fps_frac = av_d2q(vo_fps, INT_MAX);
91 if (image_width == width && image_height == height &&
92 image_fps == vo_fps && vo_config_count)
93 return 0;
94 if (vo_config_count) {
95 mp_msg(MSGT_VO, MSGL_WARN,
96 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
97 "restarting output.\n",
98 image_width, width, image_height, height, image_fps, vo_fps);
99 uninit();
101 image_height = height;
102 image_width = width;
103 image_fps = vo_fps;
104 using_format = format;
106 if (Y4M_IS_INTERLACED)
108 if (height % 4)
110 mp_msg(MSGT_VO,MSGL_FATAL,
111 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4);
112 return -1;
115 rgb_line_buffer = malloc(image_width * 3);
116 if (!rgb_line_buffer)
118 mp_msg(MSGT_VO,MSGL_FATAL,
119 MSGTR_VO_YUV4MPEG_InterlacedLineBufAllocFail);
120 return -1;
123 if (using_format == IMGFMT_YV12)
124 mp_msg(MSGT_VO,MSGL_WARN,
125 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB);
128 if (width % 2)
130 mp_msg(MSGT_VO,MSGL_FATAL,
131 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2);
132 return -1;
135 if(using_format != IMGFMT_YV12)
137 sws_rgb2rgb_init(get_sws_cpuflags());
138 rgb_buffer = malloc(image_width * image_height * 3);
139 if (!rgb_buffer)
141 mp_msg(MSGT_VO,MSGL_FATAL,
142 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf);
143 return -1;
147 write_bytes = image_width * image_height * 3 / 2;
148 image = malloc(write_bytes);
150 yuv_out = fopen(yuv_filename, "wb");
151 if (!yuv_out || image == 0)
153 mp_msg(MSGT_VO,MSGL_FATAL,
154 MSGTR_VO_YUV4MPEG_OutFileOpenError,
155 yuv_filename);
156 return -1;
158 image_y = image;
159 image_u = image_y + image_width * image_height;
160 image_v = image_u + image_width * image_height / 4;
162 fprintf(yuv_out, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
163 image_width, image_height, fps_frac.num, fps_frac.den,
164 config_interlace,
165 pixelaspect.num, pixelaspect.den);
167 fflush(yuv_out);
168 return 0;
171 /* Only use when h divisable by 2! */
172 static void swap_fields(uint8_t *ptr, const int h, const int stride)
174 int i;
176 for (i=0; i<h; i +=2)
178 fast_memcpy(rgb_line_buffer , ptr + stride * i , stride);
179 fast_memcpy(ptr + stride * i , ptr + stride * (i+1), stride);
180 fast_memcpy(ptr + stride * (i+1), rgb_line_buffer , stride);
184 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
185 unsigned char *srca, int stride) {
186 switch (using_format)
188 case IMGFMT_YV12:
189 vo_draw_alpha_yv12(w, h, src, srca, stride,
190 image + y0 * image_width + x0, image_width);
191 break;
193 case IMGFMT_BGR|24:
194 case IMGFMT_RGB|24:
195 if (config_interlace != Y4M_ILACE_BOTTOM_FIRST)
196 vo_draw_alpha_rgb24(w, h, src, srca, stride,
197 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
198 else
200 swap_fields (rgb_buffer, image_height, image_width * 3);
202 vo_draw_alpha_rgb24(w, h, src, srca, stride,
203 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
205 swap_fields (rgb_buffer, image_height, image_width * 3);
207 break;
211 static void draw_osd(void)
213 vo_draw_text(image_width, image_height, draw_alpha);
216 static void deinterleave_fields(uint8_t *ptr, const int stride,
217 const int img_height)
219 unsigned int i, j, k_start = 1, modv = img_height - 1;
220 unsigned char *line_state = malloc(modv);
222 for (i=0; i<modv; i++)
223 line_state[i] = 0;
225 line_state[0] = 1;
227 while(k_start < modv)
229 i = j = k_start;
230 fast_memcpy(rgb_line_buffer, ptr + stride * i, stride);
232 while (!line_state[j])
234 line_state[j] = 1;
235 i = j;
236 j = j * 2 % modv;
237 fast_memcpy(ptr + stride * i, ptr + stride * j, stride);
239 fast_memcpy(ptr + stride * i, rgb_line_buffer, stride);
241 while(k_start < modv && line_state[k_start])
242 k_start++;
244 free(line_state);
247 static void vo_y4m_write(const void *ptr, const size_t num_bytes)
249 if (fwrite(ptr, 1, num_bytes, yuv_out) != num_bytes)
250 mp_msg(MSGT_VO,MSGL_ERR,
251 MSGTR_VO_YUV4MPEG_OutFileWriteError);
254 static int write_last_frame(void)
257 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
258 int rgb_stride, uv_stride, field_height;
259 unsigned int i, low_ofs;
261 fprintf(yuv_out, "FRAME\n");
263 if (using_format != IMGFMT_YV12)
265 rgb_stride = image_width * 3;
266 uv_stride = image_width / 2;
268 if (Y4M_IS_INTERLACED)
270 field_height = image_height / 2;
272 upper_y = image;
273 upper_u = upper_y + image_width * field_height;
274 upper_v = upper_u + image_width * field_height / 4;
275 low_ofs = image_width * field_height * 3 / 2;
276 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
278 /* Write Y plane */
279 for(i = 0; i < field_height; i++)
281 vo_y4m_write(upper_y + image_width * i, image_width);
282 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
285 /* Write U and V plane */
286 for(i = 0; i < field_height / 2; i++)
288 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
289 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
291 for(i = 0; i < field_height / 2; i++)
293 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
294 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
296 return VO_TRUE; /* Image written; We have to stop here */
299 /* Write progressive frame */
300 vo_y4m_write(image, write_bytes);
301 return VO_TRUE;
304 static void flip_page (void)
306 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
307 int rgb_stride, uv_stride, field_height;
308 unsigned int i, low_ofs;
310 fprintf(yuv_out, "FRAME\n");
312 if (using_format != IMGFMT_YV12)
314 rgb_stride = image_width * 3;
315 uv_stride = image_width / 2;
317 if (Y4M_IS_INTERLACED)
319 field_height = image_height / 2;
321 upper_y = image;
322 upper_u = upper_y + image_width * field_height;
323 upper_v = upper_u + image_width * field_height / 4;
324 low_ofs = image_width * field_height * 3 / 2;
325 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
327 deinterleave_fields(rgb_buffer, rgb_stride, image_height);
329 rgb24toyv12(rgb_buffer, upper_y, upper_u, upper_v,
330 image_width, field_height,
331 image_width, uv_stride, rgb_stride);
332 rgb24toyv12(rgb_buffer_lower, upper_y + low_ofs,
333 upper_u + low_ofs, upper_v + low_ofs,
334 image_width, field_height,
335 image_width, uv_stride, rgb_stride);
337 /* Write Y plane */
338 for(i = 0; i < field_height; i++)
340 vo_y4m_write(upper_y + image_width * i, image_width);
341 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
344 /* Write U and V plane */
345 for(i = 0; i < field_height / 2; i++)
347 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
348 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
350 for(i = 0; i < field_height / 2; i++)
352 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
353 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
355 return; /* Image written; We have to stop here */
358 rgb24toyv12(rgb_buffer, image_y, image_u, image_v,
359 image_width, image_height,
360 image_width, uv_stride, rgb_stride);
363 /* Write progressive frame */
364 vo_y4m_write(image, write_bytes);
367 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
369 int i;
370 uint8_t *dst, *src = srcimg[0];
372 switch (using_format)
374 case IMGFMT_YV12:
376 // copy Y:
377 dst = image_y + image_width * y + x;
378 for (i = 0; i < h; i++)
380 fast_memcpy(dst, src, w);
381 src += stride[0];
382 dst += image_width;
385 // copy U + V:
386 int imgstride = image_width >> 1;
387 uint8_t *src1 = srcimg[1];
388 uint8_t *src2 = srcimg[2];
389 uint8_t *dstu = image_u + imgstride * (y >> 1) + (x >> 1);
390 uint8_t *dstv = image_v + imgstride * (y >> 1) + (x >> 1);
391 for (i = 0; i < h / 2; i++)
393 fast_memcpy(dstu, src1 , w >> 1);
394 fast_memcpy(dstv, src2, w >> 1);
395 src1 += stride[1];
396 src2 += stride[2];
397 dstu += imgstride;
398 dstv += imgstride;
401 break;
403 case IMGFMT_BGR24:
404 case IMGFMT_RGB24:
405 dst = rgb_buffer + (image_width * y + x) * 3;
406 for (i = 0; i < h; i++)
408 fast_memcpy(dst, src, w * 3);
409 src += stride[0];
410 dst += image_width * 3;
412 break;
414 return 0;
417 static int draw_frame(uint8_t * src[])
419 switch(using_format)
421 case IMGFMT_YV12:
422 // gets done in draw_slice
423 break;
425 case IMGFMT_BGR24:
426 case IMGFMT_RGB24:
427 fast_memcpy(rgb_buffer, src[0], image_width * image_height * 3);
428 break;
430 return 0;
433 static int query_format(uint32_t format)
436 if (Y4M_IS_INTERLACED)
438 /* When processing interlaced material we want to get the raw RGB
439 * data and do the YV12 conversion ourselves to have the chrominance
440 * information sampled correct. */
442 switch(format)
444 case IMGFMT_YV12:
445 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
446 case IMGFMT_BGR|24:
447 case IMGFMT_RGB|24:
448 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
451 else
454 switch(format)
456 case IMGFMT_YV12:
457 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
458 case IMGFMT_BGR|24:
459 case IMGFMT_RGB|24:
460 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
463 return 0;
466 // WARNING: config(...) also uses this
467 static void uninit(void)
469 if(image)
470 free(image);
471 image = NULL;
473 if(yuv_out)
474 fclose(yuv_out);
475 yuv_out = NULL;
477 if(rgb_buffer)
478 free(rgb_buffer);
479 rgb_buffer = NULL;
481 if(rgb_line_buffer)
482 free(rgb_line_buffer);
483 rgb_line_buffer = NULL;
485 if (yuv_filename)
486 free(yuv_filename);
487 yuv_filename = NULL;
488 image_width = 0;
489 image_height = 0;
490 image_fps = 0;
494 static void check_events(void)
498 static int preinit(const char *arg)
500 int il, il_bf;
501 opt_t subopts[] = {
502 {"interlaced", OPT_ARG_BOOL, &il, NULL},
503 {"interlaced_bf", OPT_ARG_BOOL, &il_bf, NULL},
504 {"file", OPT_ARG_MSTRZ, &yuv_filename, NULL},
505 {NULL}
508 il = 0;
509 il_bf = 0;
510 yuv_filename = strdup("stream.yuv");
511 if (subopt_parse(arg, subopts) != 0) {
512 mp_msg(MSGT_VO, MSGL_FATAL, MSGTR_VO_YUV4MPEG_UnknownSubDev, arg);
513 return -1;
516 config_interlace = Y4M_ILACE_NONE;
517 if (il)
518 config_interlace = Y4M_ILACE_TOP_FIRST;
519 if (il_bf)
520 config_interlace = Y4M_ILACE_BOTTOM_FIRST;
522 /* Inform user which output mode is used */
523 switch (config_interlace)
525 case Y4M_ILACE_TOP_FIRST:
526 mp_msg(MSGT_VO,MSGL_STATUS,
527 MSGTR_VO_YUV4MPEG_InterlacedTFFMode);
528 break;
529 case Y4M_ILACE_BOTTOM_FIRST:
530 mp_msg(MSGT_VO,MSGL_STATUS,
531 MSGTR_VO_YUV4MPEG_InterlacedBFFMode);
532 break;
533 default:
534 mp_msg(MSGT_VO,MSGL_STATUS,
535 MSGTR_VO_YUV4MPEG_ProgressiveMode);
536 break;
538 return 0;
541 static int control(uint32_t request, void *data, ...)
543 switch (request) {
544 case VOCTRL_QUERY_FORMAT:
545 return query_format(*((uint32_t*)data));
546 case VOCTRL_DUPLICATE_FRAME:
547 return write_last_frame();
549 return VO_NOTIMPL;