1 /*****************************************************************************
2 * dts.c: parse DTS audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2003-2005 the VideoLAN team
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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 #include <vlc_block_helper.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenDecoder ( vlc_object_t * );
43 static int OpenPacketizer( vlc_object_t * );
44 static void CloseCommon ( vlc_object_t * );
47 set_description( N_("DTS parser") )
48 set_capability( "decoder", 100 )
49 set_callbacks( OpenDecoder, CloseCommon )
52 set_description( N_("DTS audio packetizer") )
53 set_capability( "packetizer", 10 )
54 set_callbacks( OpenPacketizer, CloseCommon )
57 /*****************************************************************************
58 * decoder_sys_t : decoder descriptor
59 *****************************************************************************/
70 block_bytestream_t bytestream;
75 audio_date_t end_date;
79 bool b_dts_hd; /* Is the current frame a DTS HD one */
80 unsigned int i_bit_rate;
81 unsigned int i_frame_size;
82 unsigned int i_frame_length;
84 unsigned int i_channels;
85 unsigned int i_channels_conf;
98 #define DTS_HEADER_SIZE 14
100 /****************************************************************************
102 ****************************************************************************/
103 static int OpenCommon( vlc_object_t *, bool b_packetizer );
104 static void *DecodeBlock( decoder_t *, block_t ** );
106 static inline int SyncCode( const uint8_t * );
107 static int SyncInfo( const uint8_t *, bool *, unsigned int *, unsigned int *,
108 unsigned int *, unsigned int *, unsigned int * );
110 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
111 static aout_buffer_t *GetAoutBuffer( decoder_t * );
112 static block_t *GetSoutBuffer( decoder_t * );
114 /*****************************************************************************
115 * OpenDecoder: probe the decoder
116 *****************************************************************************/
117 static int OpenDecoder( vlc_object_t *p_this )
119 /* HACK: Don't use this codec if we don't have an dts audio filter */
120 if( !module_exists( "dtstofloat32" ) )
123 return OpenCommon( p_this, false );
126 /*****************************************************************************
127 * OpenPacketizer: probe the packetizer
128 *****************************************************************************/
129 static int OpenPacketizer( vlc_object_t *p_this )
131 return OpenCommon( p_this, true );
134 /*****************************************************************************
136 *****************************************************************************/
137 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
139 decoder_t *p_dec = (decoder_t*)p_this;
140 decoder_sys_t *p_sys;
142 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ') &&
143 p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s','b') )
148 /* Allocate the memory needed to store the decoder's structure */
149 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
153 p_sys->b_packetizer = b_packetizer;
154 p_sys->i_state = STATE_NOSYNC;
155 aout_DateSet( &p_sys->end_date, 0 );
156 p_sys->b_dts_hd = false;
158 p_sys->bytestream = block_BytestreamInit();
160 /* Set output properties */
161 p_dec->fmt_out.i_cat = AUDIO_ES;
162 p_dec->fmt_out.i_codec = VLC_FOURCC('d','t','s',' ');
163 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
166 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
168 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
174 /****************************************************************************
175 * DecodeBlock: the whole thing
176 ****************************************************************************/
177 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
179 decoder_sys_t *p_sys = p_dec->p_sys;
180 uint8_t p_header[DTS_HEADER_SIZE];
184 if( !pp_block || !*pp_block )
187 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
189 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
191 p_sys->i_state = STATE_NOSYNC;
192 block_BytestreamEmpty( &p_sys->bytestream );
194 aout_DateSet( &p_sys->end_date, 0 );
195 block_Release( *pp_block );
199 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
201 /* We've just started the stream, wait for the first PTS. */
202 block_Release( *pp_block );
206 block_BytestreamPush( &p_sys->bytestream, *pp_block );
210 switch( p_sys->i_state )
213 /* Look for sync code - should be 0x7ffe8001 */
214 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
217 if( SyncCode( p_header ) == VLC_SUCCESS )
219 p_sys->i_state = STATE_SYNC;
222 block_SkipByte( &p_sys->bytestream );
224 if( p_sys->i_state != STATE_SYNC )
226 block_BytestreamFlush( &p_sys->bytestream );
233 /* New frame, set the Presentation Time Stamp */
234 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
235 if( p_sys->i_pts != 0 &&
236 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
238 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
240 p_sys->i_state = STATE_HEADER;
243 /* Get DTS frame header (DTS_HEADER_SIZE bytes) */
244 if( block_PeekBytes( &p_sys->bytestream, p_header,
245 DTS_HEADER_SIZE ) != VLC_SUCCESS )
251 /* Check if frame is valid and get frame info */
252 p_sys->i_frame_size = SyncInfo( p_header,
255 &p_sys->i_channels_conf,
258 &p_sys->i_frame_length );
259 if( !p_sys->i_frame_size )
261 msg_Dbg( p_dec, "emulated sync word" );
262 block_SkipByte( &p_sys->bytestream );
263 p_sys->i_state = STATE_NOSYNC;
266 p_sys->i_state = STATE_NEXT_SYNC;
268 case STATE_NEXT_SYNC:
269 /* TODO: If pp_block == NULL, flush the buffer without checking the
272 /* Check if next expected frame contains the sync word */
273 if( block_PeekOffsetBytes( &p_sys->bytestream,
274 p_sys->i_frame_size, p_header, 6 )
281 if( SyncCode( p_header ) != VLC_SUCCESS )
283 msg_Dbg( p_dec, "emulated sync word "
284 "(no sync on following frame): %2.2x%2.2x%2.2x%2.2x",
285 (int)p_header[0], (int)p_header[1],
286 (int)p_header[2], (int)p_header[3] );
287 p_sys->i_state = STATE_NOSYNC;
288 block_SkipByte( &p_sys->bytestream );
291 p_sys->i_state = STATE_SEND_DATA;
295 /* Make sure we have enough data.
296 * (Not useful if we went through NEXT_SYNC) */
297 if( block_WaitBytes( &p_sys->bytestream,
298 p_sys->i_frame_size ) != VLC_SUCCESS )
303 p_sys->i_state = STATE_SEND_DATA;
305 case STATE_SEND_DATA:
306 if( p_sys->b_dts_hd )
309 block_SkipBytes( &p_sys->bytestream, p_sys->i_frame_size );
310 p_sys->i_state = STATE_NOSYNC;
314 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
316 //p_dec->b_error = true;
320 /* Copy the whole frame into the buffer. When we reach this point
321 * we already know we have enough data available. */
322 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
324 /* Make sure we don't reuse the same pts twice */
325 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
326 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
328 p_sys->i_state = STATE_NOSYNC;
330 /* So p_block doesn't get re-added several times */
331 *pp_block = block_BytestreamPop( &p_sys->bytestream );
340 /*****************************************************************************
341 * CloseCommon: clean up the decoder
342 *****************************************************************************/
343 static void CloseCommon( vlc_object_t *p_this )
345 decoder_t *p_dec = (decoder_t*)p_this;
346 decoder_sys_t *p_sys = p_dec->p_sys;
348 block_BytestreamRelease( &p_sys->bytestream );
353 /*****************************************************************************
355 *****************************************************************************/
356 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
358 decoder_sys_t *p_sys = p_dec->p_sys;
361 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
363 msg_Info( p_dec, "DTS channels:%d samplerate:%d bitrate:%d",
364 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
366 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
367 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
370 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
371 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
372 /* Hack for DTS S/PDIF filter which needs to pad the DTS frames */
373 p_dec->fmt_out.audio.i_bytes_per_frame =
374 __MAX( p_sys->i_frame_size, p_sys->i_frame_length * 4 );
375 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
377 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
378 p_dec->fmt_out.audio.i_physical_channels =
379 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
381 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
383 if( p_sys->b_packetizer )
385 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
386 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
387 *pp_out_buffer = p_sout_buffer;
391 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
392 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
393 *pp_out_buffer = p_aout_buffer;
399 /*****************************************************************************
401 *****************************************************************************/
402 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
404 decoder_sys_t *p_sys = p_dec->p_sys;
405 aout_buffer_t *p_buf;
407 /* Hack for DTS S/PDIF filter which needs to send 3 frames at a time
408 * (plus a few header bytes) */
409 p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length * 4 );
410 if( p_buf == NULL ) return NULL;
411 p_buf->i_nb_samples = p_sys->i_frame_length;
412 p_buf->i_nb_bytes = p_sys->i_frame_size;
414 p_buf->start_date = aout_DateGet( &p_sys->end_date );
416 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
421 /*****************************************************************************
423 *****************************************************************************/
424 static block_t *GetSoutBuffer( decoder_t *p_dec )
426 decoder_sys_t *p_sys = p_dec->p_sys;
429 p_block = block_New( p_dec, p_sys->i_frame_size );
430 if( p_block == NULL ) return NULL;
432 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
434 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
435 p_sys->i_frame_length ) - p_block->i_pts;
440 /*****************************************************************************
441 * SyncInfo: parse DTS sync info
442 *****************************************************************************/
443 static const unsigned int ppi_dts_samplerate[] =
445 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
446 12000, 24000, 48000, 96000, 192000
449 static const unsigned int ppi_dts_bitrate[] =
451 32000, 56000, 64000, 96000, 112000, 128000,
452 192000, 224000, 256000, 320000, 384000,
453 448000, 512000, 576000, 640000, 768000,
454 896000, 1024000, 1152000, 1280000, 1344000,
455 1408000, 1411200, 1472000, 1536000, 1920000,
456 2048000, 3072000, 3840000, 1/*open*/, 2/*variable*/, 3/*lossless*/
459 static int SyncInfo16be( const uint8_t *p_buf,
460 unsigned int *pi_audio_mode,
461 unsigned int *pi_sample_rate,
462 unsigned int *pi_bit_rate,
463 unsigned int *pi_frame_length )
465 unsigned int i_frame_size;
468 *pi_frame_length = (p_buf[4] & 0x01) << 6 | (p_buf[5] >> 2);
469 i_frame_size = (p_buf[5] & 0x03) << 12 | (p_buf[6] << 4) |
472 *pi_audio_mode = (p_buf[7] & 0x0f) << 2 | (p_buf[8] >> 6);
473 *pi_sample_rate = (p_buf[8] >> 2) & 0x0f;
474 *pi_bit_rate = (p_buf[8] & 0x03) << 3 | ((p_buf[9] >> 5) & 0x07);
476 i_lfe = (p_buf[10] >> 1) & 0x03;
477 if( i_lfe ) *pi_audio_mode |= 0x10000;
479 return i_frame_size + 1;
482 static void BufLeToBe( uint8_t *p_out, const uint8_t *p_in, int i_in )
486 for( i = 0; i < i_in/2; i++ )
488 p_out[i*2] = p_in[i*2+1];
489 p_out[i*2+1] = p_in[i*2];
493 static int Buf14To16( uint8_t *p_out, const uint8_t *p_in, int i_in, int i_le )
495 unsigned char tmp, cur = 0;
496 int bits_in, bits_out = 0;
499 for( i = 0; i < i_in; i++ )
508 tmp = p_in[i+i_le] & 0x3F;
514 int need = __MIN( 8 - bits_out, bits_in );
516 cur |= ( tmp >> (bits_in - need) );
517 tmp <<= (8 - bits_in + need);
518 tmp >>= (8 - bits_in + need);
539 static inline int SyncCode( const uint8_t *p_buf )
541 /* 14 bits, little endian version of the bitstream */
542 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
543 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
544 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
548 /* 14 bits, big endian version of the bitstream */
549 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
550 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
551 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
555 /* 16 bits, big endian version of the bitstream */
556 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
557 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
561 /* 16 bits, little endian version of the bitstream */
562 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
563 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
568 else if( p_buf[0] == 0x64 && p_buf[1] == 0x58 &&
569 p_buf[2] == 0x20 && p_buf[3] == 0x25 )
577 static int SyncInfo( const uint8_t *p_buf,
579 unsigned int *pi_channels,
580 unsigned int *pi_channels_conf,
581 unsigned int *pi_sample_rate,
582 unsigned int *pi_bit_rate,
583 unsigned int *pi_frame_length )
585 unsigned int i_audio_mode;
586 unsigned int i_frame_size;
588 /* 14 bits, little endian version of the bitstream */
589 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
590 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
591 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
593 uint8_t conv_buf[DTS_HEADER_SIZE];
594 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 1 );
595 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
596 pi_bit_rate, pi_frame_length );
597 i_frame_size = i_frame_size * 8 / 14 * 2;
599 /* 14 bits, big endian version of the bitstream */
600 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
601 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
602 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
604 uint8_t conv_buf[DTS_HEADER_SIZE];
605 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 0 );
606 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
607 pi_bit_rate, pi_frame_length );
608 i_frame_size = i_frame_size * 8 / 14 * 2;
610 /* 16 bits, big endian version of the bitstream */
611 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
612 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
614 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
615 pi_bit_rate, pi_frame_length );
617 /* 16 bits, little endian version of the bitstream */
618 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
619 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
621 uint8_t conv_buf[DTS_HEADER_SIZE];
622 BufLeToBe( conv_buf, p_buf, DTS_HEADER_SIZE );
623 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
624 pi_bit_rate, pi_frame_length );
627 else if( p_buf[0] == 0x64 && p_buf[1] == 0x58 &&
628 p_buf[2] == 0x20 && p_buf[3] == 0x25 )
632 bs_init( &s, &p_buf[4], DTS_HEADER_SIZE - 4 );
634 bs_skip( &s, 8 + 2 );
639 i_dts_hd_size = bs_read( &s, 20 ) + 1;
644 i_dts_hd_size = bs_read( &s, 16 ) + 1;
646 //uint16_t s0 = bs_read( &s, 16 );
647 //uint16_t s1 = bs_read( &s, 16 );
648 //fprintf( stderr, "DTS HD=%d : %x %x\n", i_dts_hd_size, s0, s1 );
651 /* As we ignore the stream, do not modify those variables:
653 *pi_channels_conf = ;
658 return i_dts_hd_size;
663 switch( i_audio_mode & 0xFFFF )
667 *pi_channels_conf = AOUT_CHAN_CENTER;
670 /* Dual-mono = stereo + dual-mono */
671 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
679 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
684 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
690 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
691 AOUT_CHAN_REARCENTER;
696 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
697 AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER;
702 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
703 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
708 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
709 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
716 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
717 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
718 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
723 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
724 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
725 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
732 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
733 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
734 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
735 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
739 if( i_audio_mode <= 63 )
743 *pi_channels_conf = 0;
749 if( i_audio_mode & 0x10000 )
752 *pi_channels_conf |= AOUT_CHAN_LFE;
755 if( *pi_sample_rate >= sizeof( ppi_dts_samplerate ) /
756 sizeof( ppi_dts_samplerate[0] ) )
760 *pi_sample_rate = ppi_dts_samplerate[ *pi_sample_rate ];
761 if( !*pi_sample_rate ) return 0;
763 if( *pi_bit_rate >= sizeof( ppi_dts_bitrate ) /
764 sizeof( ppi_dts_bitrate[0] ) )
768 *pi_bit_rate = ppi_dts_bitrate[ *pi_bit_rate ];
769 if( !*pi_bit_rate ) return 0;
771 *pi_frame_length = (*pi_frame_length + 1) * 32;