1 /*****************************************************************************
2 * yuv.c : yuv video output
3 *****************************************************************************
4 * Copyright (C) 2008, M2X BV
7 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_charset.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Create ( vlc_object_t * );
41 static void Destroy ( vlc_object_t * );
43 static int Init ( vout_thread_t * );
44 static void End ( vout_thread_t *p_vout );
45 static void Display ( vout_thread_t *, picture_t * );
47 /*****************************************************************************
49 *****************************************************************************/
51 #define YUV_FILE_TEXT N_("device, fifo or filename")
52 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too." )
54 #define CHROMA_TEXT N_("Chroma used.")
55 #define CHROMA_LONGTEXT N_( \
56 "Force use of a specific chroma for output. Default is I420." )
58 #define YUV4MPEG2_TEXT N_( "YUV4MPEG2 header (default disabled)" )
59 #define YUV4MPEG2_LONGTEXT N_( "The YUV4MPEG2 header is compatible " \
60 "with mplayer yuv video ouput and requires YV12/I420 fourcc. By default "\
61 "vlc writes the fourcc of the picture frame into the output destination." )
63 #define CFG_PREFIX "yuv-"
66 set_shortname( N_( "YUV output" ) )
67 set_description( N_( "YUV video output" ) )
68 set_category( CAT_VIDEO )
69 set_subcategory( SUBCAT_VIDEO_VOUT )
70 set_capability( "video output", 0 )
72 add_string( CFG_PREFIX "file", "stream.yuv", NULL,
73 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false )
74 add_string( CFG_PREFIX "chroma", NULL, NULL,
75 CHROMA_TEXT, CHROMA_LONGTEXT, true )
76 add_bool ( CFG_PREFIX "yuv4mpeg2", false, NULL,
77 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true )
79 set_callbacks( Create, Destroy )
82 static const char *const ppsz_vout_options[] = {
83 "file", "chroma", "yuv4mpeg2", NULL
86 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
89 /*****************************************************************************
90 * vout_sys_t: video output descriptor
91 *****************************************************************************/
98 vlc_fourcc_t i_chroma;
101 /*****************************************************************************
102 * Create: allocates video thread
103 *****************************************************************************
104 * This function allocates and initializes a vout method.
105 *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
108 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
112 config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
115 /* Allocate instance and initialize some members */
116 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
120 p_sys->b_header = false;
123 p_sys->b_yuv4mpeg2 = var_CreateGetBool( p_this, CFG_PREFIX "yuv4mpeg2" );
124 p_sys->i_chroma = VLC_CODEC_I420;
127 var_CreateGetString( p_this, CFG_PREFIX "file" );
128 p_sys->p_fd = utf8_fopen( p_sys->psz_file, "wb" );
131 free( p_sys->psz_file );
136 psz_fcc = var_CreateGetNonEmptyString( p_this, CFG_PREFIX "chroma" );
137 if( psz_fcc && (strlen( psz_fcc ) == 4) )
139 p_sys->i_chroma = vlc_fourcc_GetCodec( VIDEO_ES,
140 VLC_FOURCC( psz_fcc[0], psz_fcc[1],
141 psz_fcc[2], psz_fcc[3] ) );
145 if( p_sys->b_yuv4mpeg2 )
147 switch( p_sys->i_chroma )
155 "YUV4MPEG2 mode needs chroma YV12 not %4s as requested",
156 (char *)&(p_sys->i_chroma) );
157 fclose( p_vout->p_sys->p_fd );
158 free( p_vout->p_sys->psz_file );
159 free( p_vout->p_sys );
164 msg_Dbg( p_this, "using chroma %4s", (char *)&(p_sys->i_chroma) );
166 p_vout->pf_init = Init;
167 p_vout->pf_end = End;
168 p_vout->pf_manage = NULL;
169 p_vout->pf_render = NULL;
170 p_vout->pf_display = Display;
175 /*****************************************************************************
176 * Init: initialize video thread
177 *****************************************************************************/
178 static int Init( vout_thread_t *p_vout )
180 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
184 /* Initialize the output structure */
185 if( p_vout->render.i_chroma != p_sys->i_chroma )
186 p_vout->output.i_chroma = p_vout->fmt_out.i_chroma = p_sys->i_chroma;
188 p_vout->output.i_chroma = p_vout->render.i_chroma;
190 p_vout->output.pf_setpalette = NULL;
191 p_vout->output.i_width = p_vout->render.i_width;
192 p_vout->output.i_height = p_vout->render.i_height;
193 p_vout->output.i_aspect = p_vout->output.i_width
194 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
196 p_vout->output.i_rmask = 0xff0000;
197 p_vout->output.i_gmask = 0x00ff00;
198 p_vout->output.i_bmask = 0x0000ff;
200 /* Try to initialize 1 direct buffer */
203 /* Find an empty picture slot */
204 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
206 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
208 p_pic = p_vout->p_picture + i_index;
213 /* Allocate the picture */
217 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
218 p_vout->output.i_width, p_vout->output.i_height,
219 p_vout->output.i_aspect );
221 if( p_pic->i_planes == 0 )
226 p_pic->i_status = DESTROYED_PICTURE;
227 p_pic->i_type = DIRECT_PICTURE;
229 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
234 /*****************************************************************************
235 * Destroy: destroy video thread
236 *****************************************************************************
237 * Terminate an output method created by Create
238 *****************************************************************************/
239 static void Destroy( vlc_object_t *p_this )
242 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
244 for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
246 free( PP_OUTPUTPICTURE[ i_index ]->p_data );
249 /* Destroy structure */
250 fclose( p_vout->p_sys->p_fd );
252 free( p_vout->p_sys->psz_file );
253 free( p_vout->p_sys );
256 /*****************************************************************************
257 * Display: displays previously rendered output
258 *****************************************************************************
259 * This function copies the rendered picture into our circular buffer.
260 *****************************************************************************/
261 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
263 video_format_t fmt_in;
265 memset( &fmt_in, 0, sizeof( fmt_in ) );
266 video_format_Copy( &fmt_in, &p_pic->format );
267 /* assume square pixels if i_sar_num = 0 */
268 if( p_pic->format.i_sar_num == 0 )
269 fmt_in.i_sar_num = fmt_in.i_sar_den = 1;
271 WriteYUV( p_vout, fmt_in, p_pic );
272 video_format_Clean( &fmt_in );
276 static void End( vout_thread_t *p_vout )
281 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
284 vout_sys_t *p_sys = p_vout->p_sys;
286 if( !p_pic || !p_sys ) return;
289 msg_Dbg( p_vout, "writing %d bytes of fourcc %4s",
290 i_bytes, (char *)&fmt.i_chroma );
292 if( !p_sys->b_header )
294 const char *p_hdr = "";
295 if( p_sys->b_yuv4mpeg2 )
297 /* MPlayer compatible header, unfortunately it doesn't tell you
298 * the exact fourcc used. */
303 p_hdr = (const char*)&fmt.i_chroma;
305 fprintf( p_sys->p_fd, "%4s W%d H%d F%d:%d I%c A%d:%d\n",
307 fmt.i_width, fmt.i_height,
308 fmt.i_frame_rate, fmt.i_frame_rate_base,
309 (p_pic->b_progressive ? 'p' :
310 (p_pic->b_top_field_first ? 't' : 'b')),
311 fmt.i_sar_num, fmt.i_sar_den );
312 fflush( p_sys->p_fd );
313 p_sys->b_header = true;
316 fprintf( p_sys->p_fd, "FRAME\n" );
317 if( p_pic->b_progressive )
319 size_t i_bytes = (fmt.i_width * fmt.i_height * fmt.i_bits_per_pixel) >> 3;
320 size_t i_written = 0;
322 i_written = fwrite( p_pic->p_data, 1, i_bytes, p_sys->p_fd );
323 if( i_written != i_bytes )
325 msg_Warn( p_vout, "only %d of %d bytes written",
326 i_written, i_bytes );
331 msg_Warn( p_vout, "only progressive frames are supported, "
332 "use a deinterlace filter" );
334 fflush( p_sys->p_fd );