1 /*****************************************************************************
2 * dts_header.c: parse DTS audio headers info
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
32 #include "dts_header.h"
37 static int SyncInfo16be( const uint8_t *p_buf,
38 unsigned int *pi_audio_mode,
39 unsigned int *pi_sample_rate,
40 unsigned int *pi_bit_rate,
41 unsigned int *pi_frame_length )
43 unsigned int frame_size;
46 *pi_frame_length = (p_buf[4] & 0x01) << 6 | (p_buf[5] >> 2);
47 frame_size = (p_buf[5] & 0x03) << 12 | (p_buf[6] << 4) | (p_buf[7] >> 4);
49 *pi_audio_mode = (p_buf[7] & 0x0f) << 2 | (p_buf[8] >> 6);
50 *pi_sample_rate = (p_buf[8] >> 2) & 0x0f;
51 *pi_bit_rate = (p_buf[8] & 0x03) << 3 | ((p_buf[9] >> 5) & 0x07);
53 i_lfe = (p_buf[10] >> 1) & 0x03;
54 if( i_lfe ) *pi_audio_mode |= 0x10000;
56 return frame_size + 1;
59 static void BufLeToBe( uint8_t *p_out, const uint8_t *p_in, int i_in )
63 for( i = 0; i < i_in/2; i++ )
65 p_out[i*2] = p_in[i*2+1];
66 p_out[i*2+1] = p_in[i*2];
70 static int Buf14To16( uint8_t *p_out, const uint8_t *p_in, int i_in, int i_le )
72 unsigned char tmp, cur = 0;
73 int bits_in, bits_out = 0;
76 for( i = 0; i < i_in; i++ )
85 tmp = p_in[i+i_le] & 0x3F;
91 int need = __MIN( 8 - bits_out, bits_in );
93 cur |= ( tmp >> (bits_in - need) );
94 tmp <<= (8 - bits_in + need);
95 tmp >>= (8 - bits_in + need);
116 int SyncCode( const uint8_t *p_buf )
118 /* 14 bits, little endian version of the bitstream */
119 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
120 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
121 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
125 /* 14 bits, big endian version of the bitstream */
126 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
127 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
128 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
132 /* 16 bits, big endian version of the bitstream */
133 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
134 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
138 /* 16 bits, little endian version of the bitstream */
139 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
140 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
145 else if( p_buf[0] == 0x64 && p_buf[1] == 0x58 &&
146 p_buf[2] == 0x20 && p_buf[3] == 0x25 )
154 int GetSyncInfo( const uint8_t *p_buf,
156 unsigned int *pi_sample_rate,
157 unsigned int *pi_bit_rate,
158 unsigned int *pi_frame_length,
159 unsigned int *pi_audio_mode )
161 unsigned int i_frame_size;
163 /* 14 bits, little endian version of the bitstream */
164 if( p_buf[0] == 0xff && p_buf[1] == 0x1f &&
165 p_buf[2] == 0x00 && p_buf[3] == 0xe8 &&
166 (p_buf[4] & 0xf0) == 0xf0 && p_buf[5] == 0x07 )
168 uint8_t conv_buf[DTS_HEADER_SIZE];
169 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 1 );
170 i_frame_size = SyncInfo16be( conv_buf, pi_audio_mode, pi_sample_rate,
171 pi_bit_rate, pi_frame_length );
172 i_frame_size = i_frame_size * 8 / 14 * 2;
174 /* 14 bits, big endian version of the bitstream */
175 else if( p_buf[0] == 0x1f && p_buf[1] == 0xff &&
176 p_buf[2] == 0xe8 && p_buf[3] == 0x00 &&
177 p_buf[4] == 0x07 && (p_buf[5] & 0xf0) == 0xf0 )
179 uint8_t conv_buf[DTS_HEADER_SIZE];
180 Buf14To16( conv_buf, p_buf, DTS_HEADER_SIZE, 0 );
181 i_frame_size = SyncInfo16be( conv_buf, pi_audio_mode, pi_sample_rate,
182 pi_bit_rate, pi_frame_length );
183 i_frame_size = i_frame_size * 8 / 14 * 2;
185 /* 16 bits, big endian version of the bitstream */
186 else if( p_buf[0] == 0x7f && p_buf[1] == 0xfe &&
187 p_buf[2] == 0x80 && p_buf[3] == 0x01 )
189 i_frame_size = SyncInfo16be( p_buf, pi_audio_mode, pi_sample_rate,
190 pi_bit_rate, pi_frame_length );
192 /* 16 bits, little endian version of the bitstream */
193 else if( p_buf[0] == 0xfe && p_buf[1] == 0x7f &&
194 p_buf[2] == 0x01 && p_buf[3] == 0x80 )
196 uint8_t conv_buf[DTS_HEADER_SIZE];
197 BufLeToBe( conv_buf, p_buf, DTS_HEADER_SIZE );
198 i_frame_size = SyncInfo16be( p_buf, pi_audio_mode, pi_sample_rate,
199 pi_bit_rate, pi_frame_length );
202 else if( p_buf[0] == 0x64 && p_buf[1] == 0x58 &&
203 p_buf[2] == 0x20 && p_buf[3] == 0x25 )
207 bs_init( &s, &p_buf[4], DTS_HEADER_SIZE - 4 );
209 bs_skip( &s, 8 + 2 );
214 i_dts_hd_size = bs_read( &s, 20 ) + 1;
219 i_dts_hd_size = bs_read( &s, 16 ) + 1;
221 //uint16_t s0 = bs_read( &s, 16 );
222 //uint16_t s1 = bs_read( &s, 16 );
223 //fprintf( stderr, "DTS HD=%d : %x %x\n", i_dts_hd_size, s0, s1 );
226 /* As we ignore the stream, do not modify those variables:
228 *pi_channels_conf = ;
233 return i_dts_hd_size;