1 /*****************************************************************************
2 * rawdv.c : raw DV input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Paul Corke <paul dot corke at datatote dot co dot uk>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define HURRYUP_TEXT N_( "Hurry up" )
41 #define HURRYUP_LONGTEXT N_( "The demuxer will advance timestamps if the " \
42 "input can't keep up with the rate." )
44 static int Open ( vlc_object_t
* );
45 static void Close( vlc_object_t
* );
49 set_description( N_("DV (Digital Video) demuxer") )
50 set_capability( "demux", 3 )
51 set_category( CAT_INPUT
)
52 set_subcategory( SUBCAT_INPUT_DEMUX
)
53 add_bool( "rawdv-hurry-up", false, HURRYUP_TEXT
, HURRYUP_LONGTEXT
, false )
54 set_callbacks( Open
, Close
)
55 add_shortcut( "rawdv" )
59 /*****************************************************************************
60 A little bit of background information (copied over from libdv glossary).
62 - DIF block: A block of 80 bytes. This is the basic data framing unit of the
63 DVC tape format, analogous to sectors of hard disc.
65 - Video Section: Each DIF sequence contains a video section, consisting of
66 135 DIF blocks, which are further subdivided into Video Segments.
68 - Video Segment: A video segment consists of 5 DIF blocks, each corresponding
69 to a single compressed macroblock.
71 *****************************************************************************/
74 /*****************************************************************************
76 *****************************************************************************/
77 #define DV_PAL_FRAME_SIZE 144000
78 #define DV_NTSC_FRAME_SIZE 122000
80 /*****************************************************************************
81 * Definitions of structures used by this plugin
82 *****************************************************************************/
84 int8_t sct
; /* Section type (header,subcode,aux,audio,video) */
85 int8_t dsn
; /* DIF sequence number (0-12) */
86 int fsc
; /* First (0)/Second channel (1) */
87 int8_t dbn
; /* DIF block number (0-134) */
91 int dsf
; /* DIF sequence flag: 525/60 (0) or 625,50 (1) */
105 es_out_id_t
*p_es_video
;
106 es_format_t fmt_video
;
108 es_out_id_t
*p_es_audio
;
109 es_format_t fmt_audio
;
115 /* program clock reference (in units of 90kHz) */
120 /*****************************************************************************
122 *****************************************************************************/
123 static int Demux( demux_t
* );
124 static int Control( demux_t
*, int i_query
, va_list args
);
126 static block_t
*dv_extract_audio( demux_t
*p_demux
,
127 block_t
* p_frame_block
);
129 /*****************************************************************************
130 * Open: initializes raw DV demux structures
131 *****************************************************************************/
132 static int Open( vlc_object_t
* p_this
)
134 demux_t
*p_demux
= (demux_t
*)p_this
;
137 const uint8_t *p_peek
, *p_peek_backup
;
140 dv_header_t dv_header
;
143 /* It isn't easy to recognize a raw DV stream. The chances that we'll
144 * mistake a stream from another type for a raw DV stream are too high, so
145 * we'll rely on the file extension to trigger this demux. Alternatively,
146 * it is possible to force this demux. */
148 /* Check for DV file extension */
149 if( !demux_IsPathExtension( p_demux
, ".dv" ) && !p_demux
->b_force
)
152 if( stream_Peek( p_demux
->s
, &p_peek
, DV_PAL_FRAME_SIZE
) <
155 /* Stream too short ... */
156 msg_Err( p_demux
, "cannot peek()" );
159 p_peek_backup
= p_peek
;
161 /* fill in the dv_id_t structure */
162 i_dword
= GetDWBE( p_peek
); p_peek
+= 4;
163 dv_id
.sct
= i_dword
>> (32 - 3);
165 dv_id
.dsn
= i_dword
>> (32 - 4);
167 dv_id
.fsc
= i_dword
>> (32 - 1);
169 dv_id
.dbn
= i_dword
>> (32 - 8);
174 msg_Warn( p_demux
, "not a raw DV stream header" );
178 /* fill in the dv_header_t structure */
179 dv_header
.dsf
= i_dword
>> (32 - 1);
181 if( i_dword
>> (32 - 1) ) /* incorrect bit */
183 msg_Warn( p_demux
, "incorrect bit" );
187 i_dword
= GetDWBE( p_peek
); p_peek
+= 4;
189 dv_header
.apt
= i_dword
>> (32 - 3);
191 dv_header
.tf1
= i_dword
>> (32 - 1);
193 dv_header
.ap1
= i_dword
>> (32 - 3);
195 dv_header
.tf2
= i_dword
>> (32 - 1);
197 dv_header
.ap2
= i_dword
>> (32 - 3);
199 dv_header
.tf3
= i_dword
>> (32 - 1);
201 dv_header
.ap3
= i_dword
>> (32 - 3);
203 p_peek
+= 72; /* skip rest of DIF block */
205 /* Set p_input field */
206 p_demux
->pf_demux
= Demux
;
207 p_demux
->pf_control
= Control
;
208 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
212 p_sys
->b_hurry_up
= var_CreateGetBool( p_demux
, "rawdv-hurry-up" );
213 msg_Dbg( p_demux
, "Realtime DV Source: %s", (p_sys
->b_hurry_up
)?"Yes":"No" );
215 p_sys
->i_dsf
= dv_header
.dsf
;
216 p_sys
->frame_size
= dv_header
.dsf
? 12 * 150 * 80 : 10 * 150 * 80;
217 p_sys
->f_rate
= dv_header
.dsf
? 25 : 29.97;
220 p_sys
->p_es_video
= NULL
;
221 p_sys
->p_es_audio
= NULL
;
223 p_sys
->i_bitrate
= 0;
225 es_format_Init( &p_sys
->fmt_video
, VIDEO_ES
, VLC_CODEC_DV
);
226 p_sys
->fmt_video
.video
.i_width
= 720;
227 p_sys
->fmt_video
.video
.i_height
= dv_header
.dsf
? 576 : 480;;
231 /* necessary because input_SplitBuffer() will only get
232 * INPUT_DEFAULT_BUFSIZE bytes at a time. */
233 p_input
->i_bufsize
= p_sys
->frame_size
;
236 p_sys
->p_es_video
= es_out_Add( p_demux
->out
, &p_sys
->fmt_video
);
239 p_peek
= p_peek_backup
+ 80*6+80*16*3 + 3; /* beginning of AAUX pack */
240 if( *p_peek
== 0x50 )
242 /* 12 bits non-linear will be converted to 16 bits linear */
243 es_format_Init( &p_sys
->fmt_audio
, AUDIO_ES
, VLC_CODEC_S16L
);
245 p_sys
->fmt_audio
.audio
.i_bitspersample
= 16;
246 p_sys
->fmt_audio
.audio
.i_channels
= 2;
247 switch( (p_peek
[4] >> 3) & 0x07 )
250 p_sys
->fmt_audio
.audio
.i_rate
= 48000;
253 p_sys
->fmt_audio
.audio
.i_rate
= 44100;
257 p_sys
->fmt_audio
.audio
.i_rate
= 32000;
261 p_sys
->p_es_audio
= es_out_Add( p_demux
->out
, &p_sys
->fmt_audio
);
267 /*****************************************************************************
268 * Close: frees unused data
269 *****************************************************************************/
270 static void Close( vlc_object_t
*p_this
)
272 demux_t
*p_demux
= (demux_t
*)p_this
;
273 demux_sys_t
*p_sys
= p_demux
->p_sys
;
275 var_Destroy( p_demux
, "rawdv-hurry-up");
279 /*****************************************************************************
280 * Demux: reads and demuxes data packets
281 *****************************************************************************
282 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
283 *****************************************************************************/
284 static int Demux( demux_t
*p_demux
)
286 demux_sys_t
*p_sys
= p_demux
->p_sys
;
288 bool b_audio
= false;
290 if( p_sys
->b_hurry_up
)
293 p_sys
->i_pcr
= mdate() + (p_sys
->i_dsf
? 120000 : 90000);
296 /* Call the pace control */
297 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, VLC_TS_0
+ p_sys
->i_pcr
);
298 p_block
= stream_Block( p_demux
->s
, p_sys
->frame_size
);
299 if( p_block
== NULL
)
305 if( p_sys
->p_es_audio
)
307 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
308 p_sys
->p_es_audio
, &b_audio
);
312 p_block
->i_pts
= VLC_TS_0
+ p_sys
->i_pcr
;
316 block_t
*p_audio_block
= dv_extract_audio( p_demux
, p_block
);
319 p_audio_block
->i_pts
=
320 p_audio_block
->i_dts
= VLC_TS_0
+ p_sys
->i_pcr
;
321 es_out_Send( p_demux
->out
, p_sys
->p_es_audio
, p_audio_block
);
325 es_out_Send( p_demux
->out
, p_sys
->p_es_video
, p_block
);
327 if( !p_sys
->b_hurry_up
)
329 p_sys
->i_pcr
+= ( INT64_C(1000000) / p_sys
->f_rate
);
335 /*****************************************************************************
337 *****************************************************************************/
338 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
340 demux_sys_t
*p_sys
= p_demux
->p_sys
;
342 /* XXX: DEMUX_SET_TIME is precise here */
343 return demux_vaControlHelper( p_demux
->s
,
345 p_sys
->frame_size
* p_sys
->f_rate
* 8,
346 p_sys
->frame_size
, i_query
, args
);
349 static const uint16_t dv_audio_shuffle525
[10][9] = {
350 { 0, 30, 60, 20, 50, 80, 10, 40, 70 }, /* 1st channel */
351 { 6, 36, 66, 26, 56, 86, 16, 46, 76 },
352 { 12, 42, 72, 2, 32, 62, 22, 52, 82 },
353 { 18, 48, 78, 8, 38, 68, 28, 58, 88 },
354 { 24, 54, 84, 14, 44, 74, 4, 34, 64 },
356 { 1, 31, 61, 21, 51, 81, 11, 41, 71 }, /* 2nd channel */
357 { 7, 37, 67, 27, 57, 87, 17, 47, 77 },
358 { 13, 43, 73, 3, 33, 63, 23, 53, 83 },
359 { 19, 49, 79, 9, 39, 69, 29, 59, 89 },
360 { 25, 55, 85, 15, 45, 75, 5, 35, 65 },
363 static const uint16_t dv_audio_shuffle625
[12][9] = {
364 { 0, 36, 72, 26, 62, 98, 16, 52, 88}, /* 1st channel */
365 { 6, 42, 78, 32, 68, 104, 22, 58, 94},
366 { 12, 48, 84, 2, 38, 74, 28, 64, 100},
367 { 18, 54, 90, 8, 44, 80, 34, 70, 106},
368 { 24, 60, 96, 14, 50, 86, 4, 40, 76},
369 { 30, 66, 102, 20, 56, 92, 10, 46, 82},
371 { 1, 37, 73, 27, 63, 99, 17, 53, 89}, /* 2nd channel */
372 { 7, 43, 79, 33, 69, 105, 23, 59, 95},
373 { 13, 49, 85, 3, 39, 75, 29, 65, 101},
374 { 19, 55, 91, 9, 45, 81, 35, 71, 107},
375 { 25, 61, 97, 15, 51, 87, 5, 41, 77},
376 { 31, 67, 103, 21, 57, 93, 11, 47, 83},
379 static inline uint16_t dv_audio_12to16( uint16_t sample
)
381 uint16_t shift
, result
;
383 sample
= (sample
< 0x800) ? sample
: sample
| 0xf000;
384 shift
= (sample
& 0xf00) >> 8;
386 if (shift
< 0x2 || shift
> 0xd) {
388 } else if (shift
< 0x8) {
390 result
= (sample
- (256 * shift
)) << shift
;
393 result
= ((sample
+ ((256 * shift
) + 1)) << shift
) - 1;
399 static block_t
*dv_extract_audio( demux_t
*p_demux
,
400 block_t
* p_frame_block
)
402 demux_sys_t
*p_sys
= p_demux
->p_sys
;
404 uint8_t *p_frame
, *p_buf
;
405 int i_audio_quant
, i_samples
, i_size
, i_half_ch
;
406 const uint16_t (*audio_shuffle
)[9];
410 /* Beginning of AAUX pack */
411 p_buf
= p_frame_block
->p_buffer
+ 80*6+80*16*3 + 3;
412 if( *p_buf
!= 0x50 ) return NULL
;
414 i_audio_quant
= p_buf
[4] & 0x07; /* 0 - 16bit, 1 - 12bit */
415 if( i_audio_quant
> 1 )
417 msg_Dbg( p_demux
, "unsupported quantization for DV audio");
421 i_samples
= p_buf
[1] & 0x3f; /* samples in this frame - min samples */
422 switch( (p_buf
[4] >> 3) & 0x07 )
425 i_size
= p_sys
->i_dsf
? 1896 : 1580;
428 i_size
= p_sys
->i_dsf
? 1742 : 1452;
432 i_size
= p_sys
->i_dsf
? 1264 : 1053;
435 i_size
= (i_size
+ i_samples
) * 4; /* 2ch, 2bytes */
437 p_block
= block_New( p_demux
, i_size
);
439 /* for each DIF segment */
440 p_frame
= p_frame_block
->p_buffer
;
441 audio_shuffle
= p_sys
->i_dsf
? dv_audio_shuffle625
: dv_audio_shuffle525
;
442 i_half_ch
= (p_sys
->i_dsf
? 12 : 10)/2;
443 for( i
= 0; i
< (p_sys
->i_dsf
? 12 : 10); i
++ )
445 p_frame
+= 6 * 80; /* skip DIF segment header */
447 if( i_audio_quant
== 1 && i
== i_half_ch
) break;
449 for( j
= 0; j
< 9; j
++ )
451 for( d
= 8; d
< 80; d
+= 2 )
453 if( i_audio_quant
== 0 )
455 /* 16bit quantization */
456 of
= audio_shuffle
[i
][j
] + (d
- 8) / 2 *
457 (p_sys
->i_dsf
? 108 : 90);
459 if( of
* 2 >= i_size
) continue;
462 p_block
->p_buffer
[of
*2] = p_frame
[d
+1];
463 p_block
->p_buffer
[of
*2+1] = p_frame
[d
];
465 if( p_block
->p_buffer
[of
*2+1] == 0x80 &&
466 p_block
->p_buffer
[of
*2] == 0x00 )
467 p_block
->p_buffer
[of
*2+1] = 0;
471 /* 12bit quantization */
472 lc
= ((uint16_t)p_frame
[d
] << 4) |
473 ((uint16_t)p_frame
[d
+2] >> 4);
474 lc
= (lc
== 0x800 ? 0 : dv_audio_12to16(lc
));
476 of
= audio_shuffle
[i
][j
] + (d
- 8) / 3 *
477 (p_sys
->i_dsf
? 108 : 90);
478 if( of
*2 >= i_size
) continue;
481 p_block
->p_buffer
[of
*2] = lc
& 0xff;
482 p_block
->p_buffer
[of
*2+1] = lc
>> 8;
487 p_frame
+= 16 * 80; /* 15 Video DIFs + 1 Audio DIF */