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/)
8 #define _XOPEN_SOURCE 600
19 #include "ad_internal.h"
21 #ifdef CONFIG_LIBA52_INTERNAL
22 #include "liba52/a52.h"
24 #include <a52dec/a52.h>
28 static int isdts
= -1;
30 static ad_info_t info
=
32 "AC3/DTS pass-through S/PDIF",
34 "Nick Kurshev/Peter Schüller",
42 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
);
43 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
);
46 static int ac3dts_fillbuff(sh_audio_t
*sh_audio
)
53 sh_audio
->a_in_buffer_len
= 0;
57 // Original code DTS has a 10 bytes header.
58 // Now max 12 bytes for 14 bits DTS header.
59 while(sh_audio
->a_in_buffer_len
< 12)
61 int c
= demux_getc(sh_audio
->ds
);
64 sh_audio
->a_in_buffer
[sh_audio
->a_in_buffer_len
++] = c
;
67 if (sh_audio
->format
== 0x2001)
69 length
= dts_syncinfo(sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
74 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate
, sample_rate
);
82 length
= a52_syncinfo(sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
83 if(length
>= 7 && length
<= 3840)
87 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate
, sample_rate
);
90 break; /* we're done.*/
93 /* bad file => resync*/
94 memcpy(sh_audio
->a_in_buffer
, sh_audio
->a_in_buffer
+ 1, 11);
95 --sh_audio
->a_in_buffer_len
;
97 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
);
99 sh_audio
->samplerate
= sample_rate
;
100 sh_audio
->i_bps
= bit_rate
/ 8;
101 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
+ 12, length
- 12);
102 sh_audio
->a_in_buffer_len
= length
;
104 // TODO: is DTS also checksummed?
105 #ifdef CONFIG_LIBA52_INTERNAL
106 if(isdts
== 0 && crc16_block(sh_audio
->a_in_buffer
+ 2, length
- 2) != 0)
107 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "a52: CRC check failed! \n");
114 static int preinit(sh_audio_t
*sh
)
116 /* Dolby AC3 audio: */
117 sh
->audio_out_minsize
= 128 * 32 * 2 * 2; // DTS seems to need more than AC3
118 sh
->audio_in_minsize
= 8192;
121 sh
->sample_format
= AF_FORMAT_AC3
;
125 static int init(sh_audio_t
*sh_audio
)
127 /* Dolby AC3 passthrough:*/
128 a52_state_t
*a52_state
= a52_init(0);
129 if(a52_state
== NULL
)
131 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "A52 init failed\n");
134 if(ac3dts_fillbuff(sh_audio
) < 0)
137 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "AC3/DTS sync failed\n");
140 sh_audio
->context
= a52_state
;
144 static void uninit(sh_audio_t
*sh
)
146 a52_free(sh
->context
);
149 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
153 case ADCTRL_RESYNC_STREAM
:
154 case ADCTRL_SKIP_FRAME
:
158 return CONTROL_UNKNOWN
;
162 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
164 int len
= sh_audio
->a_in_buffer_len
;
167 if((len
= ac3dts_fillbuff(sh_audio
)) <= 0)
169 sh_audio
->a_in_buffer_len
= 0;
173 return decode_audio_dts(sh_audio
->a_in_buffer
, len
, buf
);
177 uint16_t *buf16
= (uint16_t *)buf
;
178 buf16
[0] = 0xF872; // iec 61937 syncword 1
179 buf16
[1] = 0x4E1F; // iec 61937 syncword 2
180 buf16
[2] = 0x0001; // data-type ac3
181 buf16
[2] |= (sh_audio
->a_in_buffer
[5] & 0x7) << 8; // bsmod
182 buf16
[3] = len
<< 3; // number of bits in payload
183 #ifdef WORDS_BIGENDIAN
184 memcpy(buf
+ 8, sh_audio
->a_in_buffer
, len
);
186 swab(sh_audio
->a_in_buffer
, buf
+ 8, len
);
189 buf
[8+len
] = sh_audio
->a_in_buffer
[len
-1];
193 memset(buf
+ 8 + len
, 0, 6144 - 8 - len
);
202 static const int DTS_SAMPLEFREQS
[16] =
222 static const int DTS_BITRATES
[30] =
256 static int dts_decode_header(uint8_t *indata_ptr
, int *rate
, int *nblks
, int *sfreq
)
267 unsigned int first4bytes
= indata_ptr
[0] << 24 | indata_ptr
[1] << 16
268 | indata_ptr
[2] << 8 | indata_ptr
[3];
274 /* Also make sure frame type is 1. */
275 if ((indata_ptr
[4]&0xf0) != 0xf0 || indata_ptr
[5] != 0x07)
282 /* Also make sure frame type is 1. */
283 if (indata_ptr
[4] != 0x07 || (indata_ptr
[5]&0xf0) != 0xf0)
304 /* First bit after first 32 bits:
305 Frame type ( 1: Normal frame; 0: Termination frame ) */
306 ftype
= indata_ptr
[4+le_mode
] >> 7;
310 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Termination frames not handled, REPORT BUG\n");
313 /* Next 5 bits: Surplus Sample Count V SURP 5 bits */
314 surp
= indata_ptr
[4+le_mode
] >> 2 & 0x1f;
315 /* Number of surplus samples */
316 surp
= (surp
+ 1) % 32;
318 /* One unknown bit, crc? */
319 unknown_bit
= indata_ptr
[4+le_mode
] >> 1 & 0x01;
321 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
322 *nblks
= (indata_ptr
[4+le_mode
] & 0x01) << 6 | indata_ptr
[5-le_mode
] >> 2;
323 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
324 encoded in the current frame per channel. */
327 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
328 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
329 FSIZE defines the byte size of the current audio frame. */
330 fsize
= (indata_ptr
[5-le_mode
] & 0x03) << 12 | indata_ptr
[6+le_mode
] << 4
331 | indata_ptr
[7-le_mode
] >> 4;
334 /* Audio Channel Arrangement ACC AMODE 6 bits */
335 amode
= (indata_ptr
[7-le_mode
] & 0x0f) << 2 | indata_ptr
[8+le_mode
] >> 6;
337 /* Source Sampling rate ACC SFREQ 4 bits */
338 *sfreq
= indata_ptr
[8+le_mode
] >> 2 & 0x0f;
339 /* Transmission Bit Rate ACC RATE 5 bits */
340 *rate
= (indata_ptr
[8+le_mode
] & 0x03) << 3
341 | (indata_ptr
[9-le_mode
] >> 5 & 0x07);
345 /* in the case judgement, we assure this */
348 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
350 32 bits: Sync code (28 + 4) 1th and 2th word, 4 bits in 3th word
351 1 bits: Frame type 1 bits in 3th word
352 5 bits: SURP 5 bits in 3th word
353 1 bits: crc? 1 bits in 3th word
354 7 bits: NBLKS 3 bits in 3th word, 4 bits in 4th word
355 14 bits: FSIZE 10 bits in 4th word, 4 bits in 5th word
356 in 14 bits mode, FSIZE = FSIZE*8/14*2
357 6 bits: AMODE 6 bits in 5th word
358 4 bits: SFREQ 4 bits in 5th word
359 5 bits: RATE 5 bits in 6th word
360 total bits: 75 bits */
362 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
363 *nblks
= (indata_ptr
[5-le_mode
] & 0x07) << 4
364 | (indata_ptr
[6+le_mode
] & 0x3f) >> 2;
365 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
366 encoded in the current frame per channel. */
369 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
370 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
371 FSIZE defines the byte size of the current audio frame. */
372 fsize
= (indata_ptr
[6+le_mode
] & 0x03) << 12 | indata_ptr
[7-le_mode
] << 4
373 | (indata_ptr
[8+le_mode
] & 0x3f) >> 2;
375 fsize
= fsize
* 8 / 14 * 2;
377 /* Audio Channel Arrangement ACC AMODE 6 bits */
378 amode
= (indata_ptr
[8+le_mode
] & 0x03) << 4
379 | (indata_ptr
[9-le_mode
] & 0xf0) >> 4;
381 /* Source Sampling rate ACC SFREQ 4 bits */
382 *sfreq
= indata_ptr
[9-le_mode
] & 0x0f;
383 /* Transmission Bit Rate ACC RATE 5 bits */
384 *rate
= (indata_ptr
[10+le_mode
] & 0x3f) >> 1;
389 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Only 48kHz supported, REPORT BUG\n");
393 if((fsize
> 8192) || (fsize
< 96))
395 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: fsize: %d invalid, REPORT BUG\n", fsize
);
406 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks
);
413 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
)
420 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
423 if(rate
>= 0 && rate
<= 29)
424 *bit_rate
= DTS_BITRATES
[rate
];
427 if(sfreq
>= 1 && sfreq
<= 15)
428 *sample_rate
= DTS_SAMPLEFREQS
[sfreq
];
435 static int convert_14bits_to_16bits(const unsigned char *src
,
440 uint16_t *p
= (uint16_t *)dest
;
443 if (len
<= 0) return 0;
447 v
= is_le
? src
[0] : src
[0] << 8;
449 v
= is_le
? src
[1] << 8 | src
[0] : src
[0] << 8 | src
[1];
453 buf
|= v
>> (16 - spacebits
);
458 buf
= v
<< (spacebits
- 2);
462 return (unsigned char *)p
- dest
;
465 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
)
472 int convert_16bits
= 0;
473 uint16_t *buf16
= (uint16_t *)buf
;
475 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
478 nr_samples
= nblks
* 32;
480 buf16
[0] = 0xf872; /* iec 61937 */
481 buf16
[1] = 0x4e1f; /* syncword */
485 buf16
[2] = 0x000b; /* DTS-1 (512-sample bursts) */
488 buf16
[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
491 buf16
[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
494 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: %d-sample bursts not supported\n", nr_samples
);
499 if(fsize
+ 8 > nr_samples
* 2 * 2)
501 // dts wav (14bits LE) match this condition, one way to passthrough
502 // is not add iec 61937 header, decoders will notice the dts header
503 // and identify the dts stream. Another way here is convert
504 // the stream from 14 bits to 16 bits.
505 if ((indata_ptr
[0] == 0xff || indata_ptr
[0] == 0x1f)
506 && fsize
* 14 / 16 + 8 <= nr_samples
* 2 * 2) {
507 // The input stream is 14 bits, we can shrink it to 16 bits
508 // to save space for add the 61937 header
509 fsize
= convert_14bits_to_16bits(indata_ptr
,
512 indata_ptr
[0] == 0xff /* is LE */
514 mp_msg(MSGT_DECAUDIO
, MSGL_DBG3
, "DTS: shrink 14 bits stream to "
515 "16 bits %02x%02x%02x%02x => %02x%02x%02x%02x, new size %d.\n",
516 indata_ptr
[0], indata_ptr
[1], indata_ptr
[2], indata_ptr
[3],
517 buf
[8], buf
[9], buf
[10], buf
[11], fsize
);
521 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: more data than fits\n");
524 buf16
[3] = fsize
<< 3;
526 if (!convert_16bits
) {
527 #ifdef WORDS_BIGENDIAN
529 if (indata_ptr
[0] == 0x1f || indata_ptr
[0] == 0x7f)
532 if (indata_ptr
[0] == 0xff || indata_ptr
[0] == 0xfe)
534 memcpy(&buf
[8], indata_ptr
, fsize
);
537 swab(indata_ptr
, &buf
[8], fsize
);
540 buf
[8+fsize
] = indata_ptr
[fsize
-1];
545 memset(&buf
[fsize
+ 8], 0, nr_samples
* 2 * 2 - (fsize
+ 8));
547 return nr_samples
* 2 * 2;