2 * vo_yuv4mpeg.c, yuv4mpeg (mjpegtools) interface
5 * Robert Kesterson <robertk@robertk.com>
6 * Based on the pgm output plugin, the rgb2rgb postproc filter, divxdec,
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
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
32 #include "subopt-helper.h"
33 #include "video_out.h"
34 #include "video_out_internal.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",
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;
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
,
85 if (image_width
== width
&& image_height
== height
&&
86 image_fps
== vo_fps
&& vo_config_count
)
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
);
95 image_height
= height
;
98 using_format
= format
;
100 if (Y4M_IS_INTERLACED
)
104 mp_msg(MSGT_VO
,MSGL_FATAL
,
105 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4
);
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
);
117 if (using_format
== IMGFMT_YV12
)
118 mp_msg(MSGT_VO
,MSGL_WARN
,
119 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB
);
124 mp_msg(MSGT_VO
,MSGL_FATAL
,
125 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2
);
129 if(using_format
!= IMGFMT_YV12
)
131 sws_rgb2rgb_init(get_sws_cpuflags());
132 rgb_buffer
= malloc(image_width
* image_height
* 3);
135 mp_msg(MSGT_VO
,MSGL_FATAL
,
136 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf
);
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
,
153 image_u
= image_y
+ image_width
* image_height
;
154 image_v
= image_u
+ image_width
* image_height
/ 4;
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
);
169 /* Only use when h divisable by 2! */
170 static void swap_fields(uint8_t *ptr
, const int h
, const int stride
)
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
)
187 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
188 image
+ y0
* image_width
+ x0
, image_width
);
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);
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);
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
++)
225 while(k_start
< modv
)
228 memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
230 while (!line_state
[j
])
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
])
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;
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
;
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
);
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;
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
);
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
)
368 uint8_t *dst
, *src
= srcimg
[0];
370 switch (using_format
)
375 dst
= image_y
+ image_width
* y
+ x
;
376 for (i
= 0; i
< h
; i
++)
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);
403 dst
= rgb_buffer
+ (image_width
* y
+ x
) * 3;
404 for (i
= 0; i
< h
; i
++)
406 memcpy(dst
, src
, w
* 3);
408 dst
+= image_width
* 3;
415 static int draw_frame(uint8_t * src
[])
420 // gets done in draw_slice
425 memcpy(rgb_buffer
, src
[0], image_width
* image_height
* 3);
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. */
443 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
446 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
455 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
458 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
464 // WARNING: config(...) also uses this
465 static void uninit(void)
480 free(rgb_line_buffer
);
481 rgb_line_buffer
= NULL
;
492 static void check_events(void)
496 static int preinit(const char *arg
)
500 {"interlaced", OPT_ARG_BOOL
, &il
, NULL
},
501 {"interlaced_bf", OPT_ARG_BOOL
, &il_bf
, NULL
},
502 {"file", OPT_ARG_MSTRZ
, &yuv_filename
, NULL
},
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
);
514 config_interlace
= Y4M_ILACE_NONE
;
516 config_interlace
= Y4M_ILACE_TOP_FIRST
;
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
);
527 case Y4M_ILACE_BOTTOM_FIRST
:
528 mp_msg(MSGT_VO
,MSGL_STATUS
,
529 MSGTR_VO_YUV4MPEG_InterlacedBFFMode
);
532 mp_msg(MSGT_VO
,MSGL_STATUS
,
533 MSGTR_VO_YUV4MPEG_ProgressiveMode
);
539 static int control(uint32_t request
, void *data
, ...)
542 case VOCTRL_QUERY_FORMAT
:
543 return query_format(*((uint32_t*)data
));
544 case VOCTRL_DUPLICATE_FRAME
:
545 return write_last_frame();