qt: playlist: use item title if available
[vlc.git] / modules / demux / rawvid.c
blob94a3b00f6ab41666e33a6b1b60ca934a3ad4b93f
1 /*****************************************************************************
2 * rawvid.c : raw video input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
7 * Antoine Cellerier <dionoea at videolan d.t org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 /*****************************************************************************
37 * Module descriptor
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 #define FPS_TEXT N_("Frames per Second")
43 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
44 "playing raw video streams. In the form 30000/1001 or 29.97")
46 #define WIDTH_TEXT N_("Width")
47 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
48 "video stream.")
50 #define HEIGHT_TEXT N_("Height")
51 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
52 "video stream.")
54 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
55 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
57 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
58 #define ASPECT_RATIO_LONGTEXT N_( \
59 "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
61 vlc_module_begin ()
62 set_shortname( "Raw Video" )
63 set_description( N_("Raw video demuxer") )
64 set_capability( "demux", 10 )
65 set_category( CAT_INPUT )
66 set_subcategory( SUBCAT_INPUT_DEMUX )
67 set_callbacks( Open, Close )
68 add_shortcut( "rawvideo" )
69 add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
70 add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
71 add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
72 add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
73 true )
74 add_string( "rawvid-aspect-ratio", NULL,
75 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
76 vlc_module_end ()
78 /*****************************************************************************
79 * Definitions of structures used by this plugin
80 *****************************************************************************/
81 typedef struct
83 int frame_size;
85 es_out_id_t *p_es_video;
86 es_format_t fmt_video;
88 date_t pcr;
90 bool b_y4m;
91 } demux_sys_t;
93 /*****************************************************************************
94 * Local prototypes
95 *****************************************************************************/
96 static int Demux( demux_t * );
97 static int Control( demux_t *, int i_query, va_list args );
99 struct preset_t
101 const char *psz_ext;
102 int i_width;
103 int i_height;
104 unsigned u_fps_num;
105 unsigned u_fps_den;
106 unsigned u_ar_num;
107 unsigned u_ar_den;
108 vlc_fourcc_t i_chroma;
111 static const struct preset_t p_presets[] =
113 { "sqcif", 128, 96, 30000, 1001, 4,3, VLC_CODEC_YV12 },
114 { "qcif", 176, 144, 30000, 1001, 4,3, VLC_CODEC_YV12 },
115 { "cif", 352, 288, 30000, 1001, 4,3, VLC_CODEC_YV12 },
116 { "4cif", 704, 576, 30000, 1001, 4,3, VLC_CODEC_YV12 },
117 { "16cif", 1408, 1152, 30000, 1001, 4,3, VLC_CODEC_YV12 },
118 { "yuv", 176, 144, 25, 1, 4,3, VLC_CODEC_YV12 },
119 { NULL, 0, 0, 0, 0, 0,0, 0 }
122 /*****************************************************************************
123 * Open: initializes raw DV demux structures
124 *****************************************************************************/
125 static int Open( vlc_object_t * p_this )
127 demux_t *p_demux = (demux_t*)p_this;
128 demux_sys_t *p_sys;
129 int i_width=-1, i_height=-1;
130 unsigned u_fps_num, u_fps_den;
131 vlc_fourcc_t i_chroma = 0;
132 unsigned int i_sar_num;
133 unsigned int i_sar_den;
134 const struct preset_t *p_preset = NULL;
135 const uint8_t *p_peek;
136 bool b_y4m = false;
138 if( vlc_stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
140 /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
141 if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
143 b_y4m = true;
144 goto valid;
148 if( !p_demux->obj.force )
150 /* guess preset based on file extension */
151 if( !p_demux->psz_filepath )
152 return VLC_EGENERIC;
154 const char *psz_ext = strrchr( p_demux->psz_filepath, '.' );
155 if( !psz_ext )
156 return VLC_EGENERIC;
157 psz_ext++;
159 for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
161 if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
163 p_preset = &p_presets[i];
164 goto valid;
167 return VLC_EGENERIC;
169 valid:
170 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
171 if( !p_sys )
172 return VLC_ENOMEM;
174 p_sys->b_y4m = b_y4m;
176 /* guess the parameters based on the preset */
177 if( p_preset )
179 i_width = p_preset->i_width;
180 i_height = p_preset->i_height;
181 u_fps_num = p_preset->u_fps_num;
182 u_fps_den = p_preset->u_fps_den;
183 i_sar_num = p_preset->u_ar_num * p_preset->i_height;
184 i_sar_den = p_preset->u_ar_den * p_preset->i_width;
185 i_chroma = p_preset->i_chroma;
188 /* override presets if yuv4mpeg2 */
189 if( b_y4m )
191 /* The string should start with "YUV4MPEG2" */
192 char *psz = vlc_stream_ReadLine( p_demux->s );
193 char *psz_buf;
194 int a = 1;
195 int b = 1;
197 if( unlikely(psz == NULL) )
198 goto error;
200 /* NB, it is not possible to handle interlaced here, since the
201 * interlaced picture flags are in picture_t not block_t */
203 #define READ_FRAC( key, num, den ) do { \
204 psz_buf = strstr( psz+9, key );\
205 if( psz_buf )\
207 char *end = strchr( psz_buf+1, ' ' );\
208 char *sep;\
209 if( end ) *end = '\0';\
210 sep = strchr( psz_buf+1, ':' );\
211 if( sep )\
213 *sep = '\0';\
214 den = atoi( sep+1 );\
216 else\
218 den = 1;\
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 if( b != 0 )
231 i_sar_num = a;
232 i_sar_den = b;
235 psz_buf = strstr( psz+9, " C" );
236 if( psz_buf )
238 static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
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 }
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;
254 for( int i = 0; formats[i].psz_name != NULL; i++ )
256 if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
258 i_chroma = formats[i].i_fcc;
259 b_found = true;
260 break;
263 if( !b_found )
264 msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
265 if( psz_end )
266 *psz_end = ' ';
269 free( psz );
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;
277 i_tmp = var_CreateGetInteger( p_demux, "rawvid-height" );
278 if( i_tmp ) i_height = i_tmp;
280 char *psz_tmp;
281 psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
282 if( psz_tmp )
284 if( strlen( psz_tmp ) != 4 )
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;
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 );
296 if( var_InheritURational( p_demux, &u_fps_num, &u_fps_den, "rawvid-fps" ) )
298 u_fps_num = 0;
299 u_fps_den = 1;
302 if( var_InheritURational( p_demux, &i_sar_num, &i_sar_den,
303 "rawvid-aspect-ratio" ) )
304 i_sar_num = i_sar_den = 1;
306 /* moan about anything wrong */
307 if( i_width <= 0 || i_height <= 0 )
309 msg_Err( p_demux, "width and height must be strictly positive." );
310 goto error;
313 if( !u_fps_num || !u_fps_den )
315 msg_Err( p_demux, "invalid or no framerate specified." );
316 goto error;
319 if( i_chroma == 0 )
321 msg_Err( p_demux, "invalid or no chroma specified." );
322 goto error;
325 /* fixup anything missing with sensible assumptions */
326 if( i_sar_num <= 0 || i_sar_den <= 0 )
328 /* assume 1:1 sar */
329 i_sar_num = 1;
330 i_sar_den = 1;
333 es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
334 video_format_Setup( &p_sys->fmt_video.video, i_chroma,
335 i_width, i_height, i_width, i_height,
336 i_sar_num, i_sar_den );
338 vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
339 &p_sys->fmt_video.video.i_frame_rate_base,
340 u_fps_num, u_fps_den, 0);
341 date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
342 p_sys->fmt_video.video.i_frame_rate_base );
343 date_Set( &p_sys->pcr, VLC_TICK_0 );
345 if( !p_sys->fmt_video.video.i_bits_per_pixel )
347 msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
348 (char*)&i_chroma );
349 goto error;
351 const vlc_chroma_description_t *dsc =
352 vlc_fourcc_GetChromaDescription(p_sys->fmt_video.video.i_chroma);
353 if (unlikely(dsc == NULL))
354 goto error;
355 p_sys->frame_size = 0;
356 for (unsigned i=0; i<dsc->plane_count; i++)
358 unsigned pitch = (i_width + (dsc->p[i].w.den - 1))
359 * dsc->p[i].w.num / dsc->p[i].w.den * dsc->pixel_size;
360 unsigned lines = (i_height + (dsc->p[i].h.den - 1))
361 * dsc->p[i].h.num / dsc->p[i].h.den;
362 p_sys->frame_size += pitch * lines;
364 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
366 p_demux->pf_demux = Demux;
367 p_demux->pf_control = Control;
368 return VLC_SUCCESS;
370 error:
371 free( p_sys );
372 return VLC_EGENERIC;
375 /*****************************************************************************
376 * Close: frees unused data
377 *****************************************************************************/
378 static void Close( vlc_object_t *p_this )
380 demux_t *p_demux = (demux_t*)p_this;
381 demux_sys_t *p_sys = p_demux->p_sys;
382 free( p_sys );
385 /*****************************************************************************
386 * Demux: reads and demuxes data packets
387 *****************************************************************************
388 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
389 *****************************************************************************/
390 static int Demux( demux_t *p_demux )
392 demux_sys_t *p_sys = p_demux->p_sys;
393 block_t *p_block;
394 vlc_tick_t i_pcr = date_Get( &p_sys->pcr );
396 /* Call the pace control */
397 es_out_SetPCR( p_demux->out, i_pcr );
399 if( p_sys->b_y4m )
401 /* Skip the frame header */
402 /* Skip "FRAME" */
403 if( vlc_stream_Read( p_demux->s, NULL, 5 ) < 5 )
404 return VLC_DEMUXER_EOF;
405 /* Find \n */
406 for( ;; )
408 uint8_t b;
409 if( vlc_stream_Read( p_demux->s, &b, 1 ) < 1 )
410 return VLC_DEMUXER_EOF;
411 if( b == 0x0a )
412 break;
416 p_block = vlc_stream_Block( p_demux->s, p_sys->frame_size );
417 if( p_block == NULL )
418 return VLC_DEMUXER_EOF;
420 p_block->i_dts = p_block->i_pts = i_pcr;
421 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
423 date_Increment( &p_sys->pcr, 1 );
425 return VLC_DEMUXER_SUCCESS;
428 /*****************************************************************************
429 * Control:
430 *****************************************************************************/
431 static int Control( demux_t *p_demux, int i_query, va_list args )
433 demux_sys_t *p_sys = p_demux->p_sys;
435 /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
436 const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
437 p_sys->pcr.i_divider_den;
439 /* XXX: DEMUX_SET_TIME is precise here */
440 return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
441 p_sys->frame_size, i_query, args );