1 /*****************************************************************************
2 * dts.c: parse DTS audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: dts.c,v 1.16 2004/02/06 18:15:44 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc/decoder.h>
31 #include "vlc_block_helper.h"
33 #define DTS_HEADER_SIZE 14
35 /*****************************************************************************
36 * decoder_sys_t : decoder descriptor
37 *****************************************************************************/
41 vlc_bool_t b_packetizer;
48 block_bytestream_t bytestream;
53 audio_date_t end_date;
57 int i_frame_size, i_bit_rate;
58 unsigned int i_frame_length, i_rate, i_channels, i_channels_conf;
71 /****************************************************************************
73 ****************************************************************************/
74 static int OpenDecoder ( vlc_object_t * );
75 static int OpenPacketizer( vlc_object_t * );
76 static void CloseDecoder ( vlc_object_t * );
77 static void *DecodeBlock ( decoder_t *, block_t ** );
79 static inline int SyncCode( const uint8_t * );
80 static int SyncInfo ( const uint8_t *, unsigned int *, unsigned int *,
81 unsigned int *, unsigned int *, unsigned int * );
83 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
84 static aout_buffer_t *GetAoutBuffer( decoder_t * );
85 static block_t *GetSoutBuffer( decoder_t * );
87 /*****************************************************************************
89 *****************************************************************************/
91 set_description( _("DTS parser") );
92 set_capability( "decoder", 100 );
93 set_callbacks( OpenDecoder, CloseDecoder );
96 set_description( _("DTS audio packetizer") );
97 set_capability( "packetizer", 10 );
98 set_callbacks( OpenPacketizer, NULL );
101 /*****************************************************************************
102 * OpenDecoder: probe the decoder and return score
103 *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
106 decoder_t *p_dec = (decoder_t*)p_this;
107 decoder_sys_t *p_sys;
109 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ')
110 && p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s','b') )
115 /* Allocate the memory needed to store the decoder's structure */
116 if( ( p_dec->p_sys = p_sys =
117 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
119 msg_Err( p_dec, "out of memory" );
124 p_sys->b_packetizer = VLC_FALSE;
125 p_sys->i_state = STATE_NOSYNC;
126 aout_DateSet( &p_sys->end_date, 0 );
128 p_sys->bytestream = block_BytestreamInit( p_dec );
130 /* Set output properties */
131 p_dec->fmt_out.i_cat = AUDIO_ES;
132 p_dec->fmt_out.i_codec = VLC_FOURCC('d','t','s',' ');
135 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
137 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
143 static int OpenPacketizer( vlc_object_t *p_this )
145 decoder_t *p_dec = (decoder_t*)p_this;
147 int i_ret = OpenDecoder( p_this );
149 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
154 /****************************************************************************
155 * DecodeBlock: the whole thing
156 ****************************************************************************
157 * This function is called just after the thread is launched.
158 ****************************************************************************/
159 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
161 decoder_sys_t *p_sys = p_dec->p_sys;
162 uint8_t p_header[DTS_HEADER_SIZE];
166 if( !pp_block || !*pp_block ) return NULL;
168 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
170 /* We've just started the stream, wait for the first PTS. */
171 block_Release( *pp_block );
175 if( (*pp_block)->b_discontinuity )
177 p_sys->i_state = STATE_NOSYNC;
180 block_BytestreamPush( &p_sys->bytestream, *pp_block );
184 switch( p_sys->i_state )
187 /* Look for sync code - should be 0x7ffe8001 */
188 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
191 if( SyncCode( p_header ) == VLC_SUCCESS )
193 p_sys->i_state = STATE_SYNC;
196 block_SkipByte( &p_sys->bytestream );
198 if( p_sys->i_state != STATE_SYNC )
200 block_BytestreamFlush( &p_sys->bytestream );
207 /* New frame, set the Presentation Time Stamp */
208 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
209 if( p_sys->i_pts != 0 &&
210 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
212 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
214 p_sys->i_state = STATE_HEADER;
217 /* Get DTS frame header (DTS_HEADER_SIZE bytes) */
218 if( block_PeekBytes( &p_sys->bytestream, p_header,
219 DTS_HEADER_SIZE ) != VLC_SUCCESS )
225 /* Check if frame is valid and get frame info */
226 p_sys->i_frame_size = SyncInfo( p_header,
228 &p_sys->i_channels_conf,
231 &p_sys->i_frame_length );
232 if( !p_sys->i_frame_size )
234 msg_Dbg( p_dec, "emulated sync word" );
235 block_SkipByte( &p_sys->bytestream );
236 p_sys->i_state = STATE_NOSYNC;
239 p_sys->i_state = STATE_NEXT_SYNC;
241 case STATE_NEXT_SYNC:
242 /* TODO: If pp_block == NULL, flush the buffer without checking the
245 /* Check if next expected frame contains the sync word */
246 if( block_PeekOffsetBytes( &p_sys->bytestream,
247 p_sys->i_frame_size, p_header, 6 )
254 if( SyncCode( p_header ) != VLC_SUCCESS )
256 msg_Dbg( p_dec, "emulated sync word "
257 "(no sync on following frame): %2.2x%2.2x%2.2x%2.2x",
258 (int)p_header[0], (int)p_header[1],
259 (int)p_header[2], (int)p_header[3] );
260 p_sys->i_state = STATE_NOSYNC;
261 block_SkipByte( &p_sys->bytestream );
264 p_sys->i_state = STATE_SEND_DATA;
268 /* Make sure we have enough data.
269 * (Not useful if we went through NEXT_SYNC) */
270 if( block_WaitBytes( &p_sys->bytestream,
271 p_sys->i_frame_size ) != VLC_SUCCESS )
276 p_sys->i_state = STATE_SEND_DATA;
278 case STATE_SEND_DATA:
279 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
281 //p_dec->b_error = VLC_TRUE;
285 /* Copy the whole frame into the buffer. When we reach this point
286 * we already know we have enough data available. */
287 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
289 /* Make sure we don't reuse the same pts twice */
290 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
291 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
293 p_sys->i_state = STATE_NOSYNC;
295 /* So p_block doesn't get re-added several times */
296 *pp_block = block_BytestreamPop( &p_sys->bytestream );
305 /*****************************************************************************
306 * CloseDecoder: clean up the decoder
307 *****************************************************************************/
308 static void CloseDecoder( vlc_object_t *p_this )
310 decoder_t *p_dec = (decoder_t*)p_this;
311 decoder_sys_t *p_sys = p_dec->p_sys;
313 block_BytestreamRelease( &p_sys->bytestream );
318 /*****************************************************************************
320 *****************************************************************************/
321 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
323 decoder_sys_t *p_sys = p_dec->p_sys;
326 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
328 msg_Info( p_dec, "DTS channels:%d samplerate:%d bitrate:%d",
329 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
331 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
332 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
335 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
336 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
337 p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_frame_size;
338 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
340 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
341 p_dec->fmt_out.audio.i_physical_channels =
342 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
344 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
346 if( p_sys->b_packetizer )
348 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
349 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
350 *pp_out_buffer = p_sout_buffer;
354 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
355 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
356 *pp_out_buffer = p_aout_buffer;
362 /*****************************************************************************
364 *****************************************************************************/
365 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
367 decoder_sys_t *p_sys = p_dec->p_sys;
368 aout_buffer_t *p_buf;
370 /* Hack for DTS S/PDIF filter which needs to send 3 frames at a time
371 * (plus a few header bytes) */
372 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length * 4 );
373 if( p_buf == NULL ) return NULL;
374 p_buf->i_nb_samples = p_sys->i_frame_length;
375 p_buf->i_nb_bytes = p_sys->i_frame_size;
377 p_buf->start_date = aout_DateGet( &p_sys->end_date );
379 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
384 /*****************************************************************************
386 *****************************************************************************/
387 static block_t *GetSoutBuffer( decoder_t *p_dec )
389 decoder_sys_t *p_sys = p_dec->p_sys;
392 p_block = block_New( p_dec, p_sys->i_frame_size );
393 if( p_block == NULL ) return NULL;
395 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
397 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
398 p_sys->i_frame_length ) - p_block->i_pts;
403 /*****************************************************************************
404 * SyncInfo: parse DTS sync info
405 *****************************************************************************/
406 static const unsigned int ppi_dts_samplerate[] =
408 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
409 12000, 24000, 48000, 96000, 192000
412 static const unsigned int ppi_dts_bitrate[] =
414 32000, 56000, 64000, 96000, 112000, 128000,
415 192000, 224000, 256000, 320000, 384000,
416 448000, 512000, 576000, 640000, 768000,
417 896000, 1024000, 1152000, 1280000, 1344000,
418 1408000, 1411200, 1472000, 1536000, 1920000,
419 2048000, 3072000, 3840000, 1/*open*/, 2/*variable*/, 3/*lossless*/
422 static int SyncInfo16be( const uint8_t *p_buf,
423 unsigned int *pi_audio_mode,
424 unsigned int *pi_sample_rate,
425 unsigned int *pi_bit_rate,
426 unsigned int *pi_frame_length )
428 unsigned int i_frame_size;
431 *pi_frame_length = (p_buf[4] & 0x01) << 6 | (p_buf[5] >> 2);
432 i_frame_size = (p_buf[5] & 0x03) << 12 | (p_buf[6] << 4) |
435 *pi_audio_mode = (p_buf[7] & 0x0f) << 2 | (p_buf[8] >> 6);
436 *pi_sample_rate = (p_buf[8] >> 2) & 0x0f;
437 *pi_bit_rate = (p_buf[8] & 0x03) << 3 | ((p_buf[9] >> 5) & 0x07);
439 i_lfe = (p_buf[10] >> 1) & 0x03;
440 if( i_lfe ) *pi_audio_mode |= 0x10000;
442 return i_frame_size + 1;
445 static void BufLeToBe( uint8_t *p_out, const uint8_t *p_in, int i_in )
449 for( i = 0; i < i_in/2; i++ )
451 p_out[i*2] = p_in[i*2+1];
452 p_out[i*2+1] = p_in[i*2];
456 static int Buf14To16( uint8_t *p_out, const uint8_t *p_in, int i_in, int i_le )
458 unsigned char tmp, cur = 0;
459 int bits_in, bits_out = 0;
462 for( i = 0; i < i_in; i++ )
471 tmp = p_in[i+i_le] & 0x3F;
477 int need = __MIN( 8 - bits_out, bits_in );
479 cur |= ( tmp >> (bits_in - need) );
480 tmp <<= (8 - bits_in + need);
481 tmp >>= (8 - bits_in + need);
502 static inline int SyncCode( const uint8_t *p_buf )
504 /* 14 bits, little endian version of the bitstream */
505 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
506 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
507 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
511 /* 14 bits, big endian version of the bitstream */
512 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
513 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
514 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
518 /* 16 bits, big endian version of the bitstream */
519 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
520 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
524 /* 16 bits, little endian version of the bitstream */
525 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
526 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
530 else return VLC_EGENERIC;
533 static int SyncInfo( const uint8_t *p_buf,
534 unsigned int *pi_channels,
535 unsigned int *pi_channels_conf,
536 unsigned int *pi_sample_rate,
537 unsigned int *pi_bit_rate,
538 unsigned int *pi_frame_length )
540 unsigned int i_audio_mode;
541 unsigned int i_frame_size;
543 /* 14 bits, little endian version of the bitstream */
544 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
545 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
546 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
548 uint8_t conv_buf[DTS_HEADER_SIZE];
549 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 1 );
550 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
551 pi_bit_rate, pi_frame_length );
552 i_frame_size = i_frame_size * 8 / 14 * 2;
554 /* 14 bits, big endian version of the bitstream */
555 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
556 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
557 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
559 uint8_t conv_buf[DTS_HEADER_SIZE];
560 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 0 );
561 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
562 pi_bit_rate, pi_frame_length );
563 i_frame_size = i_frame_size * 8 / 14 * 2;
565 /* 16 bits, big endian version of the bitstream */
566 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
567 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
569 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
570 pi_bit_rate, pi_frame_length );
572 /* 16 bits, little endian version of the bitstream */
573 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
574 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
576 uint8_t conv_buf[DTS_HEADER_SIZE];
577 BufLeToBe( conv_buf, p_buf, DTS_HEADER_SIZE );
578 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
579 pi_bit_rate, pi_frame_length );
583 switch( i_audio_mode & 0xFFFF )
587 *pi_channels_conf = AOUT_CHAN_CENTER;
590 /* Dual-mono = stereo + dual-mono */
591 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
599 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
604 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
610 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
611 AOUT_CHAN_REARCENTER;
616 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
617 AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER;
622 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
623 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
628 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
629 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
636 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
637 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
638 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
643 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
644 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
645 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
652 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
653 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
654 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
655 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
659 if( i_audio_mode <= 63 )
663 *pi_channels_conf = 0;
669 if( i_audio_mode & 0x10000 )
672 *pi_channels_conf |= AOUT_CHAN_LFE;
675 if( *pi_sample_rate >= sizeof( ppi_dts_samplerate ) /
676 sizeof( ppi_dts_samplerate[0] ) )
680 *pi_sample_rate = ppi_dts_samplerate[ *pi_sample_rate ];
681 if( !*pi_sample_rate ) return 0;
683 if( *pi_bit_rate >= sizeof( ppi_dts_bitrate ) /
684 sizeof( ppi_dts_bitrate[0] ) )
688 *pi_bit_rate = ppi_dts_bitrate[ *pi_bit_rate ];
689 if( !*pi_bit_rate ) return 0;
691 *pi_frame_length = (*pi_frame_length + 1) * 32;