1 /*****************************************************************************
2 * rawvid.c : raw video input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id: 6f40c48afdc79706ca534df86b24daccd244e504 $
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 #define FPS_TEXT N_("Frames per Second")
44 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
45 "playing raw video streams. In the form 30000/1001 or 29.97")
46
47 #define WIDTH_TEXT N_("Width")
48 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
49 "video stream.")
50
51 #define HEIGHT_TEXT N_("Height")
52 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
53 "video stream.")
54
55 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
56 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
57
58 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
59 #define ASPECT_RATIO_LONGTEXT N_( \
60 "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
61
62 vlc_module_begin ()
63 set_shortname( "Raw Video" )
64 set_description( N_("Raw video demuxer") )
65 set_capability( "demux", 10 )
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_DEMUX )
68 set_callbacks( Open, Close )
69 add_shortcut( "rawvideo" )
70 add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
71 add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
72 add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
73 add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
74 true )
75 add_string( "rawvid-aspect-ratio", NULL,
76 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
77 vlc_module_end ()
78
79 /*****************************************************************************
80 * Definitions of structures used by this plugin
81 *****************************************************************************/
82 struct demux_sys_t
83 {
84 int frame_size;
85
86 es_out_id_t *p_es_video;
87 es_format_t fmt_video;
88
89 date_t pcr;
90
91 bool b_y4m;
92 };
93
94 /*****************************************************************************
95 * Local prototypes
96 *****************************************************************************/
97 static int Demux( demux_t * );
98 static int Control( demux_t *, int i_query, va_list args );
99
100 struct preset_t
101 {
102 const char *psz_ext;
103 int i_width;
104 int i_height;
105 unsigned u_fps_num;
106 unsigned u_fps_den;
107 unsigned u_ar_num;
108 unsigned u_ar_den;
109 vlc_fourcc_t i_chroma;
110 };
111
112 static const struct preset_t p_presets[] =
113 {
114 { "sqcif", 128, 96, 30000, 1001, 4,3, VLC_CODEC_YV12 },
115 { "qcif", 176, 144, 30000, 1001, 4,3, VLC_CODEC_YV12 },
116 { "cif", 352, 288, 30000, 1001, 4,3, VLC_CODEC_YV12 },
117 { "4cif", 704, 576, 30000, 1001, 4,3, VLC_CODEC_YV12 },
118 { "16cif", 1408, 1152, 30000, 1001, 4,3, VLC_CODEC_YV12 },
119 { "yuv", 176, 144, 25, 1, 4,3, VLC_CODEC_YV12 },
120 { NULL, 0, 0, 0, 0, 0,0, 0 }
121 };
122
123 /*****************************************************************************
124 * Open: initializes raw DV demux structures
125 *****************************************************************************/
Open(vlc_object_t * p_this)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=-1, i_height=-1;
131 unsigned u_fps_num, u_fps_den;
132 vlc_fourcc_t i_chroma = 0;
133 unsigned int i_sar_num;
134 unsigned int i_sar_den;
135 const struct preset_t *p_preset = NULL;
136 const uint8_t *p_peek;
137 bool b_y4m = false;
138
139 if( vlc_stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
140 {
141 /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
142 if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
143 {
144 b_y4m = true;
145 goto valid;
146 }
147 }
148
149 if( !p_demux->obj.force )
150 {
151 /* guess preset based on file extension */
152 if( !p_demux->psz_file )
153 return VLC_EGENERIC;
154
155 const char *psz_ext = strrchr( p_demux->psz_file, '.' );
156 if( !psz_ext )
157 return VLC_EGENERIC;
158 psz_ext++;
159
160 for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
161 {
162 if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
163 {
164 p_preset = &p_presets[i];
165 goto valid;
166 }
167 }
168 return VLC_EGENERIC;
169 }
170 valid:
171 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
172 if( !p_sys )
173 return VLC_ENOMEM;
174
175 p_sys->b_y4m = b_y4m;
176
177 /* guess the parameters based on the preset */
178 if( p_preset )
179 {
180 i_width = p_preset->i_width;
181 i_height = p_preset->i_height;
182 u_fps_num = p_preset->u_fps_num;
183 u_fps_den = p_preset->u_fps_den;
184 i_sar_num = p_preset->u_ar_num * p_preset->i_height;
185 i_sar_den = p_preset->u_ar_den * p_preset->i_width;
186 i_chroma = p_preset->i_chroma;
187 }
188
189 /* override presets if yuv4mpeg2 */
190 if( b_y4m )
191 {
192 /* The string should start with "YUV4MPEG2" */
193 char *psz = vlc_stream_ReadLine( p_demux->s );
194 char *psz_buf;
195 int a = 1;
196 int b = 1;
197
198 if( unlikely(psz == NULL) )
199 goto error;
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 if( b != 0 )
231 {
232 i_sar_num = a;
233 i_sar_den = b;
234 }
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_CODEC_I420 },
242 { "420paldv", VLC_CODEC_I420 },
243 { "420", VLC_CODEC_I420 },
244 { "422", VLC_CODEC_I422 },
245 { "444", VLC_CODEC_I444 },
246 { "mono", VLC_CODEC_GREY },
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 if( var_InheritURational( p_demux, &u_fps_num, &u_fps_den, "rawvid-fps" ) )
298 {
299 u_fps_num = 0;
300 u_fps_den = 1;
301 }
302
303 if( var_InheritURational( p_demux, &i_sar_num, &i_sar_den,
304 "rawvid-aspect-ratio" ) )
305 i_sar_num = i_sar_den = 1;
306
307 /* moan about anything wrong */
308 if( i_width <= 0 || i_height <= 0 )
309 {
310 msg_Err( p_demux, "width and height must be strictly positive." );
311 goto error;
312 }
313
314 if( !u_fps_num || !u_fps_den )
315 {
316 msg_Err( p_demux, "invalid or no framerate specified." );
317 goto error;
318 }
319
320 if( i_chroma == 0 )
321 {
322 msg_Err( p_demux, "invalid or no chroma specified." );
323 goto error;
324 }
325
326 /* fixup anything missing with sensible assumptions */
327 if( i_sar_num <= 0 || i_sar_den <= 0 )
328 {
329 /* assume 1:1 sar */
330 i_sar_num = 1;
331 i_sar_den = 1;
332 }
333
334 es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
335 video_format_Setup( &p_sys->fmt_video.video, i_chroma,
336 i_width, i_height, i_width, i_height,
337 i_sar_num, i_sar_den );
338
339 vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
340 &p_sys->fmt_video.video.i_frame_rate_base,
341 u_fps_num, u_fps_den, 0);
342 date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
343 p_sys->fmt_video.video.i_frame_rate_base );
344 date_Set( &p_sys->pcr, 0 );
345
346 if( !p_sys->fmt_video.video.i_bits_per_pixel )
347 {
348 msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
349 (char*)&i_chroma );
350 goto error;
351 }
352 const vlc_chroma_description_t *dsc =
353 vlc_fourcc_GetChromaDescription(p_sys->fmt_video.video.i_chroma);
354 if (unlikely(dsc == NULL))
355 goto error;
356 p_sys->frame_size = 0;
357 for (unsigned i=0; i<dsc->plane_count; i++)
358 {
359 unsigned pitch = (i_width + (dsc->p[i].w.den - 1))
360 * dsc->p[i].w.num / dsc->p[i].w.den * dsc->pixel_size;
361 unsigned lines = (i_height + (dsc->p[i].h.den - 1))
362 * dsc->p[i].h.num / dsc->p[i].h.den;
363 p_sys->frame_size += pitch * lines;
364 }
365 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
366
367 p_demux->pf_demux = Demux;
368 p_demux->pf_control = Control;
369 return VLC_SUCCESS;
370
371 error:
372 free( p_sys );
373 return VLC_EGENERIC;
374 }
375
376 /*****************************************************************************
377 * Close: frees unused data
378 *****************************************************************************/
Close(vlc_object_t * p_this)379 static void Close( vlc_object_t *p_this )
380 {
381 demux_t *p_demux = (demux_t*)p_this;
382 demux_sys_t *p_sys = p_demux->p_sys;
383 free( p_sys );
384 }
385
386 /*****************************************************************************
387 * Demux: reads and demuxes data packets
388 *****************************************************************************
389 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
390 *****************************************************************************/
Demux(demux_t * p_demux)391 static int Demux( demux_t *p_demux )
392 {
393 demux_sys_t *p_sys = p_demux->p_sys;
394 block_t *p_block;
395 mtime_t i_pcr = date_Get( &p_sys->pcr );
396
397 /* Call the pace control */
398 es_out_SetPCR( p_demux->out, VLC_TS_0 + i_pcr );
399
400 if( p_sys->b_y4m )
401 {
402 /* Skip the frame header */
403 /* Skip "FRAME" */
404 if( vlc_stream_Read( p_demux->s, NULL, 5 ) < 5 )
405 return 0;
406 /* Find \n */
407 for( ;; )
408 {
409 uint8_t b;
410 if( vlc_stream_Read( p_demux->s, &b, 1 ) < 1 )
411 return 0;
412 if( b == 0x0a )
413 break;
414 }
415 }
416
417 p_block = vlc_stream_Block( p_demux->s, p_sys->frame_size );
418 if( p_block == NULL )
419 {
420 /* EOF */
421 return 0;
422 }
423
424 p_block->i_dts = p_block->i_pts = VLC_TS_0 + i_pcr;
425 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
426
427 date_Increment( &p_sys->pcr, 1 );
428
429 return 1;
430 }
431
432 /*****************************************************************************
433 * Control:
434 *****************************************************************************/
Control(demux_t * p_demux,int i_query,va_list args)435 static int Control( demux_t *p_demux, int i_query, va_list args )
436 {
437 demux_sys_t *p_sys = p_demux->p_sys;
438
439 /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
440 const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
441 p_sys->pcr.i_divider_den;
442
443 /* XXX: DEMUX_SET_TIME is precise here */
444 return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
445 p_sys->frame_size, i_query, args );
446 }
447
448