Remove unused static functions asx_list_add() and asx_list_remove().
[mplayer/glamo.git] / libvo / vo_yuv4mpeg.c
blobf4cbfb1639ed86bf5c44d1e004812e4c816d2049
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 #include "libswscale/rgb2rgb.h"
60 #include "libmpcodecs/vf_scale.h"
61 #include "libavutil/rational.h"
63 static const vo_info_t info =
65 "yuv4mpeg output for mjpegtools",
66 "yuv4mpeg",
67 "Robert Kesterson <robertk@robertk.com>",
71 const LIBVO_EXTERN (yuv4mpeg)
73 static int image_width = 0;
74 static int image_height = 0;
75 static float image_fps = 0;
77 static uint8_t *image = NULL;
78 static uint8_t *image_y = NULL;
79 static uint8_t *image_u = NULL;
80 static uint8_t *image_v = NULL;
82 static uint8_t *rgb_buffer = NULL;
83 static uint8_t *rgb_line_buffer = NULL;
85 static char *yuv_filename = NULL;
87 static int using_format = 0;
88 static FILE *yuv_out;
89 static int write_bytes;
91 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
92 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
93 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
95 /* Set progressive mode as default */
96 static int config_interlace = Y4M_ILACE_NONE;
97 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
99 static int config(uint32_t width, uint32_t height, uint32_t d_width,
100 uint32_t d_height, uint32_t flags, char *title,
101 uint32_t format)
103 AVRational pixelaspect = av_div_q((AVRational){d_width, d_height},
104 (AVRational){width, height});
105 AVRational fps_frac = av_d2q(vo_fps, vo_fps * 1001 + 2);
106 if (image_width == width && image_height == height &&
107 image_fps == vo_fps && vo_config_count)
108 return 0;
109 if (vo_config_count) {
110 mp_msg(MSGT_VO, MSGL_WARN,
111 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
112 "restarting output.\n",
113 image_width, width, image_height, height, image_fps, vo_fps);
114 uninit();
116 image_height = height;
117 image_width = width;
118 image_fps = vo_fps;
119 using_format = format;
121 if (Y4M_IS_INTERLACED)
123 if (height % 4)
125 mp_msg(MSGT_VO,MSGL_FATAL,
126 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4);
127 return -1;
130 rgb_line_buffer = malloc(image_width * 3);
131 if (!rgb_line_buffer)
133 mp_msg(MSGT_VO,MSGL_FATAL,
134 MSGTR_VO_YUV4MPEG_InterlacedLineBufAllocFail);
135 return -1;
138 if (using_format == IMGFMT_YV12)
139 mp_msg(MSGT_VO,MSGL_WARN,
140 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB);
143 if (width % 2)
145 mp_msg(MSGT_VO,MSGL_FATAL,
146 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2);
147 return -1;
150 if(using_format != IMGFMT_YV12)
152 sws_rgb2rgb_init(get_sws_cpuflags());
153 rgb_buffer = malloc(image_width * image_height * 3);
154 if (!rgb_buffer)
156 mp_msg(MSGT_VO,MSGL_FATAL,
157 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf);
158 return -1;
162 write_bytes = image_width * image_height * 3 / 2;
163 image = malloc(write_bytes);
165 yuv_out = fopen(yuv_filename, "wb");
166 if (!yuv_out || image == 0)
168 mp_msg(MSGT_VO,MSGL_FATAL,
169 MSGTR_VO_YUV4MPEG_OutFileOpenError,
170 yuv_filename);
171 return -1;
173 image_y = image;
174 image_u = image_y + image_width * image_height;
175 image_v = image_u + image_width * image_height / 4;
177 fprintf(yuv_out, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
178 image_width, image_height, fps_frac.num, fps_frac.den,
179 config_interlace,
180 pixelaspect.num, pixelaspect.den);
182 fflush(yuv_out);
183 return 0;
186 /* Only use when h divisable by 2! */
187 static void swap_fields(uint8_t *ptr, const int h, const int stride)
189 int i;
191 for (i=0; i<h; i +=2)
193 fast_memcpy(rgb_line_buffer , ptr + stride * i , stride);
194 fast_memcpy(ptr + stride * i , ptr + stride * (i+1), stride);
195 fast_memcpy(ptr + stride * (i+1), rgb_line_buffer , stride);
199 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
200 unsigned char *srca, int stride) {
201 switch (using_format)
203 case IMGFMT_YV12:
204 vo_draw_alpha_yv12(w, h, src, srca, stride,
205 image + y0 * image_width + x0, image_width);
206 break;
208 case IMGFMT_BGR|24:
209 case IMGFMT_RGB|24:
210 if (config_interlace != Y4M_ILACE_BOTTOM_FIRST)
211 vo_draw_alpha_rgb24(w, h, src, srca, stride,
212 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
213 else
215 swap_fields (rgb_buffer, image_height, image_width * 3);
217 vo_draw_alpha_rgb24(w, h, src, srca, stride,
218 rgb_buffer + (y0 * image_width + x0) * 3, image_width * 3);
220 swap_fields (rgb_buffer, image_height, image_width * 3);
222 break;
226 static void draw_osd(void)
228 vo_draw_text(image_width, image_height, draw_alpha);
231 static void deinterleave_fields(uint8_t *ptr, const int stride,
232 const int img_height)
234 unsigned int i, j, k_start = 1, modv = img_height - 1;
235 unsigned char *line_state = malloc(modv);
237 for (i=0; i<modv; i++)
238 line_state[i] = 0;
240 line_state[0] = 1;
242 while(k_start < modv)
244 i = j = k_start;
245 fast_memcpy(rgb_line_buffer, ptr + stride * i, stride);
247 while (!line_state[j])
249 line_state[j] = 1;
250 i = j;
251 j = j * 2 % modv;
252 fast_memcpy(ptr + stride * i, ptr + stride * j, stride);
254 fast_memcpy(ptr + stride * i, rgb_line_buffer, stride);
256 while(k_start < modv && line_state[k_start])
257 k_start++;
259 free(line_state);
262 static void vo_y4m_write(const void *ptr, const size_t num_bytes)
264 if (fwrite(ptr, 1, num_bytes, yuv_out) != num_bytes)
265 mp_msg(MSGT_VO,MSGL_ERR,
266 MSGTR_VO_YUV4MPEG_OutFileWriteError);
269 static int write_last_frame(void)
272 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
273 int rgb_stride, uv_stride, field_height;
274 unsigned int i, low_ofs;
276 fprintf(yuv_out, "FRAME\n");
278 if (using_format != IMGFMT_YV12)
280 rgb_stride = image_width * 3;
281 uv_stride = image_width / 2;
283 if (Y4M_IS_INTERLACED)
285 field_height = image_height / 2;
287 upper_y = image;
288 upper_u = upper_y + image_width * field_height;
289 upper_v = upper_u + image_width * field_height / 4;
290 low_ofs = image_width * field_height * 3 / 2;
291 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
293 /* Write Y plane */
294 for(i = 0; i < field_height; i++)
296 vo_y4m_write(upper_y + image_width * i, image_width);
297 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
300 /* Write U and V plane */
301 for(i = 0; i < field_height / 2; i++)
303 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
304 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
306 for(i = 0; i < field_height / 2; i++)
308 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
309 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
311 return VO_TRUE; /* Image written; We have to stop here */
314 /* Write progressive frame */
315 vo_y4m_write(image, write_bytes);
316 return VO_TRUE;
319 static void flip_page (void)
321 uint8_t *upper_y, *upper_u, *upper_v, *rgb_buffer_lower;
322 int rgb_stride, uv_stride, field_height;
323 unsigned int i, low_ofs;
325 fprintf(yuv_out, "FRAME\n");
327 if (using_format != IMGFMT_YV12)
329 rgb_stride = image_width * 3;
330 uv_stride = image_width / 2;
332 if (Y4M_IS_INTERLACED)
334 field_height = image_height / 2;
336 upper_y = image;
337 upper_u = upper_y + image_width * field_height;
338 upper_v = upper_u + image_width * field_height / 4;
339 low_ofs = image_width * field_height * 3 / 2;
340 rgb_buffer_lower = rgb_buffer + rgb_stride * field_height;
342 deinterleave_fields(rgb_buffer, rgb_stride, image_height);
344 rgb24toyv12(rgb_buffer, upper_y, upper_u, upper_v,
345 image_width, field_height,
346 image_width, uv_stride, rgb_stride);
347 rgb24toyv12(rgb_buffer_lower, upper_y + low_ofs,
348 upper_u + low_ofs, upper_v + low_ofs,
349 image_width, field_height,
350 image_width, uv_stride, rgb_stride);
352 /* Write Y plane */
353 for(i = 0; i < field_height; i++)
355 vo_y4m_write(upper_y + image_width * i, image_width);
356 vo_y4m_write(upper_y + image_width * i + low_ofs, image_width);
359 /* Write U and V plane */
360 for(i = 0; i < field_height / 2; i++)
362 vo_y4m_write(upper_u + uv_stride * i, uv_stride);
363 vo_y4m_write(upper_u + uv_stride * i + low_ofs, uv_stride);
365 for(i = 0; i < field_height / 2; i++)
367 vo_y4m_write(upper_v + uv_stride * i, uv_stride);
368 vo_y4m_write(upper_v + uv_stride * i + low_ofs, uv_stride);
370 return; /* Image written; We have to stop here */
373 rgb24toyv12(rgb_buffer, image_y, image_u, image_v,
374 image_width, image_height,
375 image_width, uv_stride, rgb_stride);
378 /* Write progressive frame */
379 vo_y4m_write(image, write_bytes);
382 static int draw_slice(uint8_t *srcimg[], int stride[], int w,int h,int x,int y)
384 int i;
385 uint8_t *dst, *src = srcimg[0];
387 switch (using_format)
389 case IMGFMT_YV12:
391 // copy Y:
392 dst = image_y + image_width * y + x;
393 for (i = 0; i < h; i++)
395 fast_memcpy(dst, src, w);
396 src += stride[0];
397 dst += image_width;
400 // copy U + V:
401 int imgstride = image_width >> 1;
402 uint8_t *src1 = srcimg[1];
403 uint8_t *src2 = srcimg[2];
404 uint8_t *dstu = image_u + imgstride * (y >> 1) + (x >> 1);
405 uint8_t *dstv = image_v + imgstride * (y >> 1) + (x >> 1);
406 for (i = 0; i < h / 2; i++)
408 fast_memcpy(dstu, src1 , w >> 1);
409 fast_memcpy(dstv, src2, w >> 1);
410 src1 += stride[1];
411 src2 += stride[2];
412 dstu += imgstride;
413 dstv += imgstride;
416 break;
418 case IMGFMT_BGR24:
419 case IMGFMT_RGB24:
420 dst = rgb_buffer + (image_width * y + x) * 3;
421 for (i = 0; i < h; i++)
423 fast_memcpy(dst, src, w * 3);
424 src += stride[0];
425 dst += image_width * 3;
427 break;
429 return 0;
432 static int draw_frame(uint8_t * src[])
434 switch(using_format)
436 case IMGFMT_YV12:
437 // gets done in draw_slice
438 break;
440 case IMGFMT_BGR24:
441 case IMGFMT_RGB24:
442 fast_memcpy(rgb_buffer, src[0], image_width * image_height * 3);
443 break;
445 return 0;
448 static int query_format(uint32_t format)
451 if (Y4M_IS_INTERLACED)
453 /* When processing interlaced material we want to get the raw RGB
454 * data and do the YV12 conversion ourselves to have the chrominance
455 * information sampled correct. */
457 switch(format)
459 case IMGFMT_YV12:
460 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
461 case IMGFMT_BGR|24:
462 case IMGFMT_RGB|24:
463 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
466 else
469 switch(format)
471 case IMGFMT_YV12:
472 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
473 case IMGFMT_BGR|24:
474 case IMGFMT_RGB|24:
475 return VFCAP_CSP_SUPPORTED|VFCAP_OSD|VFCAP_ACCEPT_STRIDE;
478 return 0;
481 // WARNING: config(...) also uses this
482 static void uninit(void)
484 if(image)
485 free(image);
486 image = NULL;
488 if(yuv_out)
489 fclose(yuv_out);
490 yuv_out = NULL;
492 if(rgb_buffer)
493 free(rgb_buffer);
494 rgb_buffer = NULL;
496 if(rgb_line_buffer)
497 free(rgb_line_buffer);
498 rgb_line_buffer = NULL;
500 if (yuv_filename)
501 free(yuv_filename);
502 yuv_filename = NULL;
503 image_width = 0;
504 image_height = 0;
505 image_fps = 0;
509 static void check_events(void)
513 static int preinit(const char *arg)
515 int il, il_bf;
516 const opt_t subopts[] = {
517 {"interlaced", OPT_ARG_BOOL, &il, NULL},
518 {"interlaced_bf", OPT_ARG_BOOL, &il_bf, NULL},
519 {"file", OPT_ARG_MSTRZ, &yuv_filename, NULL},
520 {NULL}
523 il = 0;
524 il_bf = 0;
525 yuv_filename = strdup("stream.yuv");
526 if (subopt_parse(arg, subopts) != 0) {
527 mp_msg(MSGT_VO, MSGL_FATAL, MSGTR_VO_YUV4MPEG_UnknownSubDev, arg);
528 return -1;
531 config_interlace = Y4M_ILACE_NONE;
532 if (il)
533 config_interlace = Y4M_ILACE_TOP_FIRST;
534 if (il_bf)
535 config_interlace = Y4M_ILACE_BOTTOM_FIRST;
537 /* Inform user which output mode is used */
538 switch (config_interlace)
540 case Y4M_ILACE_TOP_FIRST:
541 mp_msg(MSGT_VO,MSGL_STATUS,
542 MSGTR_VO_YUV4MPEG_InterlacedTFFMode);
543 break;
544 case Y4M_ILACE_BOTTOM_FIRST:
545 mp_msg(MSGT_VO,MSGL_STATUS,
546 MSGTR_VO_YUV4MPEG_InterlacedBFFMode);
547 break;
548 default:
549 mp_msg(MSGT_VO,MSGL_STATUS,
550 MSGTR_VO_YUV4MPEG_ProgressiveMode);
551 break;
553 return 0;
556 static int control(uint32_t request, void *data, ...)
558 switch (request) {
559 case VOCTRL_QUERY_FORMAT:
560 return query_format(*((uint32_t*)data));
561 case VOCTRL_DUPLICATE_FRAME:
562 return write_last_frame();
564 return VO_NOTIMPL;