]> git.sesse.net Git - vlc/blob - modules/demux/rawdv.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / demux / rawdv.c
1 /*****************************************************************************
2  * rawdv.c : raw DV input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Paul Corke <paul dot corke at datatote dot co dot uk>
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
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 #define HURRYUP_TEXT N_( "Hurry up" )
36 #define HURRYUP_LONGTEXT N_( "The demuxer will advance timestamps if the " \
37                 "input can't keep up with the rate." )
38
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 vlc_module_begin();
43     set_shortname( "DV" );
44     set_description( _("DV (Digital Video) demuxer") );
45     set_capability( "demux2", 3 );
46     set_category( CAT_INPUT );
47     set_subcategory( SUBCAT_INPUT_DEMUX );
48     add_bool( "rawdv-hurry-up", 0, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT, VLC_FALSE );
49         change_safe();
50     set_callbacks( Open, Close );
51     add_shortcut( "rawdv" );
52 vlc_module_end();
53
54
55 /*****************************************************************************
56  A little bit of background information (copied over from libdv glossary).
57
58  - DIF block: A block of 80 bytes. This is the basic data framing unit of the
59        DVC tape format, analogous to sectors of hard disc.
60
61  - Video Section: Each DIF sequence contains a video section, consisting of
62        135 DIF blocks, which are further subdivided into Video Segments.
63
64  - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
65        to a single compressed macroblock.
66
67 *****************************************************************************/
68
69
70 /*****************************************************************************
71  * Constants
72  *****************************************************************************/
73 #define DV_PAL_FRAME_SIZE  144000
74 #define DV_NTSC_FRAME_SIZE 122000
75
76 /*****************************************************************************
77  * Definitions of structures used by this plugin
78  *****************************************************************************/
79 typedef struct {
80     int8_t sct;      /* Section type (header,subcode,aux,audio,video) */
81     int8_t dsn;      /* DIF sequence number (0-12) */
82     int    fsc;      /* First (0)/Second channel (1) */
83     int8_t dbn;      /* DIF block number (0-134) */
84 } dv_id_t;
85
86 typedef struct {
87     int    dsf;      /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
88     int8_t apt;
89     int    tf1;
90     int8_t ap1;
91     int    tf2;
92     int8_t ap2;
93     int    tf3;
94     int8_t ap3;
95 } dv_header_t;
96
97 struct demux_sys_t
98 {
99     int    frame_size;
100
101     es_out_id_t *p_es_video;
102     es_format_t  fmt_video;
103
104     es_out_id_t *p_es_audio;
105     es_format_t  fmt_audio;
106
107     int    i_dsf;
108     double f_rate;
109     int    i_bitrate;
110
111     /* program clock reference (in units of 90kHz) */
112     mtime_t i_pcr;
113     vlc_bool_t b_hurry_up;
114 };
115
116 /*****************************************************************************
117  * Local prototypes
118  *****************************************************************************/
119 static int Demux( demux_t * );
120 static int Control( demux_t *, int i_query, va_list args );
121
122 static block_t *dv_extract_audio( demux_t *p_demux,
123                                   block_t* p_frame_block );
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
133     const byte_t *p_peek, *p_peek_backup;
134
135     uint32_t    i_dword;
136     dv_header_t dv_header;
137     dv_id_t     dv_id;
138
139     /* It isn't easy to recognize a raw DV stream. The chances that we'll
140      * mistake a stream from another type for a raw DV stream are too high, so
141      * we'll rely on the file extension to trigger this demux. Alternatively,
142      * it is possible to force this demux. */
143
144     /* Check for DV file extension */
145     if( !demux2_IsPathExtension( p_demux, ".dv" ) && !p_demux->b_force )
146         return VLC_EGENERIC;
147
148     if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
149         DV_NTSC_FRAME_SIZE )
150     {
151         /* Stream too short ... */
152         msg_Err( p_demux, "cannot peek()" );
153         return VLC_EGENERIC;
154     }
155     p_peek_backup = p_peek;
156
157     /* fill in the dv_id_t structure */
158     i_dword = GetDWBE( p_peek ); p_peek += 4;
159     dv_id.sct = i_dword >> (32 - 3);
160     i_dword <<= 8;
161     dv_id.dsn = i_dword >> (32 - 4);
162     i_dword <<= 4;
163     dv_id.fsc = i_dword >> (32 - 1);
164     i_dword <<= 4;
165     dv_id.dbn = i_dword >> (32 - 8);
166     i_dword <<= 8;
167
168     if( dv_id.sct != 0 )
169     {
170         msg_Warn( p_demux, "not a raw DV stream header" );
171         return VLC_EGENERIC;
172     }
173
174     /* fill in the dv_header_t structure */
175     dv_header.dsf = i_dword >> (32 - 1);
176     i_dword <<= 1;
177     if( i_dword >> (32 - 1) ) /* incorrect bit */
178     {
179         msg_Warn( p_demux, "incorrect bit" );
180         return VLC_EGENERIC;
181     }
182
183     i_dword = GetDWBE( p_peek ); p_peek += 4;
184     i_dword <<= 5;
185     dv_header.apt = i_dword >> (32 - 3);
186     i_dword <<= 3;
187     dv_header.tf1 = i_dword >> (32 - 1);
188     i_dword <<= 5;
189     dv_header.ap1 = i_dword >> (32 - 3);
190     i_dword <<= 3;
191     dv_header.tf2 = i_dword >> (32 - 1);
192     i_dword <<= 5;
193     dv_header.ap2 = i_dword >> (32 - 3);
194     i_dword <<= 3;
195     dv_header.tf3 = i_dword >> (32 - 1);
196     i_dword <<= 5;
197     dv_header.ap3 = i_dword >> (32 - 3);
198
199     p_peek += 72;                                  /* skip rest of DIF block */
200
201     /* Set p_input field */
202     p_demux->pf_demux   = Demux;
203     p_demux->pf_control = Control;
204     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
205     if( !p_sys )
206         return VLC_ENOMEM;
207
208     p_sys->b_hurry_up = var_CreateGetBool( p_demux, "rawdv-hurry-up" );
209     msg_Dbg( p_demux, "Realtime DV Source: %s", (p_sys->b_hurry_up)?"Yes":"No" );
210
211     p_sys->i_dsf = dv_header.dsf;
212     p_sys->frame_size = dv_header.dsf ? 12 * 150 * 80 : 10 * 150 * 80;
213     p_sys->f_rate = dv_header.dsf ? 25 : 29.97;
214
215     p_sys->i_pcr = 1;
216     p_sys->p_es_video = NULL;
217     p_sys->p_es_audio = NULL;
218
219     p_sys->i_bitrate = 0;
220
221     es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC('d','v','s','d') );
222     p_sys->fmt_video.video.i_width = 720;
223     p_sys->fmt_video.video.i_height= dv_header.dsf ? 576 : 480;;
224
225     /* FIXME FIXME */
226 #if 0
227     /* necessary because input_SplitBuffer() will only get
228      * INPUT_DEFAULT_BUFSIZE bytes at a time. */
229     p_input->i_bufsize = p_sys->frame_size;
230 #endif
231
232     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
233
234     /* Audio stuff */
235     p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
236     if( *p_peek == 0x50 )
237     {
238         es_format_Init( &p_sys->fmt_audio, AUDIO_ES,
239                         VLC_FOURCC('a','r','a','w') );
240
241         p_sys->fmt_audio.audio.i_channels = 2;
242         switch( (p_peek[4] >> 3) & 0x07 )
243         {
244         case 0:
245             p_sys->fmt_audio.audio.i_rate = 48000;
246             break;
247         case 1:
248             p_sys->fmt_audio.audio.i_rate = 44100;
249             break;
250         case 2:
251         default:
252             p_sys->fmt_audio.audio.i_rate = 32000;
253             break;
254         }
255
256         /* 12 bits non-linear will be converted to 16 bits linear */
257         p_sys->fmt_audio.audio.i_bitspersample = 16;
258
259         p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
260     }
261
262     return VLC_SUCCESS;
263 }
264
265 /*****************************************************************************
266  * Close: frees unused data
267  *****************************************************************************/
268 static void Close( vlc_object_t *p_this )
269 {
270     demux_t     *p_demux = (demux_t*)p_this;
271     demux_sys_t *p_sys  = p_demux->p_sys;
272
273     var_Destroy( p_demux, "rawdv-hurry-up");
274     free( p_sys );
275 }
276
277 /*****************************************************************************
278  * Demux: reads and demuxes data packets
279  *****************************************************************************
280  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
281  *****************************************************************************/
282 static int Demux( demux_t *p_demux )
283 {
284     demux_sys_t *p_sys  = p_demux->p_sys;
285     block_t     *p_block;
286     vlc_bool_t  b_audio = VLC_FALSE;
287
288     if( p_sys->b_hurry_up )
289     {
290          /* 3 frames */
291         p_sys->i_pcr = mdate() + (p_sys->i_dsf ? 120000 : 90000);
292     }
293
294     /* Call the pace control */
295     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
296     p_block = stream_Block( p_demux->s, p_sys->frame_size );
297     if( p_block == NULL )
298     {
299         /* EOF */
300         return 0;
301     }
302
303     if( p_sys->p_es_audio )
304     {
305         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
306                         p_sys->p_es_audio, &b_audio );
307     }
308
309     p_block->i_dts =
310     p_block->i_pts = p_sys->i_pcr;
311
312     if( b_audio )
313     {
314         block_t *p_audio_block = dv_extract_audio( p_demux, p_block );
315         if( p_audio_block )
316         {
317             p_audio_block->i_pts =
318             p_audio_block->i_dts = p_sys->i_pcr;
319             es_out_Send( p_demux->out, p_sys->p_es_audio, p_audio_block );
320         }
321     }
322
323     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
324
325     if( !p_sys->b_hurry_up )
326     {
327         p_sys->i_pcr += ( I64C(1000000) / p_sys->f_rate );
328     }
329
330     return 1;
331 }
332
333 /*****************************************************************************
334  * Control:
335  *****************************************************************************/
336 static int Control( demux_t *p_demux, int i_query, va_list args )
337 {
338     demux_sys_t *p_sys  = p_demux->p_sys;
339
340     /* XXX: DEMUX_SET_TIME is precise here */
341     return demux2_vaControlHelper( p_demux->s,
342                                    0, -1,
343                                    p_sys->frame_size * p_sys->f_rate * 8,
344                                    p_sys->frame_size, i_query, args );
345 }
346
347 static const uint16_t dv_audio_shuffle525[10][9] = {
348   {  0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
349   {  6, 36, 66, 26, 56, 86, 16, 46, 76 },
350   { 12, 42, 72,  2, 32, 62, 22, 52, 82 },
351   { 18, 48, 78,  8, 38, 68, 28, 58, 88 },
352   { 24, 54, 84, 14, 44, 74,  4, 34, 64 },
353
354   {  1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
355   {  7, 37, 67, 27, 57, 87, 17, 47, 77 },
356   { 13, 43, 73,  3, 33, 63, 23, 53, 83 },
357   { 19, 49, 79,  9, 39, 69, 29, 59, 89 },
358   { 25, 55, 85, 15, 45, 75,  5, 35, 65 },
359 };
360
361 static const uint16_t dv_audio_shuffle625[12][9] = {
362   {   0,  36,  72,  26,  62,  98,  16,  52,  88}, /* 1st channel */
363   {   6,  42,  78,  32,  68, 104,  22,  58,  94},
364   {  12,  48,  84,   2,  38,  74,  28,  64, 100},
365   {  18,  54,  90,   8,  44,  80,  34,  70, 106},
366   {  24,  60,  96,  14,  50,  86,   4,  40,  76},
367   {  30,  66, 102,  20,  56,  92,  10,  46,  82},
368
369   {   1,  37,  73,  27,  63,  99,  17,  53,  89}, /* 2nd channel */
370   {   7,  43,  79,  33,  69, 105,  23,  59,  95},
371   {  13,  49,  85,   3,  39,  75,  29,  65, 101},
372   {  19,  55,  91,   9,  45,  81,  35,  71, 107},
373   {  25,  61,  97,  15,  51,  87,   5,  41,  77},
374   {  31,  67, 103,  21,  57,  93,  11,  47,  83},
375 };
376
377 static inline uint16_t dv_audio_12to16( uint16_t sample )
378 {
379     uint16_t shift, result;
380
381     sample = (sample < 0x800) ? sample : sample | 0xf000;
382     shift = (sample & 0xf00) >> 8;
383
384     if (shift < 0x2 || shift > 0xd) {
385         result = sample;
386     } else if (shift < 0x8) {
387         shift--;
388         result = (sample - (256 * shift)) << shift;
389     } else {
390         shift = 0xe - shift;
391         result = ((sample + ((256 * shift) + 1)) << shift) - 1;
392     }
393
394     return result;
395 }
396
397 static block_t *dv_extract_audio( demux_t *p_demux,
398                                   block_t* p_frame_block )
399 {
400     demux_sys_t *p_sys  = p_demux->p_sys;
401     block_t *p_block;
402     uint8_t *p_frame, *p_buf;
403     int i_audio_quant, i_samples, i_size, i_half_ch;
404     const uint16_t (*audio_shuffle)[9];
405     int i, j, d, of;
406     uint16_t lc;
407
408     /* Beginning of AAUX pack */
409     p_buf = p_frame_block->p_buffer + 80*6+80*16*3 + 3;
410     if( *p_buf != 0x50 ) return NULL;
411
412     i_audio_quant = p_buf[4] & 0x07; /* 0 - 16bit, 1 - 12bit */
413     if( i_audio_quant > 1 )
414     {
415         msg_Dbg( p_demux, "unsupported quantization for DV audio");
416         return NULL;
417     }
418
419     i_samples = p_buf[1] & 0x3f; /* samples in this frame - min samples */
420     switch( (p_buf[4] >> 3) & 0x07 )
421     {
422     case 0:
423         i_size = p_sys->i_dsf ? 1896 : 1580;
424         break;
425     case 1:
426         i_size = p_sys->i_dsf ? 1742 : 1452;
427         break;
428     case 2:
429     default:
430         i_size = p_sys->i_dsf ? 1264 : 1053;
431         break;
432     }
433     i_size = (i_size + i_samples) * 4; /* 2ch, 2bytes */
434
435     p_block = block_New( p_demux, i_size );
436
437     /* for each DIF segment */
438     p_frame = p_frame_block->p_buffer;
439     audio_shuffle = p_sys->i_dsf ? dv_audio_shuffle625 : dv_audio_shuffle525;
440     i_half_ch = (p_sys->i_dsf ? 12 : 10)/2;
441     for( i = 0; i < (p_sys->i_dsf ? 12 : 10); i++ )
442     {
443         p_frame += 6 * 80; /* skip DIF segment header */
444
445         if( i_audio_quant == 1 && i == i_half_ch ) break;
446
447         for( j = 0; j < 9; j++ )
448         {
449             for( d = 8; d < 80; d += 2 )
450             {
451                 if( i_audio_quant == 0 )
452                 {
453                     /* 16bit quantization */
454                     of = audio_shuffle[i][j] + (d - 8) / 2 *
455                            (p_sys->i_dsf ? 108 : 90);
456
457                     if( of * 2 >= i_size ) continue;
458
459                     /* big endian */
460                     p_block->p_buffer[of*2] = p_frame[d+1];
461                     p_block->p_buffer[of*2+1] = p_frame[d];
462
463                     if( p_block->p_buffer[of*2+1] == 0x80 &&
464                         p_block->p_buffer[of*2] == 0x00 )
465                         p_block->p_buffer[of*2+1] = 0;
466                 }
467                 else
468                 {
469                     /* 12bit quantization */
470                     lc = ((uint16_t)p_frame[d] << 4) |
471                          ((uint16_t)p_frame[d+2] >> 4);
472                     lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
473
474                     of = audio_shuffle[i][j] + (d - 8) / 3 *
475                          (p_sys->i_dsf ? 108 : 90);
476                     if( of*2 >= i_size ) continue;
477
478                     /* big endian */
479                     p_block->p_buffer[of*2] = lc & 0xff;
480                     p_block->p_buffer[of*2+1] = lc >> 8;
481                     ++d;
482                 }
483             }
484
485             p_frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
486         }
487     }
488
489     return p_block;
490 }