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