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 "libavutil/rational.h"
59 static const vo_info_t info
=
61 "yuv4mpeg output for mjpegtools",
63 "Robert Kesterson <robertk@robertk.com>",
67 const LIBVO_EXTERN (yuv4mpeg
)
69 static int image_width
= 0;
70 static int image_height
= 0;
71 static float image_fps
= 0;
73 static uint8_t *image
= NULL
;
74 static uint8_t *image_y
= NULL
;
75 static uint8_t *image_u
= NULL
;
76 static uint8_t *image_v
= NULL
;
78 static char *yuv_filename
= NULL
;
80 static int using_format
= 0;
82 static int write_bytes
;
84 #define Y4M_ILACE_NONE 'p' /* non-interlaced, progressive frame */
85 #define Y4M_ILACE_TOP_FIRST 't' /* interlaced, top-field first */
86 #define Y4M_ILACE_BOTTOM_FIRST 'b' /* interlaced, bottom-field first */
88 /* Set progressive mode as default */
89 static int config_interlace
= Y4M_ILACE_NONE
;
90 #define Y4M_IS_INTERLACED (config_interlace != Y4M_ILACE_NONE)
92 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
93 uint32_t d_height
, uint32_t flags
, char *title
,
96 AVRational pixelaspect
= av_div_q((AVRational
){d_width
, d_height
},
97 (AVRational
){width
, height
});
98 AVRational fps_frac
= av_d2q(vo_fps
, vo_fps
* 1001 + 2);
99 if (image_width
== width
&& image_height
== height
&&
100 image_fps
== vo_fps
&& vo_config_count
)
102 if (vo_config_count
) {
103 mp_msg(MSGT_VO
, MSGL_WARN
,
104 "Video formats differ (w:%i=>%i, h:%i=>%i, fps:%f=>%f), "
105 "restarting output.\n",
106 image_width
, width
, image_height
, height
, image_fps
, vo_fps
);
109 image_height
= height
;
112 using_format
= format
;
114 if (Y4M_IS_INTERLACED
)
118 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
119 "Interlaced mode requires image height to be divisible by 4.");
126 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
127 "Image width must be divisible by 2.");
131 write_bytes
= image_width
* image_height
* 3 / 2;
132 image
= malloc(write_bytes
);
134 yuv_out
= fopen(yuv_filename
, "wb");
135 if (!yuv_out
|| image
== 0)
137 mp_tmsg(MSGT_VO
,MSGL_FATAL
,
138 "Can't get memory or file handle to write \"%s\"!",
143 image_u
= image_y
+ image_width
* image_height
;
144 image_v
= image_u
+ image_width
* image_height
/ 4;
146 fprintf(yuv_out
, "YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d\n",
147 image_width
, image_height
, fps_frac
.num
, fps_frac
.den
,
149 pixelaspect
.num
, pixelaspect
.den
);
155 static void draw_alpha(int x0
, int y0
, int w
, int h
, unsigned char *src
,
156 unsigned char *srca
, int stride
) {
157 vo_draw_alpha_yv12(w
, h
, src
, srca
, stride
,
158 image
+ y0
* image_width
+ x0
, image_width
);
161 static void draw_osd(void)
163 vo_draw_text(image_width
, image_height
, draw_alpha
);
166 static void vo_y4m_write(const void *ptr
, const size_t num_bytes
)
168 if (fwrite(ptr
, 1, num_bytes
, yuv_out
) != num_bytes
)
169 mp_tmsg(MSGT_VO
,MSGL_ERR
,
170 "Error writing image to output!");
173 static int write_last_frame(void)
175 fprintf(yuv_out
, "FRAME\n");
177 vo_y4m_write(image
, write_bytes
);
181 static void flip_page (void)
183 fprintf(yuv_out
, "FRAME\n");
185 vo_y4m_write(image
, write_bytes
);
188 static int draw_slice(uint8_t *srcimg
[], int stride
[], int w
,int h
,int x
,int y
)
191 uint8_t *dst
, *src
= srcimg
[0];
194 dst
= image_y
+ image_width
* y
+ x
;
195 for (i
= 0; i
< h
; i
++)
197 fast_memcpy(dst
, src
, w
);
203 int imgstride
= image_width
>> 1;
204 uint8_t *src1
= srcimg
[1];
205 uint8_t *src2
= srcimg
[2];
206 uint8_t *dstu
= image_u
+ imgstride
* (y
>> 1) + (x
>> 1);
207 uint8_t *dstv
= image_v
+ imgstride
* (y
>> 1) + (x
>> 1);
208 for (i
= 0; i
< h
/ 2; i
++)
210 fast_memcpy(dstu
, src1
, w
>> 1);
211 fast_memcpy(dstv
, src2
, w
>> 1);
221 static int draw_frame(uint8_t * src
[])
223 // gets done in draw_slice
227 static int query_format(uint32_t format
)
229 if (format
== IMGFMT_YV12
)
230 return VFCAP_CSP_SUPPORTED
|VFCAP_CSP_SUPPORTED_BY_HW
|VFCAP_OSD
|VFCAP_ACCEPT_STRIDE
;
234 // WARNING: config(...) also uses this
235 static void uninit(void)
254 static void check_events(void)
258 static int preinit(const char *arg
)
261 const opt_t subopts
[] = {
262 {"interlaced", OPT_ARG_BOOL
, &il
, NULL
},
263 {"interlaced_bf", OPT_ARG_BOOL
, &il_bf
, NULL
},
264 {"file", OPT_ARG_MSTRZ
, &yuv_filename
, NULL
},
270 yuv_filename
= strdup("stream.yuv");
271 if (subopt_parse(arg
, subopts
) != 0) {
272 mp_tmsg(MSGT_VO
, MSGL_FATAL
, "Unknown subdevice: %s", arg
);
276 config_interlace
= Y4M_ILACE_NONE
;
278 config_interlace
= Y4M_ILACE_TOP_FIRST
;
280 config_interlace
= Y4M_ILACE_BOTTOM_FIRST
;
282 /* Inform user which output mode is used */
283 switch (config_interlace
)
285 case Y4M_ILACE_TOP_FIRST
:
286 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
287 "Using interlaced output mode, top-field first.");
289 case Y4M_ILACE_BOTTOM_FIRST
:
290 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
291 "Using interlaced output mode, bottom-field first.");
294 mp_tmsg(MSGT_VO
,MSGL_STATUS
,
295 "Using (default) progressive frame mode.");
301 static int control(uint32_t request
, void *data
)
304 case VOCTRL_QUERY_FORMAT
:
305 return query_format(*((uint32_t*)data
));
306 case VOCTRL_DUPLICATE_FRAME
:
307 return write_last_frame();