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