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