2 // Reference: DOCS/tech/hwac3.txt !!!!!
4 /* DTS code based on "ac3/decode_dts.c" and "ac3/conversion.c" from "ogle 0.9"
5 (see http://www.dtek.chalmers.se/~dvd/)
19 #include "ad_internal.h"
21 #include "../liba52/a52.h"
24 static int isdts
= -1;
26 static ad_info_t info
=
28 "AC3/DTS pass-through SP/DIF",
30 "Nick Kurshev/Peter Schüller",
38 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
);
39 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
);
42 static int ac3dts_fillbuff(sh_audio_t
*sh_audio
)
49 sh_audio
->a_in_buffer_len
= 0;
53 // DTS has a 10 byte header
54 while(sh_audio
->a_in_buffer_len
< 10)
56 int c
= demux_getc(sh_audio
->ds
);
59 sh_audio
->a_in_buffer
[sh_audio
->a_in_buffer_len
++] = c
;
62 length
= dts_syncinfo(sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
67 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate
, sample_rate
);
72 length
= a52_syncinfo(sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
73 if(length
>= 7 && length
<= 3840)
77 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate
, sample_rate
);
80 break; /* we're done.*/
82 /* bad file => resync*/
83 memcpy(sh_audio
->a_in_buffer
, sh_audio
->a_in_buffer
+ 1, 9);
84 --sh_audio
->a_in_buffer_len
;
86 mp_msg(MSGT_DECAUDIO
, MSGL_DBG2
, "ac3dts: %s len=%d flags=0x%X %d Hz %d bit/s\n", isdts
== 1 ? "DTS" : isdts
== 0 ? "AC3" : "unknown", length
, flags
, sample_rate
, bit_rate
);
88 sh_audio
->samplerate
= sample_rate
;
89 sh_audio
->i_bps
= bit_rate
/ 8;
90 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
+ 10, length
- 10);
91 sh_audio
->a_in_buffer_len
= length
;
93 // TODO: is DTS also checksummed?
94 if(isdts
== 0 && crc16_block(sh_audio
->a_in_buffer
+ 2, length
- 2) != 0)
95 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "a52: CRC check failed! \n");
101 static int preinit(sh_audio_t
*sh
)
103 /* Dolby AC3 audio: */
104 sh
->audio_out_minsize
= 128 * 32 * 2 * 2; // DTS seems to need more than AC3
105 sh
->audio_in_minsize
= 8192;
108 sh
->sample_format
= AFMT_AC3
;
112 static int init(sh_audio_t
*sh_audio
)
114 /* Dolby AC3 passthrough:*/
115 sample_t
*a52_samples
= a52_init(0);
116 if(a52_samples
== NULL
)
118 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "A52 init failed\n");
121 if(ac3dts_fillbuff(sh_audio
) < 0)
123 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "AC3/DTS sync failed\n");
129 static void uninit(sh_audio_t
*sh
)
133 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
137 case ADCTRL_SKIP_FRAME
:
138 ac3dts_fillbuff(sh
); break; // skip AC3 frame
141 return CONTROL_UNKNOWN
;
145 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
147 int len
= sh_audio
->a_in_buffer_len
;
150 if((len
= ac3dts_fillbuff(sh_audio
)) <= 0)
152 sh_audio
->a_in_buffer_len
= 0;
156 return decode_audio_dts(sh_audio
->a_in_buffer
, len
, buf
);
164 buf
[4] = 0x01; //(length) ? data_type : 0; /* & 0x1F; */
166 buf
[6] = (len
<< 3) & 0xFF;
167 buf
[7] = (len
>> 5) & 0xFF;
168 #ifdef WORDS_BIGENDIAN
169 memcpy(buf
+ 8, sh_audio
->a_in_buffer
, len
); // untested
171 swab(sh_audio
->a_in_buffer
, buf
+ 8, len
);
173 memset(buf
+ 8 + len
, 0, 6144 - 8 - len
);
182 static int DTS_SAMPLEFREQS
[16] =
202 static int DTS_BITRATES
[30] =
236 static int dts_decode_header(uint8_t *indata_ptr
, int *rate
, int *nblks
, int *sfreq
)
244 if(((indata_ptr
[0] << 24) | (indata_ptr
[1] << 16) | (indata_ptr
[2] << 8)
245 | (indata_ptr
[3])) != 0x7ffe8001)
248 ftype
= indata_ptr
[4] >> 7;
250 surp
= (indata_ptr
[4] >> 2) & 0x1f;
251 surp
= (surp
+ 1) % 32;
253 unknown_bit
= (indata_ptr
[4] >> 1) & 0x01;
255 *nblks
= (indata_ptr
[4] & 0x01) << 6 | (indata_ptr
[5] >> 2);
258 fsize
= (indata_ptr
[5] & 0x03) << 12 | (indata_ptr
[6] << 4) | (indata_ptr
[7] >> 4);
261 amode
= (indata_ptr
[7] & 0x0f) << 2 | (indata_ptr
[8] >> 6);
262 *sfreq
= (indata_ptr
[8] >> 2) & 0x0f;
263 *rate
= (indata_ptr
[8] & 0x03) << 3 | ((indata_ptr
[9] >> 5) & 0x07);
267 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Termination frames not handled, REPORT BUG\n");
273 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Only 48kHz supported, REPORT BUG\n");
277 if((fsize
> 8192) || (fsize
< 96))
279 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: fsize: %d invalid, REPORT BUG\n", fsize
);
290 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks
);
297 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
)
304 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
307 if(rate
>= 0 && rate
<= 29)
308 *bit_rate
= DTS_BITRATES
[rate
];
311 if(sfreq
>= 1 && sfreq
<= 15)
312 *sample_rate
= DTS_SAMPLEFREQS
[sfreq
];
320 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
)
329 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
333 burst_len
= fsize
* 8;
334 nr_samples
= nblks
* 32;
336 buf
[0] = 0x72; buf
[1] = 0xf8; /* iec 61937 */
337 buf
[2] = 0x1f; buf
[3] = 0x4e; /* syncword */
341 buf
[4] = 0x0b; /* DTS-1 (512-sample bursts) */
344 buf
[4] = 0x0c; /* DTS-2 (1024-sample bursts) */
347 buf
[4] = 0x0d; /* DTS-3 (2048-sample bursts) */
350 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: %d-sample bursts not supported\n", nr_samples
);
355 buf
[6] = (burst_len
) & 0xff;
356 buf
[7] = (burst_len
>> 8) & 0xff;
358 if(fsize
+ 8 > nr_samples
* 2 * 2)
360 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: more data than fits\n");
362 #ifdef WORDS_BIGENDIAN
363 memcpy(&buf
[8], indata_ptr
, fsize
); // untested
365 //TODO if fzise is odd, swab doesn't copy the last byte
366 swab(indata_ptr
, &buf
[8], fsize
);
368 memset(&buf
[fsize
+ 8], 0, nr_samples
* 2 * 2 - (fsize
+ 8));
370 return nr_samples
* 2 * 2;