1 /*****************************************************************************
2 * dts.c: parse DTS audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
7 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Gildas Bazin <gbazin@netcourrier.com>
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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_block_helper.h>
39 #include <vlc_modules.h>
42 #include "../packetizer/packetizer_helper.h"
43 #include "dts_header.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int OpenDecoder ( vlc_object_t * );
49 static int OpenPacketizer( vlc_object_t * );
50 static void CloseCommon ( vlc_object_t * );
53 set_description( N_("DTS parser") )
54 set_capability( "decoder", 100 )
55 set_callbacks( OpenDecoder, CloseCommon )
58 set_description( N_("DTS audio packetizer") )
59 set_capability( "packetizer", 10 )
60 set_callbacks( OpenPacketizer, CloseCommon )
63 /*****************************************************************************
64 * decoder_sys_t : decoder descriptor
65 *****************************************************************************/
76 block_bytestream_t bytestream;
85 bool b_dts_hd; /* Is the current frame a DTS HD one */
86 unsigned int i_bit_rate;
87 unsigned int i_frame_size;
88 unsigned int i_frame_length;
90 unsigned int i_channels;
91 unsigned int i_channels_conf;
94 /****************************************************************************
96 ****************************************************************************/
97 static int OpenCommon( vlc_object_t *, bool b_packetizer );
98 static block_t *DecodeBlock( decoder_t *, block_t ** );
100 static int SyncInfo( const uint8_t *, bool *, unsigned int *, unsigned int *,
101 unsigned int *, unsigned int *, unsigned int * );
103 static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
104 static block_t *GetAoutBuffer( decoder_t * );
105 static block_t *GetSoutBuffer( decoder_t * );
107 /*****************************************************************************
108 * OpenDecoder: probe the decoder
109 *****************************************************************************/
110 static int OpenDecoder( vlc_object_t *p_this )
112 /* HACK: Don't use this codec if we don't have an dts audio filter */
113 if( !module_exists( "dtstofloat32" ) )
116 return OpenCommon( p_this, false );
119 /*****************************************************************************
120 * OpenPacketizer: probe the packetizer
121 *****************************************************************************/
122 static int OpenPacketizer( vlc_object_t *p_this )
124 return OpenCommon( p_this, true );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
132 decoder_t *p_dec = (decoder_t*)p_this;
133 decoder_sys_t *p_sys;
135 if( p_dec->fmt_in.i_codec != VLC_CODEC_DTS )
138 /* Allocate the memory needed to store the decoder's structure */
139 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
143 p_sys->b_packetizer = b_packetizer;
144 p_sys->i_state = STATE_NOSYNC;
145 date_Set( &p_sys->end_date, 0 );
146 p_sys->b_dts_hd = false;
147 p_sys->i_pts = VLC_TS_INVALID;
149 block_BytestreamInit( &p_sys->bytestream );
151 /* Set output properties */
152 p_dec->fmt_out.i_cat = AUDIO_ES;
153 p_dec->fmt_out.i_codec = VLC_CODEC_DTS;
154 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
157 p_dec->pf_decode_audio = DecodeBlock;
158 p_dec->pf_packetize = DecodeBlock;
163 /****************************************************************************
164 * DecodeBlock: the whole thing
165 ****************************************************************************/
166 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
168 decoder_sys_t *p_sys = p_dec->p_sys;
169 uint8_t p_header[DTS_HEADER_SIZE];
171 block_t *p_out_buffer;
173 if( !pp_block || !*pp_block )
176 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
178 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
180 p_sys->i_state = STATE_NOSYNC;
181 block_BytestreamEmpty( &p_sys->bytestream );
183 date_Set( &p_sys->end_date, 0 );
184 block_Release( *pp_block );
188 if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
190 /* We've just started the stream, wait for the first PTS. */
191 block_Release( *pp_block );
195 block_BytestreamPush( &p_sys->bytestream, *pp_block );
199 switch( p_sys->i_state )
202 /* Look for sync code - should be 0x7ffe8001 */
203 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
206 if( SyncCode( p_header ) == VLC_SUCCESS )
208 p_sys->i_state = STATE_SYNC;
211 block_SkipByte( &p_sys->bytestream );
213 if( p_sys->i_state != STATE_SYNC )
215 block_BytestreamFlush( &p_sys->bytestream );
222 /* New frame, set the Presentation Time Stamp */
223 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
224 if( p_sys->i_pts > VLC_TS_INVALID &&
225 p_sys->i_pts != date_Get( &p_sys->end_date ) )
227 date_Set( &p_sys->end_date, p_sys->i_pts );
229 p_sys->i_state = STATE_HEADER;
232 /* Get DTS frame header (DTS_HEADER_SIZE bytes) */
233 if( block_PeekBytes( &p_sys->bytestream, p_header,
234 DTS_HEADER_SIZE ) != VLC_SUCCESS )
240 /* Check if frame is valid and get frame info */
241 p_sys->i_frame_size = SyncInfo( p_header,
244 &p_sys->i_channels_conf,
247 &p_sys->i_frame_length );
248 if( !p_sys->i_frame_size )
250 msg_Dbg( p_dec, "emulated sync word" );
251 block_SkipByte( &p_sys->bytestream );
252 p_sys->i_state = STATE_NOSYNC;
255 p_sys->i_state = STATE_NEXT_SYNC;
257 case STATE_NEXT_SYNC:
258 /* TODO: If pp_block == NULL, flush the buffer without checking the
261 /* Check if next expected frame contains the sync word */
262 if( block_PeekOffsetBytes( &p_sys->bytestream,
263 p_sys->i_frame_size, p_header, 6 )
270 if( p_sys->b_packetizer &&
271 p_header[0] == 0 && p_header[1] == 0 )
273 /* DTS wav files and audio CD's use stuffing */
274 p_sys->i_state = STATE_SEND_DATA;
278 if( SyncCode( p_header ) != VLC_SUCCESS )
280 msg_Dbg( p_dec, "emulated sync word "
281 "(no sync on following frame): %2.2x%2.2x%2.2x%2.2x",
282 (int)p_header[0], (int)p_header[1],
283 (int)p_header[2], (int)p_header[3] );
284 p_sys->i_state = STATE_NOSYNC;
285 block_SkipByte( &p_sys->bytestream );
288 p_sys->i_state = STATE_SEND_DATA;
292 /* Make sure we have enough data.
293 * (Not useful if we went through NEXT_SYNC) */
294 if( block_WaitBytes( &p_sys->bytestream,
295 p_sys->i_frame_size ) != VLC_SUCCESS )
300 p_sys->i_state = STATE_SEND_DATA;
302 case STATE_SEND_DATA:
303 if( p_sys->b_dts_hd )
306 block_SkipBytes( &p_sys->bytestream, p_sys->i_frame_size );
307 p_sys->i_state = STATE_NOSYNC;
311 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
313 //p_dec->b_error = true;
317 /* Copy the whole frame into the buffer. When we reach this point
318 * we already know we have enough data available. */
319 block_GetBytes( &p_sys->bytestream,
320 p_buf, __MIN( p_sys->i_frame_size, p_out_buffer->i_buffer ) );
322 /* Make sure we don't reuse the same pts twice */
323 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
324 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
326 p_sys->i_state = STATE_NOSYNC;
328 /* So p_block doesn't get re-added several times */
329 *pp_block = block_BytestreamPop( &p_sys->bytestream );
338 /*****************************************************************************
339 * CloseCommon: clean up the decoder
340 *****************************************************************************/
341 static void CloseCommon( vlc_object_t *p_this )
343 decoder_t *p_dec = (decoder_t*)p_this;
344 decoder_sys_t *p_sys = p_dec->p_sys;
346 block_BytestreamRelease( &p_sys->bytestream );
351 /*****************************************************************************
353 *****************************************************************************/
354 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
356 decoder_sys_t *p_sys = p_dec->p_sys;
359 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
361 msg_Info( p_dec, "DTS channels:%d samplerate:%d bitrate:%d",
362 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
364 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
365 date_Set( &p_sys->end_date, p_sys->i_pts );
368 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
369 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
370 /* Hack for DTS S/PDIF filter which needs to pad the DTS frames */
371 p_dec->fmt_out.audio.i_bytes_per_frame =
372 __MAX( p_sys->i_frame_size, p_sys->i_frame_length * 4 );
373 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
375 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
376 p_dec->fmt_out.audio.i_physical_channels =
377 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
379 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
381 if( p_sys->b_packetizer )
383 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
384 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
385 *pp_out_buffer = p_sout_buffer;
389 block_t *p_aout_buffer = GetAoutBuffer( p_dec );
390 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
391 *pp_out_buffer = p_aout_buffer;
397 /*****************************************************************************
399 *****************************************************************************/
400 static block_t *GetAoutBuffer( decoder_t *p_dec )
402 decoder_sys_t *p_sys = p_dec->p_sys;
405 /* Hack for DTS S/PDIF filter which needs to send 3 frames at a time
406 * (plus a few header bytes) */
407 p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length * 4 );
408 if( p_buf == NULL ) return NULL;
410 p_buf->i_nb_samples = p_sys->i_frame_length;
411 p_buf->i_buffer = p_sys->i_frame_size;
413 p_buf->i_pts = date_Get( &p_sys->end_date );
414 p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
420 /*****************************************************************************
422 *****************************************************************************/
423 static block_t *GetSoutBuffer( decoder_t *p_dec )
425 decoder_sys_t *p_sys = p_dec->p_sys;
428 p_block = block_Alloc( p_sys->i_frame_size );
429 if( p_block == NULL ) return NULL;
431 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
433 p_block->i_length = date_Increment( &p_sys->end_date,
434 p_sys->i_frame_length ) - p_block->i_pts;
439 /*****************************************************************************
440 * SyncInfo: parse DTS sync info
441 *****************************************************************************/
442 static const unsigned int ppi_dts_samplerate[] =
444 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
445 12000, 24000, 48000, 96000, 192000
448 static const unsigned int ppi_dts_bitrate[] =
450 32000, 56000, 64000, 96000, 112000, 128000,
451 192000, 224000, 256000, 320000, 384000,
452 448000, 512000, 576000, 640000, 768000,
453 896000, 1024000, 1152000, 1280000, 1344000,
454 1408000, 1411200, 1472000, 1536000, 1920000,
455 2048000, 3072000, 3840000, 1/*open*/, 2/*variable*/, 3/*lossless*/
458 static int SyncInfo( const uint8_t *p_buf,
460 unsigned int *pi_channels,
461 unsigned int *pi_channels_conf,
462 unsigned int *pi_sample_rate,
463 unsigned int *pi_bit_rate,
464 unsigned int *pi_frame_length )
466 unsigned int i_audio_mode;
468 unsigned int i_frame_size = GetSyncInfo( p_buf, pb_dts_hd,
469 pi_sample_rate, pi_bit_rate, pi_frame_length, &i_audio_mode);
471 if( *pb_dts_hd == true )
474 switch( i_audio_mode & 0xFFFF )
479 *pi_channels_conf = AOUT_CHAN_CENTER;
482 /* Dual-mono = stereo + dual-mono */
484 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
492 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
497 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
503 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
504 AOUT_CHAN_REARCENTER;
509 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
510 AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER;
515 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
516 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
521 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
522 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
529 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
530 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
531 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
536 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
537 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
538 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
545 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
546 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
547 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
548 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
552 if( i_audio_mode <= 63 )
556 *pi_channels_conf = 0;
562 if( i_audio_mode & 0x10000 )
565 *pi_channels_conf |= AOUT_CHAN_LFE;
568 if( *pi_sample_rate >= sizeof( ppi_dts_samplerate ) /
569 sizeof( ppi_dts_samplerate[0] ) )
573 *pi_sample_rate = ppi_dts_samplerate[ *pi_sample_rate ];
574 if( !*pi_sample_rate ) return 0;
576 if( *pi_bit_rate >= sizeof( ppi_dts_bitrate ) /
577 sizeof( ppi_dts_bitrate[0] ) )
581 *pi_bit_rate = ppi_dts_bitrate[ *pi_bit_rate ];
582 if( !*pi_bit_rate ) return 0;
584 *pi_frame_length = (*pi_frame_length + 1) * 32;