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
33 #include "subopt-helper.h"
34 #include "video_out.h"
35 #include "video_out_internal.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",
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;
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
,
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
)
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
);
101 image_height
= height
;
104 using_format
= format
;
106 if (Y4M_IS_INTERLACED
)
110 mp_msg(MSGT_VO
,MSGL_FATAL
,
111 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4
);
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
);
123 if (using_format
== IMGFMT_YV12
)
124 mp_msg(MSGT_VO
,MSGL_WARN
,
125 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB
);
130 mp_msg(MSGT_VO
,MSGL_FATAL
,
131 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2
);
135 if(using_format
!= IMGFMT_YV12
)
137 sws_rgb2rgb_init(get_sws_cpuflags());
138 rgb_buffer
= malloc(image_width
* image_height
* 3);
141 mp_msg(MSGT_VO
,MSGL_FATAL
,
142 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf
);
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
,
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
,
165 pixelaspect
.num
, pixelaspect
.den
);
171 /* Only use when h divisable by 2! */
172 static void swap_fields(uint8_t *ptr
, const int h
, const int stride
)
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
)
189 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
190 image
+ y0
* image_width
+ x0
, image_width
);
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);
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);
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
++)
227 while(k_start
< modv
)
230 fast_memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
232 while (!line_state
[j
])
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
])
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;
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
;
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
);
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;
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
);
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
)
370 uint8_t *dst
, *src
= srcimg
[0];
372 switch (using_format
)
377 dst
= image_y
+ image_width
* y
+ x
;
378 for (i
= 0; i
< h
; i
++)
380 fast_memcpy(dst
, src
, w
);
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);
405 dst
= rgb_buffer
+ (image_width
* y
+ x
) * 3;
406 for (i
= 0; i
< h
; i
++)
408 fast_memcpy(dst
, src
, w
* 3);
410 dst
+= image_width
* 3;
417 static int draw_frame(uint8_t * src
[])
422 // gets done in draw_slice
427 fast_memcpy(rgb_buffer
, src
[0], image_width
* image_height
* 3);
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. */
445 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
448 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
457 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
460 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
466 // WARNING: config(...) also uses this
467 static void uninit(void)
482 free(rgb_line_buffer
);
483 rgb_line_buffer
= NULL
;
494 static void check_events(void)
498 static int preinit(const char *arg
)
502 {"interlaced", OPT_ARG_BOOL
, &il
, NULL
},
503 {"interlaced_bf", OPT_ARG_BOOL
, &il_bf
, NULL
},
504 {"file", OPT_ARG_MSTRZ
, &yuv_filename
, NULL
},
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
);
516 config_interlace
= Y4M_ILACE_NONE
;
518 config_interlace
= Y4M_ILACE_TOP_FIRST
;
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
);
529 case Y4M_ILACE_BOTTOM_FIRST
:
530 mp_msg(MSGT_VO
,MSGL_STATUS
,
531 MSGTR_VO_YUV4MPEG_InterlacedBFFMode
);
534 mp_msg(MSGT_VO
,MSGL_STATUS
,
535 MSGTR_VO_YUV4MPEG_ProgressiveMode
);
541 static int control(uint32_t request
, void *data
, ...)
544 case VOCTRL_QUERY_FORMAT
:
545 return query_format(*((uint32_t*)data
));
546 case VOCTRL_DUPLICATE_FRAME
:
547 return write_last_frame();