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 #ifdef CONFIG_LIBSWSCALE_INTERNALS
60 #include "libswscale/rgb2rgb.h"
62 #include "libmpcodecs/vf_scale.h"
63 #include "libavutil/rational.h"
65 static const vo_info_t info
=
67 "yuv4mpeg output for mjpegtools",
69 "Robert Kesterson <robertk@robertk.com>",
73 const LIBVO_EXTERN (yuv4mpeg
)
75 static int image_width
= 0;
76 static int image_height
= 0;
77 static float image_fps
= 0;
79 static uint8_t *image
= NULL
;
80 static uint8_t *image_y
= NULL
;
81 static uint8_t *image_u
= NULL
;
82 static uint8_t *image_v
= NULL
;
84 static uint8_t *rgb_buffer
= NULL
;
85 static uint8_t *rgb_line_buffer
= NULL
;
87 static char *yuv_filename
= NULL
;
89 static int using_format
= 0;
91 static int write_bytes
;
93 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
94 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
95 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
97 /* Set progressive mode as default */
98 static int config_interlace
= Y4M_ILACE_NONE
;
99 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
101 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
102 uint32_t d_height
, uint32_t flags
, char *title
,
105 AVRational pixelaspect
= av_div_q((AVRational
){d_width
, d_height
},
106 (AVRational
){width
, height
});
107 AVRational fps_frac
= av_d2q(vo_fps
, vo_fps
* 1001 + 2);
108 if (image_width
== width
&& image_height
== height
&&
109 image_fps
== vo_fps
&& vo_config_count
)
111 if (vo_config_count
) {
112 mp_msg(MSGT_VO
, MSGL_WARN
,
113 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
114 "restarting output.\n",
115 image_width
, width
, image_height
, height
, image_fps
, vo_fps
);
118 image_height
= height
;
121 using_format
= format
;
123 if (Y4M_IS_INTERLACED
)
127 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
128 "Interlaced mode requires image height to be divisible by 4.");
132 rgb_line_buffer
= malloc(image_width
* 3);
133 if (!rgb_line_buffer
)
135 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
136 "Unable to allocate line buffer for interlaced mode.");
140 if (using_format
== IMGFMT_YV12
)
141 mp_tmsg(MSGT_VO
,MSGL_WARN
,
142 "Input not RGB, can't separate chrominance by fields!");
147 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
148 "Image width must be divisible by 2.");
152 #ifdef CONFIG_LIBSWSCALE_INTERNALS
153 if(using_format
!= IMGFMT_YV12
)
155 sws_rgb2rgb_init(get_sws_cpuflags());
156 rgb_buffer
= malloc(image_width
* image_height
* 3);
159 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
160 "Not enough memory to allocate RGB framebuffer.");
166 write_bytes
= image_width
* image_height
* 3 / 2;
167 image
= malloc(write_bytes
);
169 yuv_out
= fopen(yuv_filename
, "wb");
170 if (!yuv_out
|| image
== 0)
172 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
173 "Can't get memory or file handle to write \"%s\"!",
178 image_u
= image_y
+ image_width
* image_height
;
179 image_v
= image_u
+ image_width
* image_height
/ 4;
181 fprintf(yuv_out
, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
182 image_width
, image_height
, fps_frac
.num
, fps_frac
.den
,
184 pixelaspect
.num
, pixelaspect
.den
);
190 /* Only use when h divisable by 2! */
191 static void swap_fields(uint8_t *ptr
, const int h
, const int stride
)
195 for (i
=0; i
<h
; i
+=2)
197 fast_memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
198 fast_memcpy(ptr
+ stride
* i
, ptr
+ stride
* (i
+1), stride
);
199 fast_memcpy(ptr
+ stride
* (i
+1), rgb_line_buffer
, stride
);
203 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
204 unsigned char *srca
, int stride
) {
205 switch (using_format
)
208 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
209 image
+ y0
* image_width
+ x0
, image_width
);
214 if (config_interlace
!= Y4M_ILACE_BOTTOM_FIRST
)
215 vo_draw_alpha_rgb24(w
, h
, src
, srca
, stride
,
216 rgb_buffer
+ (y0
* image_width
+ x0
) * 3, image_width
* 3);
219 swap_fields (rgb_buffer
, image_height
, image_width
* 3);
221 vo_draw_alpha_rgb24(w
, h
, src
, srca
, stride
,
222 rgb_buffer
+ (y0
* image_width
+ x0
) * 3, image_width
* 3);
224 swap_fields (rgb_buffer
, image_height
, image_width
* 3);
230 static void draw_osd(void)
232 vo_draw_text(image_width
, image_height
, draw_alpha
);
235 #ifdef CONFIG_LIBSWSCALE_INTERNALS
236 static void deinterleave_fields(uint8_t *ptr
, const int stride
,
237 const int img_height
)
239 unsigned int i
, j
, k_start
= 1, modv
= img_height
- 1;
240 unsigned char *line_state
= malloc(modv
);
242 for (i
=0; i
<modv
; i
++)
247 while(k_start
< modv
)
250 fast_memcpy(rgb_line_buffer
, ptr
+ stride
* i
, stride
);
252 while (!line_state
[j
])
257 fast_memcpy(ptr
+ stride
* i
, ptr
+ stride
* j
, stride
);
259 fast_memcpy(ptr
+ stride
* i
, rgb_line_buffer
, stride
);
261 while(k_start
< modv
&& line_state
[k_start
])
268 static void vo_y4m_write(const void *ptr
, const size_t num_bytes
)
270 if (fwrite(ptr
, 1, num_bytes
, yuv_out
) != num_bytes
)
271 mp_tmsg(MSGT_VO
,MSGL_ERR
,
272 "Error writing image to output!");
275 static int write_last_frame(void)
278 uint8_t *upper_y
, *upper_u
, *upper_v
, *rgb_buffer_lower
;
279 int rgb_stride
, uv_stride
, field_height
;
280 unsigned int i
, low_ofs
;
282 fprintf(yuv_out
, "FRAME\n");
284 if (using_format
!= IMGFMT_YV12
)
286 rgb_stride
= image_width
* 3;
287 uv_stride
= image_width
/ 2;
289 if (Y4M_IS_INTERLACED
)
291 field_height
= image_height
/ 2;
294 upper_u
= upper_y
+ image_width
* field_height
;
295 upper_v
= upper_u
+ image_width
* field_height
/ 4;
296 low_ofs
= image_width
* field_height
* 3 / 2;
297 rgb_buffer_lower
= rgb_buffer
+ rgb_stride
* field_height
;
300 for(i
= 0; i
< field_height
; i
++)
302 vo_y4m_write(upper_y
+ image_width
* i
, image_width
);
303 vo_y4m_write(upper_y
+ image_width
* i
+ low_ofs
, image_width
);
306 /* Write U and V plane */
307 for(i
= 0; i
< field_height
/ 2; i
++)
309 vo_y4m_write(upper_u
+ uv_stride
* i
, uv_stride
);
310 vo_y4m_write(upper_u
+ uv_stride
* i
+ low_ofs
, uv_stride
);
312 for(i
= 0; i
< field_height
/ 2; i
++)
314 vo_y4m_write(upper_v
+ uv_stride
* i
, uv_stride
);
315 vo_y4m_write(upper_v
+ uv_stride
* i
+ low_ofs
, uv_stride
);
317 return VO_TRUE
; /* Image written; We have to stop here */
320 /* Write progressive frame */
321 vo_y4m_write(image
, write_bytes
);
325 static void flip_page (void)
327 fprintf(yuv_out
, "FRAME\n");
329 #ifdef CONFIG_LIBSWSCALE_INTERNALS
330 if (using_format
!= IMGFMT_YV12
)
332 uint8_t *upper_y
, *upper_u
, *upper_v
, *rgb_buffer_lower
;
333 int rgb_stride
, uv_stride
, field_height
;
334 unsigned int i
, low_ofs
;
336 rgb_stride
= image_width
* 3;
337 uv_stride
= image_width
/ 2;
339 if (Y4M_IS_INTERLACED
)
341 field_height
= image_height
/ 2;
344 upper_u
= upper_y
+ image_width
* field_height
;
345 upper_v
= upper_u
+ image_width
* field_height
/ 4;
346 low_ofs
= image_width
* field_height
* 3 / 2;
347 rgb_buffer_lower
= rgb_buffer
+ rgb_stride
* field_height
;
349 deinterleave_fields(rgb_buffer
, rgb_stride
, image_height
);
351 rgb24toyv12(rgb_buffer
, upper_y
, upper_u
, upper_v
,
352 image_width
, field_height
,
353 image_width
, uv_stride
, rgb_stride
);
354 rgb24toyv12(rgb_buffer_lower
, upper_y
+ low_ofs
,
355 upper_u
+ low_ofs
, upper_v
+ low_ofs
,
356 image_width
, field_height
,
357 image_width
, uv_stride
, rgb_stride
);
360 for(i
= 0; i
< field_height
; i
++)
362 vo_y4m_write(upper_y
+ image_width
* i
, image_width
);
363 vo_y4m_write(upper_y
+ image_width
* i
+ low_ofs
, image_width
);
366 /* Write U and V plane */
367 for(i
= 0; i
< field_height
/ 2; i
++)
369 vo_y4m_write(upper_u
+ uv_stride
* i
, uv_stride
);
370 vo_y4m_write(upper_u
+ uv_stride
* i
+ low_ofs
, uv_stride
);
372 for(i
= 0; i
< field_height
/ 2; i
++)
374 vo_y4m_write(upper_v
+ uv_stride
* i
, uv_stride
);
375 vo_y4m_write(upper_v
+ uv_stride
* i
+ low_ofs
, uv_stride
);
377 return; /* Image written; We have to stop here */
380 rgb24toyv12(rgb_buffer
, image_y
, image_u
, image_v
,
381 image_width
, image_height
,
382 image_width
, uv_stride
, rgb_stride
);
386 /* Write progressive frame */
387 vo_y4m_write(image
, write_bytes
);
390 static int draw_slice(uint8_t *srcimg
[], int stride
[], int w
,int h
,int x
,int y
)
393 uint8_t *dst
, *src
= srcimg
[0];
395 switch (using_format
)
400 dst
= image_y
+ image_width
* y
+ x
;
401 for (i
= 0; i
< h
; i
++)
403 fast_memcpy(dst
, src
, w
);
409 int imgstride
= image_width
>> 1;
410 uint8_t *src1
= srcimg
[1];
411 uint8_t *src2
= srcimg
[2];
412 uint8_t *dstu
= image_u
+ imgstride
* (y
>> 1) + (x
>> 1);
413 uint8_t *dstv
= image_v
+ imgstride
* (y
>> 1) + (x
>> 1);
414 for (i
= 0; i
< h
/ 2; i
++)
416 fast_memcpy(dstu
, src1
, w
>> 1);
417 fast_memcpy(dstv
, src2
, w
>> 1);
428 dst
= rgb_buffer
+ (image_width
* y
+ x
) * 3;
429 for (i
= 0; i
< h
; i
++)
431 fast_memcpy(dst
, src
, w
* 3);
433 dst
+= image_width
* 3;
440 static int draw_frame(uint8_t * src
[])
445 // gets done in draw_slice
450 fast_memcpy(rgb_buffer
, src
[0], image_width
* image_height
* 3);
456 static int query_format(uint32_t format
)
459 if (Y4M_IS_INTERLACED
)
461 /* When processing interlaced material we want to get the raw RGB
462 * data and do the YV12 conversion ourselves to have the chrominance
463 * information sampled correct. */
468 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
469 #ifdef CONFIG_LIBSWSCALE_INTERNALS
472 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
482 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
483 #ifdef CONFIG_LIBSWSCALE_INTERNALS
486 return VFCAP_CSP_SUPPORTED
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
493 // WARNING: config(...) also uses this
494 static void uninit(void)
509 free(rgb_line_buffer
);
510 rgb_line_buffer
= NULL
;
521 static void check_events(void)
525 static int preinit(const char *arg
)
528 const opt_t subopts
[] = {
529 {"interlaced", OPT_ARG_BOOL
, &il
, NULL
},
530 {"interlaced_bf", OPT_ARG_BOOL
, &il_bf
, NULL
},
531 {"file", OPT_ARG_MSTRZ
, &yuv_filename
, NULL
},
537 yuv_filename
= strdup("stream.yuv");
538 if (subopt_parse(arg
, subopts
) != 0) {
539 mp_tmsg(MSGT_VO
, MSGL_FATAL
, "Unknown subdevice: %s", arg
);
543 config_interlace
= Y4M_ILACE_NONE
;
545 config_interlace
= Y4M_ILACE_TOP_FIRST
;
547 config_interlace
= Y4M_ILACE_BOTTOM_FIRST
;
549 /* Inform user which output mode is used */
550 switch (config_interlace
)
552 case Y4M_ILACE_TOP_FIRST
:
553 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
554 "Using interlaced output mode, top-field first.");
556 case Y4M_ILACE_BOTTOM_FIRST
:
557 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
558 "Using interlaced output mode, bottom-field first.");
561 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
562 "Using (default) progressive frame mode.");
568 static int control(uint32_t request
, void *data
)
571 case VOCTRL_QUERY_FORMAT
:
572 return query_format(*((uint32_t*)data
));
573 case VOCTRL_DUPLICATE_FRAME
:
574 return write_last_frame();