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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_charset.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static int Init ( vout_thread_t * );
46 static void End ( vout_thread_t *p_vout );
47 static void Display ( vout_thread_t *, picture_t * );
49 /*****************************************************************************
51 *****************************************************************************/
53 #define YUV_FILE_TEXT N_("device, fifo or filename")
54 #define YUV_FILE_LONGTEXT N_("device, fifo or filename to write yuv frames too." )
56 #define CHROMA_TEXT N_("Chroma used.")
57 #define CHROMA_LONGTEXT N_( \
58 "Force use of a specific chroma for output. Default is I420." )
60 #define YUV4MPEG2_TEXT N_( "YUV4MPEG2 header (default disabled)" )
61 #define YUV4MPEG2_LONGTEXT N_( "The YUV4MPEG2 header is compatible " \
62 "with mplayer yuv video ouput and requires YV12/I420 fourcc. By default "\
63 "vlc writes the fourcc of the picture frame into the output destination." )
65 #define CFG_PREFIX "yuv-"
68 set_shortname( N_( "YUV output" ) );
69 set_description( N_( "YUV video output" ) );
70 set_category( CAT_VIDEO );
71 set_subcategory( SUBCAT_VIDEO_VOUT );
72 set_capability( "video output", 0 );
74 add_string( CFG_PREFIX "file", "stream.yuv", NULL,
75 YUV_FILE_TEXT, YUV_FILE_LONGTEXT, false );
76 add_string( CFG_PREFIX "chroma", NULL, NULL,
77 CHROMA_TEXT, CHROMA_LONGTEXT, true );
78 add_bool ( CFG_PREFIX "yuv4mpeg2", false, NULL,
79 YUV4MPEG2_TEXT, YUV4MPEG2_LONGTEXT, true );
81 set_callbacks( Create, Destroy );
84 static const char *const ppsz_vout_options[] = {
85 "file", "chroma", "yuv4mpeg2", NULL
88 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
91 /*****************************************************************************
92 * vout_sys_t: video output descriptor
93 *****************************************************************************/
100 vlc_fourcc_t i_chroma;
103 /*****************************************************************************
104 * Create: allocates video thread
105 *****************************************************************************
106 * This function allocates and initializes a vout method.
107 *****************************************************************************/
108 static int Create( vlc_object_t *p_this )
110 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
114 config_ChainParse( p_vout, CFG_PREFIX, ppsz_vout_options,
117 /* Allocate instance and initialize some members */
118 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
122 p_sys->b_header = false;
125 p_sys->b_yuv4mpeg2 = var_CreateGetBool( p_this, CFG_PREFIX "yuv4mpeg2" );
126 p_sys->i_chroma = VLC_FOURCC('I','4','2','0');
129 var_CreateGetString( p_this, CFG_PREFIX "file" );
130 p_sys->p_fd = utf8_fopen( p_sys->psz_file, "wb" );
133 free( p_sys->psz_file );
138 psz_fcc = var_CreateGetNonEmptyString( p_this, CFG_PREFIX "chroma" );
139 if( psz_fcc && (strlen( psz_fcc ) == 4) )
141 p_sys->i_chroma = VLC_FOURCC( psz_fcc[0], psz_fcc[1],
142 psz_fcc[2], psz_fcc[3] );
146 if( p_sys->b_yuv4mpeg2 )
148 switch( p_sys->i_chroma )
150 case VLC_FOURCC('Y','V','1','2'):
151 case VLC_FOURCC('I','4','2','0'):
152 case VLC_FOURCC('I','Y','U','V'):
153 case VLC_FOURCC('J','4','2','0'):
157 "YUV4MPEG2 mode needs chroma YV12 not %4s as requested",
158 (char *)&(p_sys->i_chroma) );
159 fclose( p_vout->p_sys->p_fd );
160 free( p_vout->p_sys->psz_file );
161 free( p_vout->p_sys );
166 msg_Dbg( p_this, "using chroma %4s", (char *)&(p_sys->i_chroma) );
168 p_vout->pf_init = Init;
169 p_vout->pf_end = End;
170 p_vout->pf_manage = NULL;
171 p_vout->pf_render = NULL;
172 p_vout->pf_display = Display;
177 /*****************************************************************************
178 * Init: initialize video thread
179 *****************************************************************************/
180 static int Init( vout_thread_t *p_vout )
182 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
186 /* Initialize the output structure */
187 if( p_vout->render.i_chroma != p_sys->i_chroma )
188 p_vout->output.i_chroma = p_vout->fmt_out.i_chroma = p_sys->i_chroma;
190 p_vout->output.i_chroma = p_vout->render.i_chroma;
192 p_vout->output.pf_setpalette = NULL;
193 p_vout->output.i_width = p_vout->render.i_width;
194 p_vout->output.i_height = p_vout->render.i_height;
195 p_vout->output.i_aspect = p_vout->output.i_width
196 * VOUT_ASPECT_FACTOR / p_vout->output.i_height;
198 p_vout->output.i_rmask = 0xff0000;
199 p_vout->output.i_gmask = 0x00ff00;
200 p_vout->output.i_bmask = 0x0000ff;
202 /* Try to initialize 1 direct buffer */
205 /* Find an empty picture slot */
206 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
208 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
210 p_pic = p_vout->p_picture + i_index;
215 /* Allocate the picture */
219 vout_AllocatePicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
220 p_vout->output.i_width, p_vout->output.i_height,
221 p_vout->output.i_aspect );
223 if( p_pic->i_planes == 0 )
228 p_pic->i_status = DESTROYED_PICTURE;
229 p_pic->i_type = DIRECT_PICTURE;
231 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
236 /*****************************************************************************
237 * Destroy: destroy video thread
238 *****************************************************************************
239 * Terminate an output method created by Create
240 *****************************************************************************/
241 static void Destroy( vlc_object_t *p_this )
244 vout_thread_t *p_vout = ( vout_thread_t * )p_this;
246 for( i_index = I_OUTPUTPICTURES-1; i_index >= 0; i_index-- )
248 free( PP_OUTPUTPICTURE[ i_index ]->p_data );
251 /* Destroy structure */
252 fclose( p_vout->p_sys->p_fd );
254 free( p_vout->p_sys->psz_file );
255 free( p_vout->p_sys );
258 /*****************************************************************************
259 * Display: displays previously rendered output
260 *****************************************************************************
261 * This function copies the rendered picture into our circular buffer.
262 *****************************************************************************/
263 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
265 video_format_t fmt_in;
267 memset( &fmt_in, 0, sizeof( fmt_in ) );
268 video_format_Copy( &fmt_in, &p_pic->format );
269 /* assume square pixels if i_sar_num = 0 */
270 if( p_pic->format.i_sar_num == 0 )
271 fmt_in.i_sar_num = fmt_in.i_sar_den = 1;
273 WriteYUV( p_vout, fmt_in, p_pic );
274 video_format_Clean( &fmt_in );
278 static void End( vout_thread_t *p_vout )
283 static void WriteYUV( vout_thread_t *p_vout, video_format_t fmt,
286 vout_sys_t *p_sys = p_vout->p_sys;
288 if( !p_pic || !p_sys ) return;
291 msg_Dbg( p_vout, "writing %d bytes of fourcc %4s",
292 i_bytes, (char *)&fmt.i_chroma );
294 if( !p_sys->b_header )
296 const char *p_hdr = "";
297 if( p_sys->b_yuv4mpeg2 )
299 /* MPlayer compatible header, unfortunately it doesn't tell you
300 * the exact fourcc used. */
305 p_hdr = (const char*)&fmt.i_chroma;
307 fprintf( p_sys->p_fd, "%4s W%d H%d F%d:%d I%c A%d:%d\n",
309 fmt.i_width, fmt.i_height,
310 fmt.i_frame_rate, fmt.i_frame_rate_base,
311 (p_pic->b_progressive ? 'p' :
312 (p_pic->b_top_field_first ? 't' : 'b')),
313 fmt.i_sar_num, fmt.i_sar_den );
314 fflush( p_sys->p_fd );
315 p_sys->b_header = true;
318 fprintf( p_sys->p_fd, "FRAME\n" );
319 if( p_pic->b_progressive )
321 size_t i_bytes = (fmt.i_width * fmt.i_height * fmt.i_bits_per_pixel) >> 3;
322 size_t i_written = 0;
324 i_written = fwrite( p_pic->p_data, 1, i_bytes, p_sys->p_fd );
325 if( i_written != i_bytes )
327 msg_Warn( p_vout, "only %d of %d bytes written",
328 i_written, i_bytes );
333 msg_Warn( p_vout, "only progressive frames are supported, "
334 "use a deinterlace filter" );
336 fflush( p_sys->p_fd );