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