2 * DTS code based on "ac3/decode_dts.c" and "ac3/conversion.c" from "ogle 0.9"
3 * (see http://www.dtek.chalmers.se/~dvd/)
4 * Reference: DOCS/tech/hwac3.txt !!!!!
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #define _XOPEN_SOURCE 600
32 #include "libavutil/common.h"
33 #include "ffmpeg_files/intreadwrite.h"
35 #include "ad_internal.h"
38 static int isdts
= -1;
40 static const ad_info_t info
=
42 "AC3/DTS pass-through S/PDIF",
44 "Nick Kurshev/Peter Schüller",
52 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
);
53 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
);
56 static int a52_syncinfo (uint8_t *buf
, int *sample_rate
, int *bit_rate
)
58 static const uint16_t rate
[] = { 32, 40, 48, 56, 64, 80, 96, 112,
59 128, 160, 192, 224, 256, 320, 384, 448,
65 if (buf
[0] != 0x0b || buf
[1] != 0x77) /* syncword */
68 if (buf
[5] >= 0x60) /* bsid >= 12 */
71 half
= FFMAX(half
- 8, 0);
73 frmsizecod
= buf
[4] & 63;
76 bitrate
= rate
[frmsizecod
>> 1];
77 *bit_rate
= (bitrate
* 1000) >> half
;
79 switch (buf
[4] & 0xc0) {
81 *sample_rate
= 48000 >> half
;
84 *sample_rate
= 44100 >> half
;
85 return 2 * (320 * bitrate
/ 147 + (frmsizecod
& 1));
87 *sample_rate
= 32000 >> half
;
94 static int ac3dts_fillbuff(sh_audio_t
*sh_audio
)
101 sh_audio
->a_in_buffer_len
= 0;
105 // Original code DTS has a 10 bytes header.
106 // Now max 12 bytes for 14 bits DTS header.
107 while(sh_audio
->a_in_buffer_len
< 12)
109 int c
= demux_getc(sh_audio
->ds
);
112 sh_audio
->a_in_buffer
[sh_audio
->a_in_buffer_len
++] = c
;
115 if (sh_audio
->format
== 0x2001)
117 length
= dts_syncinfo(sh_audio
->a_in_buffer
, &flags
, &sample_rate
, &bit_rate
);
122 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate
, sample_rate
);
130 length
= a52_syncinfo(sh_audio
->a_in_buffer
, &sample_rate
, &bit_rate
);
131 if(length
>= 7 && length
<= 3840)
135 mp_msg(MSGT_DECAUDIO
, MSGL_STATUS
, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate
, sample_rate
);
138 break; /* we're done.*/
141 /* bad file => resync*/
142 memcpy(sh_audio
->a_in_buffer
, sh_audio
->a_in_buffer
+ 1, 11);
143 --sh_audio
->a_in_buffer_len
;
145 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
);
147 sh_audio
->samplerate
= sample_rate
;
148 sh_audio
->i_bps
= bit_rate
/ 8;
149 demux_read_data(sh_audio
->ds
, sh_audio
->a_in_buffer
+ 12, length
- 12);
150 sh_audio
->a_in_buffer_len
= length
;
156 static int preinit(sh_audio_t
*sh
)
158 /* Dolby AC3 audio: */
159 sh
->audio_out_minsize
= 128 * 32 * 2 * 2; // DTS seems to need more than AC3
160 sh
->audio_in_minsize
= 8192;
163 sh
->sample_format
= AF_FORMAT_AC3_BE
;
164 // HACK for DTS where useless swapping can't easily be removed
165 if (sh
->format
== 0x2001)
166 sh
->sample_format
= AF_FORMAT_AC3_NE
;
170 static int init(sh_audio_t
*sh_audio
)
172 /* Dolby AC3 passthrough:*/
173 if(ac3dts_fillbuff(sh_audio
) < 0)
175 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "AC3/DTS sync failed\n");
181 static void uninit(sh_audio_t
*sh
)
185 static int control(sh_audio_t
*sh
,int cmd
,void* arg
, ...)
189 case ADCTRL_RESYNC_STREAM
:
190 case ADCTRL_SKIP_FRAME
:
194 return CONTROL_UNKNOWN
;
198 static int decode_audio(sh_audio_t
*sh_audio
,unsigned char *buf
,int minlen
,int maxlen
)
200 int len
= sh_audio
->a_in_buffer_len
;
203 if((len
= ac3dts_fillbuff(sh_audio
)) <= 0)
205 sh_audio
->a_in_buffer_len
= 0;
209 return decode_audio_dts(sh_audio
->a_in_buffer
, len
, buf
);
213 AV_WB16(buf
, 0xF872); // iec 61937 syncword 1
214 AV_WB16(buf
+ 2, 0x4E1F); // iec 61937 syncword 2
215 buf
[4] = sh_audio
->a_in_buffer
[5] & 0x7; // bsmod
216 buf
[5] = 0x01; // data-type ac3
217 AV_WB16(buf
+ 6, len
<< 3); // number of bits in payload
218 memcpy(buf
+ 8, sh_audio
->a_in_buffer
, len
);
219 memset(buf
+ 8 + len
, 0, 6144 - 8 - len
);
228 static const int DTS_SAMPLEFREQS
[16] =
248 static const int DTS_BITRATES
[30] =
282 static int dts_decode_header(uint8_t *indata_ptr
, int *rate
, int *nblks
, int *sfreq
)
293 unsigned int first4bytes
= indata_ptr
[0] << 24 | indata_ptr
[1] << 16
294 | indata_ptr
[2] << 8 | indata_ptr
[3];
300 /* Also make sure frame type is 1. */
301 if ((indata_ptr
[4]&0xf0) != 0xf0 || indata_ptr
[5] != 0x07)
308 /* Also make sure frame type is 1. */
309 if (indata_ptr
[4] != 0x07 || (indata_ptr
[5]&0xf0) != 0xf0)
330 /* First bit after first 32 bits:
331 Frame type ( 1: Normal frame; 0: Termination frame ) */
332 ftype
= indata_ptr
[4+le_mode
] >> 7;
336 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Termination frames not handled, REPORT BUG\n");
339 /* Next 5 bits: Surplus Sample Count V SURP 5 bits */
340 surp
= indata_ptr
[4+le_mode
] >> 2 & 0x1f;
341 /* Number of surplus samples */
342 surp
= (surp
+ 1) % 32;
344 /* One unknown bit, crc? */
345 unknown_bit
= indata_ptr
[4+le_mode
] >> 1 & 0x01;
347 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
348 *nblks
= (indata_ptr
[4+le_mode
] & 0x01) << 6 | indata_ptr
[5-le_mode
] >> 2;
349 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
350 encoded in the current frame per channel. */
353 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
354 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
355 FSIZE defines the byte size of the current audio frame. */
356 fsize
= (indata_ptr
[5-le_mode
] & 0x03) << 12 | indata_ptr
[6+le_mode
] << 4
357 | indata_ptr
[7-le_mode
] >> 4;
360 /* Audio Channel Arrangement ACC AMODE 6 bits */
361 amode
= (indata_ptr
[7-le_mode
] & 0x0f) << 2 | indata_ptr
[8+le_mode
] >> 6;
363 /* Source Sampling rate ACC SFREQ 4 bits */
364 *sfreq
= indata_ptr
[8+le_mode
] >> 2 & 0x0f;
365 /* Transmission Bit Rate ACC RATE 5 bits */
366 *rate
= (indata_ptr
[8+le_mode
] & 0x03) << 3
367 | (indata_ptr
[9-le_mode
] >> 5 & 0x07);
371 /* in the case judgement, we assure this */
374 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
376 32 bits: Sync code (28 + 4) 1th and 2th word, 4 bits in 3th word
377 1 bits: Frame type 1 bits in 3th word
378 5 bits: SURP 5 bits in 3th word
379 1 bits: crc? 1 bits in 3th word
380 7 bits: NBLKS 3 bits in 3th word, 4 bits in 4th word
381 14 bits: FSIZE 10 bits in 4th word, 4 bits in 5th word
382 in 14 bits mode, FSIZE = FSIZE*8/14*2
383 6 bits: AMODE 6 bits in 5th word
384 4 bits: SFREQ 4 bits in 5th word
385 5 bits: RATE 5 bits in 6th word
386 total bits: 75 bits */
388 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
389 *nblks
= (indata_ptr
[5-le_mode
] & 0x07) << 4
390 | (indata_ptr
[6+le_mode
] & 0x3f) >> 2;
391 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
392 encoded in the current frame per channel. */
395 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
396 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
397 FSIZE defines the byte size of the current audio frame. */
398 fsize
= (indata_ptr
[6+le_mode
] & 0x03) << 12 | indata_ptr
[7-le_mode
] << 4
399 | (indata_ptr
[8+le_mode
] & 0x3f) >> 2;
401 fsize
= fsize
* 8 / 14 * 2;
403 /* Audio Channel Arrangement ACC AMODE 6 bits */
404 amode
= (indata_ptr
[8+le_mode
] & 0x03) << 4
405 | (indata_ptr
[9-le_mode
] & 0xf0) >> 4;
407 /* Source Sampling rate ACC SFREQ 4 bits */
408 *sfreq
= indata_ptr
[9-le_mode
] & 0x0f;
409 /* Transmission Bit Rate ACC RATE 5 bits */
410 *rate
= (indata_ptr
[10+le_mode
] & 0x3f) >> 1;
415 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: Only 48kHz supported, REPORT BUG\n");
419 if((fsize
> 8192) || (fsize
< 96))
421 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: fsize: %d invalid, REPORT BUG\n", fsize
);
432 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks
);
439 static int dts_syncinfo(uint8_t *indata_ptr
, int *flags
, int *sample_rate
, int *bit_rate
)
446 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
449 if(rate
>= 0 && rate
<= 29)
450 *bit_rate
= DTS_BITRATES
[rate
];
453 if(sfreq
>= 1 && sfreq
<= 15)
454 *sample_rate
= DTS_SAMPLEFREQS
[sfreq
];
461 static int convert_14bits_to_16bits(const unsigned char *src
,
466 uint16_t *p
= (uint16_t *)dest
;
469 if (len
<= 0) return 0;
473 v
= is_le
? src
[0] : src
[0] << 8;
475 v
= is_le
? src
[1] << 8 | src
[0] : src
[0] << 8 | src
[1];
479 buf
|= v
>> (16 - spacebits
);
484 buf
= v
<< (spacebits
- 2);
488 return (unsigned char *)p
- dest
;
491 static int decode_audio_dts(unsigned char *indata_ptr
, int len
, unsigned char *buf
)
498 int convert_16bits
= 0;
499 uint16_t *buf16
= (uint16_t *)buf
;
501 fsize
= dts_decode_header(indata_ptr
, &rate
, &nblks
, &sfreq
);
504 nr_samples
= nblks
* 32;
506 buf16
[0] = 0xf872; /* iec 61937 */
507 buf16
[1] = 0x4e1f; /* syncword */
511 buf16
[2] = 0x000b; /* DTS-1 (512-sample bursts) */
514 buf16
[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
517 buf16
[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
520 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: %d-sample bursts not supported\n", nr_samples
);
525 if(fsize
+ 8 > nr_samples
* 2 * 2)
527 // dts wav (14bits LE) match this condition, one way to passthrough
528 // is not add iec 61937 header, decoders will notice the dts header
529 // and identify the dts stream. Another way here is convert
530 // the stream from 14 bits to 16 bits.
531 if ((indata_ptr
[0] == 0xff || indata_ptr
[0] == 0x1f)
532 && fsize
* 14 / 16 + 8 <= nr_samples
* 2 * 2) {
533 // The input stream is 14 bits, we can shrink it to 16 bits
534 // to save space for add the 61937 header
535 fsize
= convert_14bits_to_16bits(indata_ptr
,
538 indata_ptr
[0] == 0xff /* is LE */
540 mp_msg(MSGT_DECAUDIO
, MSGL_DBG3
, "DTS: shrink 14 bits stream to "
541 "16 bits %02x%02x%02x%02x => %02x%02x%02x%02x, new size %d.\n",
542 indata_ptr
[0], indata_ptr
[1], indata_ptr
[2], indata_ptr
[3],
543 buf
[8], buf
[9], buf
[10], buf
[11], fsize
);
547 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "DTS: more data than fits\n");
550 buf16
[3] = fsize
<< 3;
552 if (!convert_16bits
) {
555 if (indata_ptr
[0] == 0x1f || indata_ptr
[0] == 0x7f)
558 if (indata_ptr
[0] == 0xff || indata_ptr
[0] == 0xfe)
560 memcpy(&buf
[8], indata_ptr
, fsize
);
563 swab(indata_ptr
, &buf
[8], fsize
);
566 buf
[8+fsize
] = indata_ptr
[fsize
-1];
571 memset(&buf
[fsize
+ 8], 0, nr_samples
* 2 * 2 - (fsize
+ 8));
573 return nr_samples
* 2 * 2;