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>
36 #include <vlc_vout.h> /* vout_InitFormat */
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.")
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 is 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_float( "rawvid-fps", 0, 0, FPS_TEXT, FPS_LONGTEXT, false );
72 add_integer( "rawvid-width", 0, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 );
73 add_integer( "rawvid-height", 0, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 );
74 add_string( "rawvid-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
76 add_string( "rawvid-aspect-ratio", NULL, NULL,
77 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true );
80 /*****************************************************************************
81 * Definitions of structures used by this plugin
82 *****************************************************************************/
88 es_out_id_t *p_es_video;
89 es_format_t fmt_video;
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Demux( demux_t * );
100 static int Control( demux_t *, int i_query, va_list args );
108 const char *psz_aspect_ratio;
109 const char *psz_chroma;
112 static struct preset_t p_presets[] =
114 { "sqcif", 128, 96, 29.97, "4:3", "YV12" },
115 { "qcif", 176, 144, 29.97, "4:3", "YV12" },
116 { "cif", 352, 288, 29.97, "4:3", "YV12" },
117 { "4cif", 704, 576, 29.97, "4:3", "YV12" },
118 { "16cif", 1408, 1152, 29.97, "4:3", "YV12" },
119 { "yuv", 176, 144, 25, "4:3", "YV12" },
120 { "", 0, 0, 0., "", "" }
123 /*****************************************************************************
124 * Open: initializes raw DV demux structures
125 *****************************************************************************/
126 static int Open( vlc_object_t * p_this )
128 demux_t *p_demux = (demux_t*)p_this;
130 int i_width, i_height;
134 char *psz_aspect_ratio;
135 unsigned int i_aspect = 0;
136 struct preset_t *p_preset = NULL;
137 const uint8_t *p_peek;
138 bool b_valid = false;
141 if( stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
143 /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
144 if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
151 psz_ext = strrchr( p_demux->psz_path, '.' );
155 for( p_preset = p_presets; *p_preset->psz_ext; p_preset++ )
156 if( !strcasecmp( psz_ext, p_preset->psz_ext ) )
162 if( !b_valid && !p_demux->b_force )
165 /* Set p_input field */
166 p_demux->pf_demux = Demux;
167 p_demux->pf_control = Control;
168 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
174 p_sys->b_y4m = b_y4m;
175 p_sys->f_fps = var_CreateGetFloat( p_demux, "rawvid-fps" );
176 i_width = var_CreateGetInteger( p_demux, "rawvid-width" );
177 i_height = var_CreateGetInteger( p_demux, "rawvid-height" );
178 psz_chroma = var_CreateGetString( p_demux, "rawvid-chroma" );
179 psz_aspect_ratio = var_CreateGetString( p_demux, "rawvid-aspect-ratio" );
187 psz = stream_ReadLine( p_demux->s );
189 /* TODO: handle interlacing */
191 #define READ_FRAC( key, num, den ) \
192 buf = strstr( psz+9, key );\
195 char *end = strchr( buf+1, ' ' );\
197 if( end ) *end = '\0';\
198 sep = strchr( buf+1, ':' );\
202 den = atoi( sep+1 );\
208 num = atoi( buf+2 );\
209 if( sep ) *sep = ':';\
210 if( end ) *end = ' ';\
212 READ_FRAC( " W", i_width, a )
213 READ_FRAC( " H", i_height, a )
214 READ_FRAC( " F", a, b )
215 p_sys->f_fps = (double)a/(double)b;
216 READ_FRAC( " A", a, b )
217 if( b != 0 ) i_aspect = a * VOUT_ASPECT_FACTOR / b;
219 buf = strstr( psz+9, " C" );
222 char *end = strchr( buf+1, ' ' );
223 if( end ) *end = '\0';
225 if( !strncmp( buf, "420jpeg", 7 ) )
227 psz_chroma = strdup( "I420" );
229 else if( !strncmp( buf, "420paldv", 8 ) )
231 psz_chroma = strdup( "I420" );
233 else if( !strncmp( buf, "420", 3 ) )
235 psz_chroma = strdup( "I420" );
237 else if( !strncmp( buf, "422", 3 ) )
239 psz_chroma = strdup( "I422" );
241 else if( !strncmp( buf, "444", 3 ) )
243 psz_chroma = strdup( "I444" );
245 else if( !strncmp( buf, "mono", 4 ) )
247 psz_chroma = strdup( "GREY" );
251 msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"",
254 if( end ) *end = ' ';
260 if( p_preset && *p_preset->psz_ext )
262 if( !i_width ) i_width = p_preset->i_width;
263 if( !i_height ) i_height = p_preset->i_height;
264 if( !p_sys->f_fps ) p_sys->f_fps = p_preset->f_fps;
265 if( !*psz_aspect_ratio )
267 free( psz_aspect_ratio );
268 psz_aspect_ratio = strdup( psz_aspect_ratio );
273 psz_chroma = strdup( psz_chroma );
277 if( i_width <= 0 || i_height <= 0 )
279 msg_Err( p_demux, "width and height must be strictly positive." );
280 free( psz_aspect_ratio );
288 if( psz_aspect_ratio && *psz_aspect_ratio )
290 char *psz_parser = strchr( psz_aspect_ratio, ':' );
293 *psz_parser++ = '\0';
294 i_aspect = atoi( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR
295 / atoi( psz_parser );
299 i_aspect = atof( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR;
304 i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
307 free( psz_aspect_ratio );
309 if( psz_chroma && strlen( psz_chroma ) >= 4 )
311 memcpy( &i_chroma, psz_chroma, 4 );
312 msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma,
317 i_chroma = VLC_FOURCC('Y','V','1','2');
318 msg_Dbg( p_demux, "Using default chroma 0x%.8x (%4.4s)", i_chroma,
323 es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
324 vout_InitFormat( &p_sys->fmt_video.video, i_chroma, i_width, i_height,
326 if( !p_sys->fmt_video.video.i_bits_per_pixel )
328 msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
333 p_sys->frame_size = i_width * i_height
334 * p_sys->fmt_video.video.i_bits_per_pixel / 8;
335 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
340 /*****************************************************************************
341 * Close: frees unused data
342 *****************************************************************************/
343 static void Close( vlc_object_t *p_this )
345 demux_t *p_demux = (demux_t*)p_this;
346 demux_sys_t *p_sys = p_demux->p_sys;
350 /*****************************************************************************
351 * Demux: reads and demuxes data packets
352 *****************************************************************************
353 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
354 *****************************************************************************/
355 static int Demux( demux_t *p_demux )
357 demux_sys_t *p_sys = p_demux->p_sys;
360 /* Call the pace control */
361 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
365 /* Skip the frame header */
366 unsigned char psz_buf[10];
368 stream_Read( p_demux->s, psz_buf, strlen( "FRAME" ) );
369 while( psz_buf[0] != 0x0a )
371 if( stream_Read( p_demux->s, psz_buf, 1 ) < 1 )
376 if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
382 p_block->i_dts = p_block->i_pts = p_sys->i_pcr;
383 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
385 p_sys->i_pcr += ( INT64_C(1000000) / p_sys->f_fps );
390 /*****************************************************************************
392 *****************************************************************************/
393 static int Control( demux_t *p_demux, int i_query, va_list args )
395 demux_sys_t *p_sys = p_demux->p_sys;
397 /* XXX: DEMUX_SET_TIME is precise here */
398 return demux_vaControlHelper( p_demux->s, 0, -1,
399 p_sys->frame_size * p_sys->f_fps * 8,
400 p_sys->frame_size, i_query, args );