]> git.sesse.net Git - vlc/blob - modules/demux/rawvid.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / rawvid.c
1 /*****************************************************************************
2  * rawvid.c : raw video input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea at videolan d.t org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31 #include <vlc_vout.h>                                     /* vout_InitFormat */
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open ( vlc_object_t * );
37 static void Close( vlc_object_t * );
38
39 #define FPS_TEXT N_("Frames per Second")
40 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
41     "playing raw video streams.")
42
43 #define WIDTH_TEXT N_("Width")
44 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
45     "video stream.")
46
47 #define HEIGHT_TEXT N_("Height")
48 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
49     "video stream.")
50
51 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
52 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
53
54 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
55 #define ASPECT_RATIO_LONGTEXT N_( \
56     "Aspect ratio (4:3, 16:9). Default is square pixels." )
57
58 vlc_module_begin();
59     set_shortname( "Raw Video" );
60     set_description( _("Raw video demuxer") );
61     set_capability( "demux2", 3 );
62     set_category( CAT_INPUT );
63     set_subcategory( SUBCAT_INPUT_DEMUX );
64     set_callbacks( Open, Close );
65     add_shortcut( "rawvideo" );
66     add_float( "rawvid-fps", 0, 0, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
67         change_safe();
68     add_integer( "rawvid-width", 0, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 );
69         change_safe();
70     add_integer( "rawvid-height", 0, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 );
71         change_safe();
72     add_string( "rawvid-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
73                 VLC_TRUE );
74         change_safe();
75     add_string( "rawvid-aspect-ratio", NULL, NULL,
76                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
77         change_safe();
78 vlc_module_end();
79
80 /*****************************************************************************
81  * Definitions of structures used by this plugin
82  *****************************************************************************/
83 struct demux_sys_t
84 {
85     int    frame_size;
86     float  f_fps;
87
88     es_out_id_t *p_es_video;
89     es_format_t  fmt_video;
90
91     mtime_t i_pcr;
92
93     vlc_bool_t b_y4m;
94 };
95
96 /*****************************************************************************
97  * Local prototypes
98  *****************************************************************************/
99 static int Demux( demux_t * );
100 static int Control( demux_t *, int i_query, va_list args );
101
102 struct preset_t
103 {
104     const char *psz_ext;
105     int i_width;
106     int i_height;
107     double f_fps;
108     const char *psz_aspect_ratio;
109     const char *psz_chroma;
110 };
111
112 static struct preset_t p_presets[] =
113 {
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., "", "" }
121 };
122
123 /*****************************************************************************
124  * Open: initializes raw DV demux structures
125  *****************************************************************************/
126 static int Open( vlc_object_t * p_this )
127 {
128     demux_t     *p_demux = (demux_t*)p_this;
129     demux_sys_t *p_sys;
130     int i_width, i_height;
131     char *psz_ext;
132     char *psz_chroma;
133     uint32_t i_chroma;
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     vlc_bool_t b_valid = VLC_FALSE;
139     vlc_bool_t b_y4m = VLC_FALSE;
140
141     if( stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
142     {
143         /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
144         if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
145         {
146             b_valid = VLC_TRUE;
147             b_y4m = VLC_TRUE;
148         }
149     }
150
151     psz_ext = strrchr( p_demux->psz_path, '.' );
152     if( psz_ext )
153     {
154         psz_ext++;
155         for( p_preset = p_presets; *p_preset->psz_ext; p_preset++ )
156             if( !strcasecmp( psz_ext, p_preset->psz_ext ) )
157             {
158                 b_valid = VLC_TRUE;
159                 break;
160             }
161     }
162     if( !b_valid && !p_demux->b_force )
163         return VLC_EGENERIC;
164
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 ) );
169     p_sys->i_pcr = 1;
170
171     p_sys->b_y4m = b_y4m;
172     p_sys->f_fps = var_CreateGetFloat( p_demux, "rawvid-fps" );
173     i_width = var_CreateGetInteger( p_demux, "rawvid-width" );
174     i_height = var_CreateGetInteger( p_demux, "rawvid-height" );
175     psz_chroma = var_CreateGetString( p_demux, "rawvid-chroma" );
176     psz_aspect_ratio = var_CreateGetString( p_demux, "rawvid-aspect-ratio" );
177
178     if( b_y4m )
179     {
180         char *psz;
181         char *buf;
182         int a, b;
183         psz = stream_ReadLine( p_demux->s );
184
185         /* TODO: handle interlacing */
186
187 #define READ_FRAC( key, num, den ) \
188         buf = strchr( psz+9, key );\
189         if( buf )\
190         {\
191             char *end = strchr( buf, ' ' );\
192             char *sep;\
193             if( end ) *end = '\0';\
194             sep = strchr( buf, ':' );\
195             if( sep )\
196             {\
197                 *sep = '\0';\
198                 den = atoi( sep+1 );\
199             }\
200             else\
201             {\
202                 den = 1;\
203             }\
204             num = atoi( buf+1 );\
205             if( sep ) *sep = ':';\
206             if( end ) *end = ' ';\
207         }
208         READ_FRAC( 'W', i_width, a )
209         READ_FRAC( 'H', i_height, a )
210         READ_FRAC( 'F', a, b )
211         p_sys->f_fps = (double)a/(double)b;
212         READ_FRAC( 'A', a, b )
213         if( b != 0 ) i_aspect = a * VOUT_ASPECT_FACTOR / b;
214
215         buf = strchr( psz+9, 'C' );
216         if( buf )
217         {
218             char *end = strchr( buf, ' ' );
219             if( end ) *end = '\0';
220             buf++;
221             if( !strncmp( buf, "C420jpeg", 8 ) )
222             {
223                 psz_chroma = strdup( "I420" );
224             }
225             else if( !strncmp( buf, "C420paldv", 9 ) )
226             {
227                 psz_chroma = strdup( "I420" );
228             }
229             else if( !strncmp( buf, "C420", 4 ) )
230             {
231                 psz_chroma = strdup( "I420" );
232             }
233             else if( !strncmp( buf, "C422", 4 ) )
234             {
235                 psz_chroma = strdup( "I422" );
236             }
237             else if( !strncmp( buf, "C444", 4 ) )
238             {
239                 psz_chroma = strdup( "I444" );
240             }
241             else
242             {
243                 msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"",
244                           buf );
245             }
246             if( end ) *end = ' ';
247         }
248
249         free( psz );
250     }
251
252     if( p_preset && *p_preset->psz_ext )
253     {
254         if( !i_width ) i_width = p_preset->i_width;
255         if( !i_height ) i_height = p_preset->i_height;
256         if( !p_sys->f_fps ) p_sys->f_fps = p_preset->f_fps;
257         if( !*psz_aspect_ratio )
258         {
259             free( psz_aspect_ratio );
260             psz_aspect_ratio = strdup( psz_aspect_ratio );
261         }
262         if( !*psz_chroma )
263         {
264             free( psz_chroma );
265             psz_chroma = strdup( psz_chroma );
266         }
267     }
268
269     if( i_width <= 0 || i_height <= 0 )
270     {
271         msg_Err( p_demux, "width and height must be strictly positive." );
272         free( psz_aspect_ratio );
273         free( psz_chroma );
274         free( p_sys );
275         return VLC_EGENERIC;
276     }
277
278     if( !i_aspect )
279     {
280         if( psz_aspect_ratio && *psz_aspect_ratio )
281         {
282             char *psz_parser = strchr( psz_aspect_ratio, ':' );
283             if( psz_parser )
284             {
285                 *psz_parser++ = '\0';
286                 i_aspect = atoi( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR
287                            / atoi( psz_parser );
288             }
289             else
290             {
291                 i_aspect = atof( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR;
292             }
293         }
294         else
295         {
296             i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
297         }
298     }
299     free( psz_aspect_ratio );
300
301     if( psz_chroma && strlen( psz_chroma ) >= 4 )
302     {
303         memcpy( &i_chroma, psz_chroma, 4 );
304         msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma,
305                  (char*)&i_chroma );
306     }
307     else
308     {
309         i_chroma = VLC_FOURCC('Y','V','1','2');
310         msg_Dbg( p_demux, "Using default chroma 0x%.8x (%4.4s)", i_chroma,
311                  (char*)&i_chroma );
312     }
313     free( psz_chroma );
314
315     es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
316     vout_InitFormat( &p_sys->fmt_video.video, i_chroma, i_width, i_height,
317                      i_aspect );
318     if( !p_sys->fmt_video.video.i_bits_per_pixel )
319     {
320         msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
321                  (char*)&i_chroma );
322         free( p_sys );
323         return VLC_EGENERIC;
324     }
325     p_sys->frame_size = i_width * i_height
326                         * p_sys->fmt_video.video.i_bits_per_pixel / 8;
327     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
328
329     return VLC_SUCCESS;
330 }
331
332 /*****************************************************************************
333  * Close: frees unused data
334  *****************************************************************************/
335 static void Close( vlc_object_t *p_this )
336 {
337     demux_t     *p_demux = (demux_t*)p_this;
338     demux_sys_t *p_sys  = p_demux->p_sys;
339     free( p_sys );
340 }
341
342 /*****************************************************************************
343  * Demux: reads and demuxes data packets
344  *****************************************************************************
345  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
346  *****************************************************************************/
347 static int Demux( demux_t *p_demux )
348 {
349     demux_sys_t *p_sys  = p_demux->p_sys;
350     block_t     *p_block;
351
352     /* Call the pace control */
353     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
354
355     if( p_sys->b_y4m )
356     {
357         /* Skip the frame header */
358         unsigned char psz_buf[10];
359         psz_buf[9] = '\0';
360         stream_Read( p_demux->s, psz_buf, strlen( "FRAME" ) );
361         while( psz_buf[0] != 0x0a )
362         {
363             if( stream_Read( p_demux->s, psz_buf, 1 ) < 1 )
364                 return 0;
365         }
366     }
367
368     if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
369     {
370         /* EOF */
371         return 0;
372     }
373
374     p_block->i_dts = p_block->i_pts = p_sys->i_pcr;
375     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
376
377     p_sys->i_pcr += ( I64C(1000000) / p_sys->f_fps );
378
379     return 1;
380 }
381
382 /*****************************************************************************
383  * Control:
384  *****************************************************************************/
385 static int Control( demux_t *p_demux, int i_query, va_list args )
386 {
387     demux_sys_t *p_sys  = p_demux->p_sys;
388
389     /* XXX: DEMUX_SET_TIME is precise here */
390     return demux2_vaControlHelper( p_demux->s, 0, -1,
391                                    p_sys->frame_size * p_sys->f_fps * 8,
392                                    p_sys->frame_size, i_query, args );
393 }