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"
56 #include "fastmemcpy.h"
57 #include "libswscale/swscale.h"
58 #ifdef CONFIG_LIBSWSCALE_INTERNALS
59 #include "libswscale/rgb2rgb.h"
61 #include "libmpcodecs/vf_scale.h"
62 #include "libavutil/rational.h"
64 static const vo_info_t info
=
66 "yuv4mpeg output for mjpegtools",
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;
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
,
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
)
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
);
117 image_height
= height
;
120 using_format
= format
;
122 if (Y4M_IS_INTERLACED
)
126 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
127 "Interlaced mode requires image height to be divisible by 4.");
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.");
139 if (using_format
== IMGFMT_YV12
)
140 mp_tmsg(MSGT_VO
,MSGL_WARN
,
141 "Input not RGB, can't separate chrominance by fields!");
146 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
147 "Image width must be divisible by 2.");
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);
158 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
159 "Not enough memory to allocate RGB framebuffer.");
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\"!",
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
,
183 pixelaspect
.num
, pixelaspect
.den
);
189 /* Only use when h divisable by 2! */
190 static void swap_fields(uint8_t *ptr
, const int h
, const int stride
)
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
)
207 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
208 image
+ y0
* image_width
+ x0
, image_width
);
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);
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);
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
++)
246 while(k_start
< modv
)
249 fast_memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
251 while (!line_state
[j
])
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
])
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;
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
;
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
);
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;
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
);
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
);
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
)
392 uint8_t *dst
, *src
= srcimg
[0];
394 switch (using_format
)
399 dst
= image_y
+ image_width
* y
+ x
;
400 for (i
= 0; i
< h
; i
++)
402 fast_memcpy(dst
, src
, w
);
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);
427 dst
= rgb_buffer
+ (image_width
* y
+ x
) * 3;
428 for (i
= 0; i
< h
; i
++)
430 fast_memcpy(dst
, src
, w
* 3);
432 dst
+= image_width
* 3;
439 static int draw_frame(uint8_t * src
[])
444 // gets done in draw_slice
449 fast_memcpy(rgb_buffer
, src
[0], image_width
* image_height
* 3);
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. */
467 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
468 #ifdef CONFIG_LIBSWSCALE_INTERNALS
471 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
481 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
482 #ifdef CONFIG_LIBSWSCALE_INTERNALS
485 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
492 // WARNING: config(...) also uses this
493 static void uninit(void)
508 free(rgb_line_buffer
);
509 rgb_line_buffer
= NULL
;
520 static void check_events(void)
524 static int preinit(const char *arg
)
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
},
536 yuv_filename
= strdup("stream.yuv");
537 if (subopt_parse(arg
, subopts
) != 0) {
538 mp_tmsg(MSGT_VO
, MSGL_FATAL
, "Unknown subdevice: %s", arg
);
542 config_interlace
= Y4M_ILACE_NONE
;
544 config_interlace
= Y4M_ILACE_TOP_FIRST
;
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.");
555 case Y4M_ILACE_BOTTOM_FIRST
:
556 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
557 "Using interlaced output mode, bottom-field first.");
560 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
561 "Using (default) progressive frame mode.");
567 static int control(uint32_t request
, void *data
)
570 case VOCTRL_QUERY_FORMAT
:
571 return query_format(*((uint32_t*)data
));
572 case VOCTRL_DUPLICATE_FRAME
:
573 return write_last_frame();