]> git.sesse.net Git - vlc/blob - modules/demux/rawvid.c
Merge branch 1.0-bugfix
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <assert.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
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")
47
48 #define WIDTH_TEXT N_("Width")
49 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
50     "video stream.")
51
52 #define HEIGHT_TEXT N_("Height")
53 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
54     "video stream.")
55
56 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
57 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
58
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." )
62
63 vlc_module_begin ()
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, NULL, 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,
75                 true )
76     add_string( "rawvid-aspect-ratio", NULL, NULL,
77                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
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
87     es_out_id_t *p_es_video;
88     es_format_t  fmt_video;
89
90     date_t pcr;
91
92     bool b_y4m;
93 };
94
95 /*****************************************************************************
96  * Local prototypes
97  *****************************************************************************/
98 static int Demux( demux_t * );
99 static int Control( demux_t *, int i_query, va_list args );
100
101 struct preset_t
102 {
103     const char *psz_ext;
104     int i_width;
105     int i_height;
106     unsigned u_fps_num;
107     unsigned u_fps_den;
108     unsigned u_ar_num;
109     unsigned u_ar_den;
110     vlc_fourcc_t i_chroma;
111 };
112
113 static const struct preset_t p_presets[] =
114 {
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 }
122 };
123
124 /*****************************************************************************
125  * Open: initializes raw DV demux structures
126  *****************************************************************************/
127 static int Open( vlc_object_t * p_this )
128 {
129     demux_t     *p_demux = (demux_t*)p_this;
130     demux_sys_t *p_sys;
131     int i_width=-1, i_height=-1;
132     unsigned u_fps_num=0, u_fps_den=1;
133     char *psz_ext;
134     vlc_fourcc_t i_chroma;
135     unsigned int i_aspect = 0;
136     const struct preset_t *p_preset = NULL;
137     const uint8_t *p_peek;
138     bool b_valid = false;
139     bool b_y4m = 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 = true;
147             b_y4m = true;
148         }
149     }
150
151     /* guess preset based on file extension */
152     psz_ext = strrchr( p_demux->psz_path, '.' );
153     if( psz_ext )
154     {
155         psz_ext++;
156         for( int i = 0; p_presets[i].psz_ext ; i++ )
157         {
158             if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
159             {
160                 p_preset = &p_presets[i];
161                 b_valid = true;
162                 break;
163             }
164         }
165     }
166     if( !b_valid && !p_demux->b_force )
167         return VLC_EGENERIC;
168
169     /* Set p_input field */
170     p_demux->pf_demux   = Demux;
171     p_demux->pf_control = Control;
172     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
173     if( !p_sys )
174         return VLC_ENOMEM;
175
176     p_sys->b_y4m = b_y4m;
177
178     /* guess the parameters based on the preset */
179     if( p_preset )
180     {
181         i_width = p_preset->i_width;
182         i_height = p_preset->i_height;
183         u_fps_num = p_preset->u_fps_num;
184         u_fps_den = p_preset->u_fps_den;
185         i_aspect = VOUT_ASPECT_FACTOR * p_preset->u_ar_num / p_preset->u_ar_den;
186         i_chroma = p_preset->i_chroma;
187     }
188
189     /* override presets if yuv4mpeg2 */
190     if( b_y4m )
191     {
192         char *psz = stream_ReadLine( p_demux->s );
193         char *psz_buf;
194         int a = 1;
195         int b = 1;
196
197         /* The string will start with "YUV4MPEG2" */
198         assert( strlen(psz) >= 9 );
199
200         /* NB, it is not possible to handle interlaced here, since the
201          * interlaced picture flags are in picture_t not block_t */
202
203 #define READ_FRAC( key, num, den ) do { \
204         psz_buf = strstr( psz+9, key );\
205         if( psz_buf )\
206         {\
207             char *end = strchr( psz_buf+1, ' ' );\
208             char *sep;\
209             if( end ) *end = '\0';\
210             sep = strchr( psz_buf+1, ':' );\
211             if( sep )\
212             {\
213                 *sep = '\0';\
214                 den = atoi( sep+1 );\
215             }\
216             else\
217             {\
218                 den = 1;\
219             }\
220             num = atoi( psz_buf+2 );\
221             if( sep ) *sep = ':';\
222             if( end ) *end = ' ';\
223         } } while(0)
224         READ_FRAC( " W", i_width, a );
225         READ_FRAC( " H", i_height, a );
226         READ_FRAC( " F", u_fps_num, u_fps_den );
227         READ_FRAC( " A", a, b );
228 #undef READ_FRAC
229         /* Try to calculate aspect ratio here, rather than store ratio
230          * in u_ar_{num,den}, since width may be overridden by then.
231          * Plus, a:b is sar. */
232         if( b != 0 )
233             i_aspect = VOUT_ASPECT_FACTOR * a * i_width / (b * i_height);
234
235         psz_buf = strstr( psz+9, " C" );
236         if( psz_buf )
237         {
238             static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
239             {
240                 { "420jpeg",    VLC_CODEC_I420 },
241                 { "420paldv",   VLC_CODEC_I420 },
242                 { "420",        VLC_CODEC_I420 },
243                 { "422",        VLC_CODEC_I422 },
244                 { "444",        VLC_CODEC_I444 },
245                 { "mono",       VLC_CODEC_GREY },
246                 { NULL, 0 }
247             };
248             bool b_found = false;
249             char *psz_end = strchr( psz_buf+1, ' ' );
250             if( psz_end )
251                 *psz_end = '\0';
252             psz_buf += 2;
253
254             for( int i = 0; formats[i].psz_name != NULL; i++ )
255             {
256                 if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
257                 {
258                     i_chroma = formats[i].i_fcc;
259                     b_found = true;
260                     break;
261                 }
262             }
263             if( !b_found )
264                 msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
265             if( psz_end )
266                 *psz_end = ' ';
267         }
268
269         free( psz );
270     }
271
272     /* allow the user to override anything guessed from the input */
273     int i_tmp;
274     i_tmp = var_CreateGetInteger( p_demux, "rawvid-width" );
275     if( i_tmp ) i_width = i_tmp;
276
277     i_tmp = var_CreateGetInteger( p_demux, "rawvid-height" );
278     if( i_tmp ) i_height = i_tmp;
279
280     char *psz_tmp;
281     psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
282     if( psz_tmp )
283     {
284         if( strlen( psz_tmp ) != 4 )
285         {
286             msg_Err( p_demux, "Invalid fourcc format/chroma specification %s"
287                      " expecting four characters eg, UYVY", psz_tmp );
288             free( psz_tmp );
289             goto error;
290         }
291         memcpy( &i_chroma, psz_tmp, 4 );
292         msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
293         free( psz_tmp );
294     }
295
296     psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-fps" );
297     if( psz_tmp )
298     {
299         char *p_ptr;
300         /* fps can either be n/d or q.f
301          * for accuracy, avoid representation in float */
302         u_fps_num = strtol( psz_tmp, &p_ptr, 10 );
303         if( *p_ptr == '/' )
304         {
305             p_ptr++;
306             u_fps_den = strtol( p_ptr, NULL, 10 );
307         }
308         else if( *p_ptr == '.' )
309         {
310             char *p_end;
311             p_ptr++;
312             int i_frac =  strtol( p_ptr, &p_end, 10 );
313             u_fps_den = (p_end - p_ptr) * 10;
314             if( !u_fps_den )
315                 u_fps_den = 1;
316             u_fps_num = u_fps_num * u_fps_den + i_frac;
317         }
318         else if( *p_ptr == '\0')
319         {
320             u_fps_den = 1;
321         }
322         free( psz_tmp );
323     }
324
325     psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-aspect-ratio" );
326     if( psz_tmp )
327     {
328         char *psz_denominator = strchr( psz_tmp, ':' );
329         if( psz_denominator )
330         {
331             *psz_denominator++ = '\0';
332             i_aspect = atoi( psz_tmp ) * VOUT_ASPECT_FACTOR
333                      / atoi( psz_denominator );
334         }
335         free( psz_tmp );
336     }
337
338     /* moan about anything wrong */
339     if( i_width <= 0 || i_height <= 0 )
340     {
341         msg_Err( p_demux, "width and height must be strictly positive." );
342         goto error;
343     }
344
345     if( !u_fps_num || !u_fps_den )
346     {
347         msg_Err( p_demux, "invalid or no framerate specified." );
348         goto error;
349     }
350
351     /* fixup anything missing with sensible assumptions */
352     if( !i_aspect )
353     {
354         /* assume 1:1 sar */
355         i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
356     }
357
358     es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
359     video_format_Setup( &p_sys->fmt_video.video,
360                         i_chroma, i_width, i_height, i_aspect );
361
362     vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
363                  &p_sys->fmt_video.video.i_frame_rate_base,
364                  u_fps_num, u_fps_den, 0);
365     date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
366                p_sys->fmt_video.video.i_frame_rate_base );
367     date_Set( &p_sys->pcr, 1 );
368
369     if( !p_sys->fmt_video.video.i_bits_per_pixel )
370     {
371         msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
372                  (char*)&i_chroma );
373         goto error;
374     }
375     p_sys->frame_size = i_width * i_height
376                         * p_sys->fmt_video.video.i_bits_per_pixel / 8;
377     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
378
379     return VLC_SUCCESS;
380
381 error:
382     free( p_sys );
383     return VLC_EGENERIC;
384 }
385
386 /*****************************************************************************
387  * Close: frees unused data
388  *****************************************************************************/
389 static void Close( vlc_object_t *p_this )
390 {
391     demux_t     *p_demux = (demux_t*)p_this;
392     demux_sys_t *p_sys  = p_demux->p_sys;
393     free( p_sys );
394 }
395
396 /*****************************************************************************
397  * Demux: reads and demuxes data packets
398  *****************************************************************************
399  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
400  *****************************************************************************/
401 static int Demux( demux_t *p_demux )
402 {
403     demux_sys_t *p_sys  = p_demux->p_sys;
404     block_t     *p_block;
405     mtime_t i_pcr = date_Get( &p_sys->pcr );
406
407     /* Call the pace control */
408     es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
409
410     if( p_sys->b_y4m )
411     {
412         /* Skip the frame header */
413         /* Skip "FRAME" */
414         if( stream_Read( p_demux->s, NULL, 5 ) < 5 )
415             return 0;
416         /* Find \n */
417         for( ;; )
418         {
419             uint8_t b;
420             if( stream_Read( p_demux->s, &b, 1 ) < 1 )
421                 return 0;
422             if( b == 0x0a )
423                 break;
424         }
425     }
426
427     if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
428     {
429         /* EOF */
430         return 0;
431     }
432
433     p_block->i_dts = p_block->i_pts = i_pcr;
434     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
435
436     date_Increment( &p_sys->pcr, 1 );
437
438     return 1;
439 }
440
441 /*****************************************************************************
442  * Control:
443  *****************************************************************************/
444 static int Control( demux_t *p_demux, int i_query, va_list args )
445 {
446     demux_sys_t *p_sys  = p_demux->p_sys;
447
448      /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
449     const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
450                                                     p_sys->pcr.i_divider_den;
451
452     /* XXX: DEMUX_SET_TIME is precise here */
453     return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
454                                    p_sys->frame_size, i_query, args );
455 }
456