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