1 /*****************************************************************************
2 * rawvid.c : raw video input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Antoine Cellerier <dionoea at videolan d.t org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
44 #define FPS_TEXT N_("Frames per Second")
45 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
46 "playing raw video streams. In the form 30000/1001 or 29.97")
48 #define WIDTH_TEXT N_("Width")
49 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
52 #define HEIGHT_TEXT N_("Height")
53 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
56 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
57 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
59 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
60 #define ASPECT_RATIO_LONGTEXT N_( \
61 "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
64 set_shortname( "Raw Video" )
65 set_description( N_("Raw video demuxer") )
66 set_capability( "demux", 10 )
67 set_category( CAT_INPUT )
68 set_subcategory( SUBCAT_INPUT_DEMUX )
69 set_callbacks( Open, Close )
70 add_shortcut( "rawvideo" )
71 add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
72 add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
73 add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
74 add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
76 add_string( "rawvid-aspect-ratio", NULL,
77 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
80 /*****************************************************************************
81 * Definitions of structures used by this plugin
82 *****************************************************************************/
87 es_out_id_t *p_es_video;
88 es_format_t fmt_video;
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Demux( demux_t * );
99 static int Control( demux_t *, int i_query, va_list args );
110 vlc_fourcc_t i_chroma;
113 static const struct preset_t p_presets[] =
115 { "sqcif", 128, 96, 30000, 1001, 4,3, VLC_CODEC_YV12 },
116 { "qcif", 176, 144, 30000, 1001, 4,3, VLC_CODEC_YV12 },
117 { "cif", 352, 288, 30000, 1001, 4,3, VLC_CODEC_YV12 },
118 { "4cif", 704, 576, 30000, 1001, 4,3, VLC_CODEC_YV12 },
119 { "16cif", 1408, 1152, 30000, 1001, 4,3, VLC_CODEC_YV12 },
120 { "yuv", 176, 144, 25, 1, 4,3, VLC_CODEC_YV12 },
121 { NULL, 0, 0, 0, 0, 0,0, 0 }
124 /*****************************************************************************
125 * Open: initializes raw DV demux structures
126 *****************************************************************************/
127 static int Open( vlc_object_t * p_this )
129 demux_t *p_demux = (demux_t*)p_this;
131 int i_width=-1, i_height=-1;
132 unsigned u_fps_num=0, u_fps_den=1;
133 vlc_fourcc_t i_chroma = 0;
134 unsigned int i_sar_num = 0;
135 unsigned int i_sar_den = 0;
136 const struct preset_t *p_preset = NULL;
137 const uint8_t *p_peek;
140 if( stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
142 /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
143 if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
150 if( !p_demux->b_force )
152 /* guess preset based on file extension */
153 if( !p_demux->psz_file )
156 const char *psz_ext = strrchr( p_demux->psz_file, '.' );
161 for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
163 if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
165 p_preset = &p_presets[i];
172 /* Set p_input field */
173 p_demux->pf_demux = Demux;
174 p_demux->pf_control = Control;
175 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
179 p_sys->b_y4m = b_y4m;
181 /* guess the parameters based on the preset */
184 i_width = p_preset->i_width;
185 i_height = p_preset->i_height;
186 u_fps_num = p_preset->u_fps_num;
187 u_fps_den = p_preset->u_fps_den;
188 i_sar_num = p_preset->u_ar_num * p_preset->i_height;
189 i_sar_den = p_preset->u_ar_den * p_preset->i_width;
190 i_chroma = p_preset->i_chroma;
193 /* override presets if yuv4mpeg2 */
196 char *psz = stream_ReadLine( p_demux->s );
201 /* The string will start with "YUV4MPEG2" */
202 assert( strlen(psz) >= 9 );
204 /* NB, it is not possible to handle interlaced here, since the
205 * interlaced picture flags are in picture_t not block_t */
207 #define READ_FRAC( key, num, den ) do { \
208 psz_buf = strstr( psz+9, key );\
211 char *end = strchr( psz_buf+1, ' ' );\
213 if( end ) *end = '\0';\
214 sep = strchr( psz_buf+1, ':' );\
218 den = atoi( sep+1 );\
224 num = atoi( psz_buf+2 );\
225 if( sep ) *sep = ':';\
226 if( end ) *end = ' ';\
228 READ_FRAC( " W", i_width, a );
229 READ_FRAC( " H", i_height, a );
230 READ_FRAC( " F", u_fps_num, u_fps_den );
231 READ_FRAC( " A", a, b );
239 psz_buf = strstr( psz+9, " C" );
242 static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
244 { "420jpeg", VLC_CODEC_I420 },
245 { "420paldv", VLC_CODEC_I420 },
246 { "420", VLC_CODEC_I420 },
247 { "422", VLC_CODEC_I422 },
248 { "444", VLC_CODEC_I444 },
249 { "mono", VLC_CODEC_GREY },
252 bool b_found = false;
253 char *psz_end = strchr( psz_buf+1, ' ' );
258 for( int i = 0; formats[i].psz_name != NULL; i++ )
260 if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
262 i_chroma = formats[i].i_fcc;
268 msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
276 /* allow the user to override anything guessed from the input */
278 i_tmp = var_CreateGetInteger( p_demux, "rawvid-width" );
279 if( i_tmp ) i_width = i_tmp;
281 i_tmp = var_CreateGetInteger( p_demux, "rawvid-height" );
282 if( i_tmp ) i_height = i_tmp;
285 psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
288 if( strlen( psz_tmp ) != 4 )
290 msg_Err( p_demux, "Invalid fourcc format/chroma specification %s"
291 " expecting four characters eg, UYVY", psz_tmp );
295 memcpy( &i_chroma, psz_tmp, 4 );
296 msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
300 psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-fps" );
304 /* fps can either be n/d or q.f
305 * for accuracy, avoid representation in float */
306 u_fps_num = strtol( psz_tmp, &p_ptr, 10 );
310 u_fps_den = strtol( p_ptr, NULL, 10 );
312 else if( *p_ptr == '.' )
316 int i_frac = strtol( p_ptr, &p_end, 10 );
317 u_fps_den = (p_end - p_ptr) * 10;
320 u_fps_num = u_fps_num * u_fps_den + i_frac;
322 else if( *p_ptr == '\0')
329 psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-aspect-ratio" );
332 char *psz_denominator = strchr( psz_tmp, ':' );
333 if( psz_denominator )
335 *psz_denominator++ = '\0';
336 i_sar_num = atoi( psz_tmp ) * i_height;
337 i_sar_den = atoi( psz_denominator ) * i_width;
342 /* moan about anything wrong */
343 if( i_width <= 0 || i_height <= 0 )
345 msg_Err( p_demux, "width and height must be strictly positive." );
349 if( !u_fps_num || !u_fps_den )
351 msg_Err( p_demux, "invalid or no framerate specified." );
357 msg_Err( p_demux, "invalid or no chroma specified." );
361 /* fixup anything missing with sensible assumptions */
362 if( i_sar_num <= 0 || i_sar_den <= 0 )
369 es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
370 video_format_Setup( &p_sys->fmt_video.video,
371 i_chroma, i_width, i_height,
372 i_sar_num, i_sar_den );
374 vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
375 &p_sys->fmt_video.video.i_frame_rate_base,
376 u_fps_num, u_fps_den, 0);
377 date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
378 p_sys->fmt_video.video.i_frame_rate_base );
379 date_Set( &p_sys->pcr, 0 );
381 if( !p_sys->fmt_video.video.i_bits_per_pixel )
383 msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
387 p_sys->frame_size = i_width * i_height
388 * p_sys->fmt_video.video.i_bits_per_pixel / 8;
389 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
394 stream_Seek( p_demux->s, 0 ); // Workaround, but y4m uses stream_ReadLines
399 /*****************************************************************************
400 * Close: frees unused data
401 *****************************************************************************/
402 static void Close( vlc_object_t *p_this )
404 demux_t *p_demux = (demux_t*)p_this;
405 demux_sys_t *p_sys = p_demux->p_sys;
409 /*****************************************************************************
410 * Demux: reads and demuxes data packets
411 *****************************************************************************
412 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
413 *****************************************************************************/
414 static int Demux( demux_t *p_demux )
416 demux_sys_t *p_sys = p_demux->p_sys;
418 mtime_t i_pcr = date_Get( &p_sys->pcr );
420 /* Call the pace control */
421 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_pcr );
425 /* Skip the frame header */
427 if( stream_Read( p_demux->s, NULL, 5 ) < 5 )
433 if( stream_Read( p_demux->s, &b, 1 ) < 1 )
440 if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
446 p_block->i_dts = p_block->i_pts = VLC_TS_0 + i_pcr;
447 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
449 date_Increment( &p_sys->pcr, 1 );
454 /*****************************************************************************
456 *****************************************************************************/
457 static int Control( demux_t *p_demux, int i_query, va_list args )
459 demux_sys_t *p_sys = p_demux->p_sys;
461 /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
462 const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
463 p_sys->pcr.i_divider_den;
465 /* XXX: DEMUX_SET_TIME is precise here */
466 return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
467 p_sys->frame_size, i_query, args );