1 /*****************************************************************************
2 * rawdv.c : raw DV input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
39 set_description( _("raw DV demuxer") );
40 set_capability( "demux2", 2 );
41 set_callbacks( Open, Close );
42 add_shortcut( "rawdv" );
46 /*****************************************************************************
47 A little bit of background information (copied over from libdv glossary).
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.
52 - Video Section: Each DIF sequence contains a video section, consisting of
53 135 DIF blocks, which are further subdivided into Video Segments.
55 - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
56 to a single compressed macroblock.
58 *****************************************************************************/
61 /*****************************************************************************
63 *****************************************************************************/
64 #define DV_PAL_FRAME_SIZE 144000
65 #define DV_NTSC_FRAME_SIZE 122000
67 /*****************************************************************************
68 * Definitions of structures used by this plugin
69 *****************************************************************************/
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) */
78 int dsf; /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
92 es_out_id_t *p_es_video;
93 es_format_t fmt_video;
95 es_out_id_t *p_es_audio;
96 es_format_t fmt_audio;
102 /* program clock reference (in units of 90kHz) */
106 /*****************************************************************************
108 *****************************************************************************/
109 static int Demux( demux_t * );
110 static int Control( demux_t *, int i_query, va_list args );
112 static block_t *dv_extract_audio( demux_t *p_demux,
113 block_t* p_frame_block );
115 /*****************************************************************************
116 * Open: initializes raw DV demux structures
117 *****************************************************************************/
118 static int Open( vlc_object_t * p_this )
120 demux_t *p_demux = (demux_t*)p_this;
123 byte_t *p_peek, *p_peek_backup;
126 dv_header_t dv_header;
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. */
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") )
143 if( stream_Peek( p_demux->s, &p_peek, DV_PAL_FRAME_SIZE ) <
146 /* Stream too short ... */
147 msg_Err( p_demux, "cannot peek()" );
150 p_peek_backup = p_peek;
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);
156 dv_id.dsn = i_dword >> (32 - 4);
158 dv_id.fsc = i_dword >> (32 - 1);
160 dv_id.dbn = i_dword >> (32 - 8);
165 msg_Warn( p_demux, "not a raw DV stream header" );
169 /* fill in the dv_header_t structure */
170 dv_header.dsf = i_dword >> (32 - 1);
172 if( i_dword >> (32 - 1) ) /* incorrect bit */
174 msg_Warn( p_demux, "incorrect bit" );
178 i_dword = GetDWBE( p_peek ); p_peek += 4;
180 dv_header.apt = i_dword >> (32 - 3);
182 dv_header.tf1 = i_dword >> (32 - 1);
184 dv_header.ap1 = i_dword >> (32 - 3);
186 dv_header.tf2 = i_dword >> (32 - 1);
188 dv_header.ap2 = i_dword >> (32 - 3);
190 dv_header.tf3 = i_dword >> (32 - 1);
192 dv_header.ap3 = i_dword >> (32 - 3);
194 p_peek += 72; /* skip rest of DIF block */
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 ) );
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;
207 p_sys->p_es_video = NULL;
208 p_sys->p_es_audio = NULL;
210 p_sys->i_bitrate = 0;
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;;
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;
223 p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
226 p_peek = p_peek_backup + 80*6+80*16*3 + 3; /* beginning of AAUX pack */
227 if( *p_peek == 0x50 )
229 es_format_Init( &p_sys->fmt_audio, AUDIO_ES,
230 VLC_FOURCC('a','r','a','w') );
232 p_sys->fmt_audio.audio.i_channels = 2;
233 switch( (p_peek[4] >> 3) & 0x07 )
236 p_sys->fmt_audio.audio.i_rate = 48000;
239 p_sys->fmt_audio.audio.i_rate = 44100;
243 p_sys->fmt_audio.audio.i_rate = 32000;
247 /* 12 bits non-linear will be converted to 16 bits linear */
248 p_sys->fmt_audio.audio.i_bitspersample = 16;
250 p_sys->p_es_audio = es_out_Add( p_demux->out, &p_sys->fmt_audio );
256 /*****************************************************************************
257 * Close: frees unused data
258 *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
261 demux_t *p_demux = (demux_t*)p_this;
262 demux_sys_t *p_sys = p_demux->p_sys;
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 )
274 demux_sys_t *p_sys = p_demux->p_sys;
276 vlc_bool_t b_audio = VLC_FALSE;
278 /* Call the pace control */
279 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
281 if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
287 if( p_sys->p_es_audio )
289 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
290 p_sys->p_es_audio, &b_audio );
294 p_block->i_pts = p_sys->i_pcr;
298 block_t *p_audio_block = dv_extract_audio( p_demux, p_block );
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 );
307 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
309 p_sys->i_pcr += ( I64C(1000000) / p_sys->f_rate );
314 /*****************************************************************************
316 *****************************************************************************/
317 static int Control( demux_t *p_demux, int i_query, va_list args )
319 demux_sys_t *p_sys = p_demux->p_sys;
321 /* XXX: DEMUX_SET_TIME is precise here */
322 return demux2_vaControlHelper( p_demux->s,
324 p_sys->frame_size * p_sys->f_rate * 8,
325 p_sys->frame_size, i_query, args );
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 },
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 },
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},
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},
358 static inline uint16_t dv_audio_12to16( uint16_t sample )
360 uint16_t shift, result;
362 sample = (sample < 0x800) ? sample : sample | 0xf000;
363 shift = (sample & 0xf00) >> 8;
365 if (shift < 0x2 || shift > 0xd) {
367 } else if (shift < 0x8) {
369 result = (sample - (256 * shift)) << shift;
372 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
378 static block_t *dv_extract_audio( demux_t *p_demux,
379 block_t* p_frame_block )
381 demux_sys_t *p_sys = p_demux->p_sys;
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];
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;
393 i_audio_quant = p_buf[4] & 0x07; /* 0 - 16bit, 1 - 12bit */
394 if( i_audio_quant > 1 )
396 msg_Dbg( p_demux, "Unsupported quantization for DV audio");
400 i_samples = p_buf[1] & 0x3f; /* samples in this frame - min samples */
401 switch( (p_buf[4] >> 3) & 0x07 )
404 i_size = p_sys->i_dsf ? 1896 : 1580;
407 i_size = p_sys->i_dsf ? 1742 : 1452;
411 i_size = p_sys->i_dsf ? 1264 : 1053;
414 i_size = (i_size + i_samples) * 4; /* 2ch, 2bytes */
416 p_block = block_New( p_demux, i_size );
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++ )
424 p_frame += 6 * 80; /* skip DIF segment header */
426 if( i_audio_quant == 1 && i == i_half_ch ) break;
428 for( j = 0; j < 9; j++ )
430 for( d = 8; d < 80; d += 2 )
432 if( i_audio_quant == 0 )
434 /* 16bit quantization */
435 of = audio_shuffle[i][j] + (d - 8) / 2 *
436 (p_sys->i_dsf ? 108 : 90);
438 if( of * 2 >= i_size ) continue;
441 p_block->p_buffer[of*2] = p_frame[d+1];
442 p_block->p_buffer[of*2+1] = p_frame[d];
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;
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));
455 of = audio_shuffle[i][j] + (d - 8) / 3 *
456 (p_sys->i_dsf ? 108 : 90);
457 if( of*2 >= i_size ) continue;
460 p_block->p_buffer[of*2] = lc & 0xff;
461 p_block->p_buffer[of*2+1] = lc >> 8;
466 p_frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */