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