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