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