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 *****************************************************************************/
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 unsigned int i_bit_rate;
58 unsigned int i_frame_size;
59 unsigned int i_frame_length;
61 unsigned int i_channels;
62 unsigned int i_channels_conf;
75 /****************************************************************************
77 ****************************************************************************/
78 static int OpenDecoder ( vlc_object_t * );
79 static int OpenPacketizer( vlc_object_t * );
80 static void CloseDecoder ( vlc_object_t * );
81 static void *DecodeBlock ( decoder_t *, block_t ** );
83 static inline int SyncCode( const uint8_t * );
84 static int SyncInfo ( const uint8_t *, unsigned int *, unsigned int *,
85 unsigned int *, unsigned int *, unsigned int * );
87 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
88 static aout_buffer_t *GetAoutBuffer( decoder_t * );
89 static block_t *GetSoutBuffer( decoder_t * );
91 /*****************************************************************************
93 *****************************************************************************/
95 set_description( _("DTS parser") );
96 set_capability( "decoder", 100 );
97 set_callbacks( OpenDecoder, CloseDecoder );
100 set_description( _("DTS audio packetizer") );
101 set_capability( "packetizer", 10 );
102 set_callbacks( OpenPacketizer, NULL );
105 /*****************************************************************************
106 * OpenDecoder: probe the decoder and return score
107 *****************************************************************************/
108 static int OpenDecoder( vlc_object_t *p_this )
110 decoder_t *p_dec = (decoder_t*)p_this;
111 decoder_sys_t *p_sys;
113 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s',' ')
114 && p_dec->fmt_in.i_codec != VLC_FOURCC('d','t','s','b') )
119 /* Allocate the memory needed to store the decoder's structure */
120 if( ( p_dec->p_sys = p_sys =
121 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
123 msg_Err( p_dec, "out of memory" );
128 p_sys->b_packetizer = VLC_FALSE;
129 p_sys->i_state = STATE_NOSYNC;
130 aout_DateSet( &p_sys->end_date, 0 );
132 p_sys->bytestream = block_BytestreamInit( p_dec );
134 /* Set output properties */
135 p_dec->fmt_out.i_cat = AUDIO_ES;
136 p_dec->fmt_out.i_codec = VLC_FOURCC('d','t','s',' ');
137 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
140 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
142 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
148 static int OpenPacketizer( vlc_object_t *p_this )
150 decoder_t *p_dec = (decoder_t*)p_this;
152 int i_ret = OpenDecoder( p_this );
154 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
159 /****************************************************************************
160 * DecodeBlock: the whole thing
161 ****************************************************************************
162 * This function is called just after the thread is launched.
163 ****************************************************************************/
164 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
166 decoder_sys_t *p_sys = p_dec->p_sys;
167 uint8_t p_header[DTS_HEADER_SIZE];
171 if( !pp_block || !*pp_block ) return NULL;
173 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
175 /* We've just started the stream, wait for the first PTS. */
176 block_Release( *pp_block );
180 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
182 p_sys->i_state = STATE_NOSYNC;
185 block_BytestreamPush( &p_sys->bytestream, *pp_block );
189 switch( p_sys->i_state )
192 /* Look for sync code - should be 0x7ffe8001 */
193 while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
196 if( SyncCode( p_header ) == VLC_SUCCESS )
198 p_sys->i_state = STATE_SYNC;
201 block_SkipByte( &p_sys->bytestream );
203 if( p_sys->i_state != STATE_SYNC )
205 block_BytestreamFlush( &p_sys->bytestream );
212 /* New frame, set the Presentation Time Stamp */
213 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
214 if( p_sys->i_pts != 0 &&
215 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
217 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
219 p_sys->i_state = STATE_HEADER;
222 /* Get DTS frame header (DTS_HEADER_SIZE bytes) */
223 if( block_PeekBytes( &p_sys->bytestream, p_header,
224 DTS_HEADER_SIZE ) != VLC_SUCCESS )
230 /* Check if frame is valid and get frame info */
231 p_sys->i_frame_size = SyncInfo( p_header,
233 &p_sys->i_channels_conf,
236 &p_sys->i_frame_length );
237 if( !p_sys->i_frame_size )
239 msg_Dbg( p_dec, "emulated sync word" );
240 block_SkipByte( &p_sys->bytestream );
241 p_sys->i_state = STATE_NOSYNC;
244 p_sys->i_state = STATE_NEXT_SYNC;
246 case STATE_NEXT_SYNC:
247 /* TODO: If pp_block == NULL, flush the buffer without checking the
250 /* Check if next expected frame contains the sync word */
251 if( block_PeekOffsetBytes( &p_sys->bytestream,
252 p_sys->i_frame_size, p_header, 6 )
259 if( SyncCode( p_header ) != VLC_SUCCESS )
261 msg_Dbg( p_dec, "emulated sync word "
262 "(no sync on following frame): %2.2x%2.2x%2.2x%2.2x",
263 (int)p_header[0], (int)p_header[1],
264 (int)p_header[2], (int)p_header[3] );
265 p_sys->i_state = STATE_NOSYNC;
266 block_SkipByte( &p_sys->bytestream );
269 p_sys->i_state = STATE_SEND_DATA;
273 /* Make sure we have enough data.
274 * (Not useful if we went through NEXT_SYNC) */
275 if( block_WaitBytes( &p_sys->bytestream,
276 p_sys->i_frame_size ) != VLC_SUCCESS )
281 p_sys->i_state = STATE_SEND_DATA;
283 case STATE_SEND_DATA:
284 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
286 //p_dec->b_error = VLC_TRUE;
290 /* Copy the whole frame into the buffer. When we reach this point
291 * we already know we have enough data available. */
292 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
294 /* Make sure we don't reuse the same pts twice */
295 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
296 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
298 p_sys->i_state = STATE_NOSYNC;
300 /* So p_block doesn't get re-added several times */
301 *pp_block = block_BytestreamPop( &p_sys->bytestream );
310 /*****************************************************************************
311 * CloseDecoder: clean up the decoder
312 *****************************************************************************/
313 static void CloseDecoder( vlc_object_t *p_this )
315 decoder_t *p_dec = (decoder_t*)p_this;
316 decoder_sys_t *p_sys = p_dec->p_sys;
318 block_BytestreamRelease( &p_sys->bytestream );
323 /*****************************************************************************
325 *****************************************************************************/
326 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
328 decoder_sys_t *p_sys = p_dec->p_sys;
331 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
333 msg_Info( p_dec, "DTS channels:%d samplerate:%d bitrate:%d",
334 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
336 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
337 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
340 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
341 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
342 /* Hack for DTS S/PDIF filter which needs to pad the DTS frames */
343 p_dec->fmt_out.audio.i_bytes_per_frame =
344 __MAX( p_sys->i_frame_size, p_sys->i_frame_length * 4 );
345 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
347 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
348 p_dec->fmt_out.audio.i_physical_channels =
349 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
351 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
353 if( p_sys->b_packetizer )
355 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
356 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
357 *pp_out_buffer = p_sout_buffer;
361 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
362 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
363 *pp_out_buffer = p_aout_buffer;
369 /*****************************************************************************
371 *****************************************************************************/
372 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
374 decoder_sys_t *p_sys = p_dec->p_sys;
375 aout_buffer_t *p_buf;
377 /* Hack for DTS S/PDIF filter which needs to send 3 frames at a time
378 * (plus a few header bytes) */
379 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length * 4 );
380 if( p_buf == NULL ) return NULL;
381 p_buf->i_nb_samples = p_sys->i_frame_length;
382 p_buf->i_nb_bytes = p_sys->i_frame_size;
384 p_buf->start_date = aout_DateGet( &p_sys->end_date );
386 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
391 /*****************************************************************************
393 *****************************************************************************/
394 static block_t *GetSoutBuffer( decoder_t *p_dec )
396 decoder_sys_t *p_sys = p_dec->p_sys;
399 p_block = block_New( p_dec, p_sys->i_frame_size );
400 if( p_block == NULL ) return NULL;
402 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
404 p_block->i_length = aout_DateIncrement( &p_sys->end_date,
405 p_sys->i_frame_length ) - p_block->i_pts;
410 /*****************************************************************************
411 * SyncInfo: parse DTS sync info
412 *****************************************************************************/
413 static const unsigned int ppi_dts_samplerate[] =
415 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
416 12000, 24000, 48000, 96000, 192000
419 static const unsigned int ppi_dts_bitrate[] =
421 32000, 56000, 64000, 96000, 112000, 128000,
422 192000, 224000, 256000, 320000, 384000,
423 448000, 512000, 576000, 640000, 768000,
424 896000, 1024000, 1152000, 1280000, 1344000,
425 1408000, 1411200, 1472000, 1536000, 1920000,
426 2048000, 3072000, 3840000, 1/*open*/, 2/*variable*/, 3/*lossless*/
429 static int SyncInfo16be( const uint8_t *p_buf,
430 unsigned int *pi_audio_mode,
431 unsigned int *pi_sample_rate,
432 unsigned int *pi_bit_rate,
433 unsigned int *pi_frame_length )
435 unsigned int i_frame_size;
438 *pi_frame_length = (p_buf[4] & 0x01) << 6 | (p_buf[5] >> 2);
439 i_frame_size = (p_buf[5] & 0x03) << 12 | (p_buf[6] << 4) |
442 *pi_audio_mode = (p_buf[7] & 0x0f) << 2 | (p_buf[8] >> 6);
443 *pi_sample_rate = (p_buf[8] >> 2) & 0x0f;
444 *pi_bit_rate = (p_buf[8] & 0x03) << 3 | ((p_buf[9] >> 5) & 0x07);
446 i_lfe = (p_buf[10] >> 1) & 0x03;
447 if( i_lfe ) *pi_audio_mode |= 0x10000;
449 return i_frame_size + 1;
452 static void BufLeToBe( uint8_t *p_out, const uint8_t *p_in, int i_in )
456 for( i = 0; i < i_in/2; i++ )
458 p_out[i*2] = p_in[i*2+1];
459 p_out[i*2+1] = p_in[i*2];
463 static int Buf14To16( uint8_t *p_out, const uint8_t *p_in, int i_in, int i_le )
465 unsigned char tmp, cur = 0;
466 int bits_in, bits_out = 0;
469 for( i = 0; i < i_in; i++ )
478 tmp = p_in[i+i_le] & 0x3F;
484 int need = __MIN( 8 - bits_out, bits_in );
486 cur |= ( tmp >> (bits_in - need) );
487 tmp <<= (8 - bits_in + need);
488 tmp >>= (8 - bits_in + need);
509 static inline int SyncCode( const uint8_t *p_buf )
511 /* 14 bits, little endian version of the bitstream */
512 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
513 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
514 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
518 /* 14 bits, big endian version of the bitstream */
519 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
520 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
521 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
525 /* 16 bits, big endian version of the bitstream */
526 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
527 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
531 /* 16 bits, little endian version of the bitstream */
532 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
533 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
537 else return VLC_EGENERIC;
540 static int SyncInfo( const uint8_t *p_buf,
541 unsigned int *pi_channels,
542 unsigned int *pi_channels_conf,
543 unsigned int *pi_sample_rate,
544 unsigned int *pi_bit_rate,
545 unsigned int *pi_frame_length )
547 unsigned int i_audio_mode;
548 unsigned int i_frame_size;
550 /* 14 bits, little endian version of the bitstream */
551 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
552 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
553 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
555 uint8_t conv_buf[DTS_HEADER_SIZE];
556 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 1 );
557 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
558 pi_bit_rate, pi_frame_length );
559 i_frame_size = i_frame_size * 8 / 14 * 2;
561 /* 14 bits, big endian version of the bitstream */
562 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
563 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
564 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
566 uint8_t conv_buf[DTS_HEADER_SIZE];
567 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 0 );
568 i_frame_size = SyncInfo16be( conv_buf, &i_audio_mode, pi_sample_rate,
569 pi_bit_rate, pi_frame_length );
570 i_frame_size = i_frame_size * 8 / 14 * 2;
572 /* 16 bits, big endian version of the bitstream */
573 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
574 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
576 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
577 pi_bit_rate, pi_frame_length );
579 /* 16 bits, little endian version of the bitstream */
580 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
581 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
583 uint8_t conv_buf[DTS_HEADER_SIZE];
584 BufLeToBe( conv_buf, p_buf, DTS_HEADER_SIZE );
585 i_frame_size = SyncInfo16be( p_buf, &i_audio_mode, pi_sample_rate,
586 pi_bit_rate, pi_frame_length );
590 switch( i_audio_mode & 0xFFFF )
594 *pi_channels_conf = AOUT_CHAN_CENTER;
597 /* Dual-mono = stereo + dual-mono */
598 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
606 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
611 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
617 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
618 AOUT_CHAN_REARCENTER;
623 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
624 AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER;
629 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
630 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
635 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
636 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
643 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
644 AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
645 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
650 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
651 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
652 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
659 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
660 AOUT_CHAN_CENTER | AOUT_CHAN_MIDDLELEFT |
661 AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT |
662 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
666 if( i_audio_mode <= 63 )
670 *pi_channels_conf = 0;
676 if( i_audio_mode & 0x10000 )
679 *pi_channels_conf |= AOUT_CHAN_LFE;
682 if( *pi_sample_rate >= sizeof( ppi_dts_samplerate ) /
683 sizeof( ppi_dts_samplerate[0] ) )
687 *pi_sample_rate = ppi_dts_samplerate[ *pi_sample_rate ];
688 if( !*pi_sample_rate ) return 0;
690 if( *pi_bit_rate >= sizeof( ppi_dts_bitrate ) /
691 sizeof( ppi_dts_bitrate[0] ) )
695 *pi_bit_rate = ppi_dts_bitrate[ *pi_bit_rate ];
696 if( !*pi_bit_rate ) return 0;
698 *pi_frame_length = (*pi_frame_length + 1) * 32;