Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / vo_yuv4mpeg.c
blob6578d4b66e87a352da4e2e2c86005599bca273ec
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"
54 #include "sub.h"
56 #include "fastmemcpy.h"
57 #include "libswscale/swscale.h"
58 #ifdef CONFIG_LIBSWSCALE_INTERNALS
59 #include "libswscale/rgb2rgb.h"
60 #endif
61 #include "libmpcodecs/vf_scale.h"
62 #include "libavutil/rational.h"
64 static const vo_info_t info =
66 "yuv4mpeg output for mjpegtools",
67 "yuv4mpeg",
68 "Robert Kesterson <robertk@robertk.com>",
72 const LIBVO_EXTERN (yuv4mpeg)
74 static int image_width = 0;
75 static int image_height = 0;
76 static float image_fps = 0;
78 static uint8_t *image = NULL;
79 static uint8_t *image_y = NULL;
80 static uint8_t *image_u = NULL;
81 static uint8_t *image_v = NULL;
83 static uint8_t *rgb_buffer = NULL;
84 static uint8_t *rgb_line_buffer = NULL;
86 static char *yuv_filename = NULL;
88 static int using_format = 0;
89 static FILE *yuv_out;
90 static int write_bytes;
92 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
93 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
94 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
96 /* Set progressive mode as default */
97 static int config_interlace = Y4M_ILACE_NONE;
98 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
100 static int config(uint32_t width, uint32_t height, uint32_t d_width,
101 uint32_t d_height, uint32_t flags, char *title,
102 uint32_t format)
104 AVRational pixelaspect = av_div_q((AVRational){d_width, d_height},
105 (AVRational){width, height});
106 AVRational fps_frac = av_d2q(vo_fps, vo_fps * 1001 + 2);
107 if (image_width == width && image_height == height &&
108 image_fps == vo_fps && vo_config_count)
109 return 0;
110 if (vo_config_count) {
111 mp_msg(MSGT_VO, MSGL_WARN,
112 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
113 "restarting output.\n",
114 image_width, width, image_height, height, image_fps, vo_fps);
115 uninit();
117 image_height = height;
118 image_width = width;
119 image_fps = vo_fps;
120 using_format = format;
122 if (Y4M_IS_INTERLACED)
124 if (height % 4)
126 mp_tmsg(MSGT_VO,MSGL_FATAL,
127 "Interlaced mode requires image height to be divisible by 4.");
128 return -1;
131 rgb_line_buffer = malloc(image_width * 3);
132 if (!rgb_line_buffer)
134 mp_tmsg(MSGT_VO,MSGL_FATAL,
135 "Unable to allocate line buffer for interlaced mode.");
136 return -1;
139 if (using_format == IMGFMT_YV12)
140 mp_tmsg(MSGT_VO,MSGL_WARN,
141 "Input not RGB, can't separate chrominance by fields!");
144 if (width % 2)
146 mp_tmsg(MSGT_VO,MSGL_FATAL,
147 "Image width must be divisible by 2.");
148 return -1;
151 #ifdef CONFIG_LIBSWSCALE_INTERNALS
152 if(using_format != IMGFMT_YV12)
154 sws_rgb2rgb_init(get_sws_cpuflags());
155 rgb_buffer = malloc(image_width * image_height * 3);
156 if (!rgb_buffer)
158 mp_tmsg(MSGT_VO,MSGL_FATAL,
159 "Not enough memory to allocate RGB framebuffer.");
160 return -1;
163 #endif
165 write_bytes = image_width * image_height * 3 / 2;
166 image = malloc(write_bytes);
168 yuv_out = fopen(yuv_filename, "wb");
169 if (!yuv_out || image == 0)
171 mp_tmsg(MSGT_VO,MSGL_FATAL,
172 "Can't get memory or file handle to write \"%s\"!",
173 yuv_filename);
174 return -1;
176 image_y = image;
177 image_u = image_y + image_width * image_height;
178 image_v = image_u + image_width * image_height / 4;
180 fprintf(yuv_out, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
181 image_width, image_height, fps_frac.num, fps_frac.den,
182 config_interlace,
183 pixelaspect.num, pixelaspect.den);
185 fflush(yuv_out);
186 return 0;
189 /* Only use when h divisable by 2! */
190 static void swap_fields(uint8_t *ptr, const int h, const int stride)
192 int i;
194 for (i=0; i<h; i +=2)
196 fast_memcpy(rgb_line_buffer , ptr + stride * i , stride);
197 fast_memcpy(ptr + stride * i , ptr + stride * (i+1), stride);
198 fast_memcpy(ptr + stride * (i+1), rgb_line_buffer , stride);
202 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
203 unsigned char *srca, int stride) {
204 switch (using_format)
206 case IMGFMT_YV12:
207 vo_draw_alpha_yv12(w, h, src, srca, stride,
208 image + y0 * image_width + x0, image_width);
209 break;
211 case IMGFMT_BGR|24:
212 case IMGFMT_RGB|24:
213 if (config_interlace != Y4M_ILACE_BOTTOM_FIRST)
214 vo_draw_alpha_rgb24(w, h, src, srca, stride,
215 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
216 else
218 swap_fields (rgb_buffer, image_height, image_width * 3);
220 vo_draw_alpha_rgb24(w, h, src, srca, stride,
221 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
223 swap_fields (rgb_buffer, image_height, image_width * 3);
225 break;
229 static void draw_osd(void)
231 vo_draw_text(image_width, image_height, draw_alpha);
234 #ifdef CONFIG_LIBSWSCALE_INTERNALS
235 static void deinterleave_fields(uint8_t *ptr, const int stride,
236 const int img_height)
238 unsigned int i, j, k_start = 1, modv = img_height - 1;
239 unsigned char *line_state = malloc(modv);
241 for (i=0; i<modv; i++)
242 line_state[i] = 0;
244 line_state[0] = 1;
246 while(k_start < modv)
248 i = j = k_start;
249 fast_memcpy(rgb_line_buffer, ptr + stride * i, stride);
251 while (!line_state[j])
253 line_state[j] = 1;
254 i = j;
255 j = j * 2 % modv;
256 fast_memcpy(ptr + stride * i, ptr + stride * j, stride);
258 fast_memcpy(ptr + stride * i, rgb_line_buffer, stride);
260 while(k_start < modv && line_state[k_start])
261 k_start++;
263 free(line_state);
265 #endif
267 static void vo_y4m_write(const void *ptr, const size_t num_bytes)
269 if (fwrite(ptr, 1, num_bytes, yuv_out) != num_bytes)
270 mp_tmsg(MSGT_VO,MSGL_ERR,
271 "Error writing image to output!");
274 static int write_last_frame(void)
277 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
278 int rgb_stride, uv_stride, field_height;
279 unsigned int i, low_ofs;
281 fprintf(yuv_out, "FRAME\n");
283 if (using_format != IMGFMT_YV12)
285 rgb_stride = image_width * 3;
286 uv_stride = image_width / 2;
288 if (Y4M_IS_INTERLACED)
290 field_height = image_height / 2;
292 upper_y = image;
293 upper_u = upper_y + image_width * field_height;
294 upper_v = upper_u + image_width * field_height / 4;
295 low_ofs = image_width * field_height * 3 / 2;
296 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
298 /* Write Y plane */
299 for(i = 0; i < field_height; i++)
301 vo_y4m_write(upper_y + image_width * i, image_width);
302 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
305 /* Write U and V plane */
306 for(i = 0; i < field_height / 2; i++)
308 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
309 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
311 for(i = 0; i < field_height / 2; i++)
313 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
314 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
316 return VO_TRUE; /* Image written; We have to stop here */
319 /* Write progressive frame */
320 vo_y4m_write(image, write_bytes);
321 return VO_TRUE;
324 static void flip_page (void)
326 fprintf(yuv_out, "FRAME\n");
328 #ifdef CONFIG_LIBSWSCALE_INTERNALS
329 if (using_format != IMGFMT_YV12)
331 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
332 int rgb_stride, uv_stride, field_height;
333 unsigned int i, low_ofs;
335 rgb_stride = image_width * 3;
336 uv_stride = image_width / 2;
338 if (Y4M_IS_INTERLACED)
340 field_height = image_height / 2;
342 upper_y = image;
343 upper_u = upper_y + image_width * field_height;
344 upper_v = upper_u + image_width * field_height / 4;
345 low_ofs = image_width * field_height * 3 / 2;
346 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
348 deinterleave_fields(rgb_buffer, rgb_stride, image_height);
350 rgb24toyv12(rgb_buffer, upper_y, upper_u, upper_v,
351 image_width, field_height,
352 image_width, uv_stride, rgb_stride);
353 rgb24toyv12(rgb_buffer_lower, upper_y + low_ofs,
354 upper_u + low_ofs, upper_v + low_ofs,
355 image_width, field_height,
356 image_width, uv_stride, rgb_stride);
358 /* Write Y plane */
359 for(i = 0; i < field_height; i++)
361 vo_y4m_write(upper_y + image_width * i, image_width);
362 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
365 /* Write U and V plane */
366 for(i = 0; i < field_height / 2; i++)
368 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
369 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
371 for(i = 0; i < field_height / 2; i++)
373 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
374 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
376 return; /* Image written; We have to stop here */
379 rgb24toyv12(rgb_buffer, image_y, image_u, image_v,
380 image_width, image_height,
381 image_width, uv_stride, rgb_stride);
383 #endif
385 /* Write progressive frame */
386 vo_y4m_write(image, write_bytes);
389 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
391 int i;
392 uint8_t *dst, *src = srcimg[0];
394 switch (using_format)
396 case IMGFMT_YV12:
398 // copy Y:
399 dst = image_y + image_width * y + x;
400 for (i = 0; i < h; i++)
402 fast_memcpy(dst, src, w);
403 src += stride[0];
404 dst += image_width;
407 // copy U + V:
408 int imgstride = image_width >> 1;
409 uint8_t *src1 = srcimg[1];
410 uint8_t *src2 = srcimg[2];
411 uint8_t *dstu = image_u + imgstride * (y >> 1) + (x >> 1);
412 uint8_t *dstv = image_v + imgstride * (y >> 1) + (x >> 1);
413 for (i = 0; i < h / 2; i++)
415 fast_memcpy(dstu, src1 , w >> 1);
416 fast_memcpy(dstv, src2, w >> 1);
417 src1 += stride[1];
418 src2 += stride[2];
419 dstu += imgstride;
420 dstv += imgstride;
423 break;
425 case IMGFMT_BGR24:
426 case IMGFMT_RGB24:
427 dst = rgb_buffer + (image_width * y + x) * 3;
428 for (i = 0; i < h; i++)
430 fast_memcpy(dst, src, w * 3);
431 src += stride[0];
432 dst += image_width * 3;
434 break;
436 return 0;
439 static int draw_frame(uint8_t * src[])
441 switch(using_format)
443 case IMGFMT_YV12:
444 // gets done in draw_slice
445 break;
447 case IMGFMT_BGR24:
448 case IMGFMT_RGB24:
449 fast_memcpy(rgb_buffer, src[0], image_width * image_height * 3);
450 break;
452 return 0;
455 static int query_format(uint32_t format)
458 if (Y4M_IS_INTERLACED)
460 /* When processing interlaced material we want to get the raw RGB
461 * data and do the YV12 conversion ourselves to have the chrominance
462 * information sampled correct. */
464 switch(format)
466 case IMGFMT_YV12:
467 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
468 #ifdef CONFIG_LIBSWSCALE_INTERNALS
469 case IMGFMT_BGR|24:
470 case IMGFMT_RGB|24:
471 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
472 #endif
475 else
478 switch(format)
480 case IMGFMT_YV12:
481 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
482 #ifdef CONFIG_LIBSWSCALE_INTERNALS
483 case IMGFMT_BGR|24:
484 case IMGFMT_RGB|24:
485 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
486 #endif
489 return 0;
492 // WARNING: config(...) also uses this
493 static void uninit(void)
495 if(image)
496 free(image);
497 image = NULL;
499 if(yuv_out)
500 fclose(yuv_out);
501 yuv_out = NULL;
503 if(rgb_buffer)
504 free(rgb_buffer);
505 rgb_buffer = NULL;
507 if(rgb_line_buffer)
508 free(rgb_line_buffer);
509 rgb_line_buffer = NULL;
511 if (yuv_filename)
512 free(yuv_filename);
513 yuv_filename = NULL;
514 image_width = 0;
515 image_height = 0;
516 image_fps = 0;
520 static void check_events(void)
524 static int preinit(const char *arg)
526 int il, il_bf;
527 const opt_t subopts[] = {
528 {"interlaced", OPT_ARG_BOOL, &il, NULL},
529 {"interlaced_bf", OPT_ARG_BOOL, &il_bf, NULL},
530 {"file", OPT_ARG_MSTRZ, &yuv_filename, NULL},
531 {NULL}
534 il = 0;
535 il_bf = 0;
536 yuv_filename = strdup("stream.yuv");
537 if (subopt_parse(arg, subopts) != 0) {
538 mp_tmsg(MSGT_VO, MSGL_FATAL, "Unknown subdevice: %s", arg);
539 return -1;
542 config_interlace = Y4M_ILACE_NONE;
543 if (il)
544 config_interlace = Y4M_ILACE_TOP_FIRST;
545 if (il_bf)
546 config_interlace = Y4M_ILACE_BOTTOM_FIRST;
548 /* Inform user which output mode is used */
549 switch (config_interlace)
551 case Y4M_ILACE_TOP_FIRST:
552 mp_tmsg(MSGT_VO,MSGL_STATUS,
553 "Using interlaced output mode, top-field first.");
554 break;
555 case Y4M_ILACE_BOTTOM_FIRST:
556 mp_tmsg(MSGT_VO,MSGL_STATUS,
557 "Using interlaced output mode, bottom-field first.");
558 break;
559 default:
560 mp_tmsg(MSGT_VO,MSGL_STATUS,
561 "Using (default) progressive frame mode.");
562 break;
564 return 0;
567 static int control(uint32_t request, void *data)
569 switch (request) {
570 case VOCTRL_QUERY_FORMAT:
571 return query_format(*((uint32_t*)data));
572 case VOCTRL_DUPLICATE_FRAME:
573 return write_last_frame();
575 return VO_NOTIMPL;