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,
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
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.
48 #include "subopt-helper.h"
49 #include "video_out.h"
50 #include "video_out_internal.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",
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;
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
,
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
)
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
);
116 image_height
= height
;
119 using_format
= format
;
121 if (Y4M_IS_INTERLACED
)
125 mp_msg(MSGT_VO
,MSGL_FATAL
,
126 MSGTR_VO_YUV4MPEG_InterlacedHeightDivisibleBy4
);
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
);
138 if (using_format
== IMGFMT_YV12
)
139 mp_msg(MSGT_VO
,MSGL_WARN
,
140 MSGTR_VO_YUV4MPEG_InterlacedInputNotRGB
);
145 mp_msg(MSGT_VO
,MSGL_FATAL
,
146 MSGTR_VO_YUV4MPEG_WidthDivisibleBy2
);
150 if(using_format
!= IMGFMT_YV12
)
152 sws_rgb2rgb_init(get_sws_cpuflags());
153 rgb_buffer
= malloc(image_width
* image_height
* 3);
156 mp_msg(MSGT_VO
,MSGL_FATAL
,
157 MSGTR_VO_YUV4MPEG_NoMemRGBFrameBuf
);
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
,
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
,
180 pixelaspect
.num
, pixelaspect
.den
);
186 /* Only use when h divisable by 2! */
187 static void swap_fields(uint8_t *ptr
, const int h
, const int stride
)
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
)
204 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
205 image
+ y0
* image_width
+ x0
, image_width
);
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);
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);
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
++)
242 while(k_start
< modv
)
245 fast_memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
247 while (!line_state
[j
])
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
])
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;
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
;
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
);
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;
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
);
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
)
385 uint8_t *dst
, *src
= srcimg
[0];
387 switch (using_format
)
392 dst
= image_y
+ image_width
* y
+ x
;
393 for (i
= 0; i
< h
; i
++)
395 fast_memcpy(dst
, src
, w
);
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);
420 dst
= rgb_buffer
+ (image_width
* y
+ x
) * 3;
421 for (i
= 0; i
< h
; i
++)
423 fast_memcpy(dst
, src
, w
* 3);
425 dst
+= image_width
* 3;
432 static int draw_frame(uint8_t * src
[])
437 // gets done in draw_slice
442 fast_memcpy(rgb_buffer
, src
[0], image_width
* image_height
* 3);
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. */
460 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
463 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
472 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
475 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
481 // WARNING: config(...) also uses this
482 static void uninit(void)
497 free(rgb_line_buffer
);
498 rgb_line_buffer
= NULL
;
509 static void check_events(void)
513 static int preinit(const char *arg
)
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
},
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
);
531 config_interlace
= Y4M_ILACE_NONE
;
533 config_interlace
= Y4M_ILACE_TOP_FIRST
;
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
);
544 case Y4M_ILACE_BOTTOM_FIRST
:
545 mp_msg(MSGT_VO
,MSGL_STATUS
,
546 MSGTR_VO_YUV4MPEG_InterlacedBFFMode
);
549 mp_msg(MSGT_VO
,MSGL_STATUS
,
550 MSGTR_VO_YUV4MPEG_ProgressiveMode
);
556 static int control(uint32_t request
, void *data
, ...)
559 case VOCTRL_QUERY_FORMAT
:
560 return query_format(*((uint32_t*)data
));
561 case VOCTRL_DUPLICATE_FRAME
:
562 return write_last_frame();