Merge branch 'ordered_chapters'
[mplayer/glamo.git] / libmpcodecs / ad_hwac3.c
blob6750ae0f0a35d838f11c3c014b06450108beacb6
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/)
6 */
8 #define _XOPEN_SOURCE 600
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
14 #include "config.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
17 #include "mpbswap.h"
19 #include "ad_internal.h"
21 #ifdef CONFIG_LIBA52_INTERNAL
22 #include "liba52/a52.h"
23 #else
24 #include <a52dec/a52.h>
25 #endif
28 static int isdts = -1;
30 static const ad_info_t info =
32 "AC3/DTS pass-through S/PDIF",
33 "hwac3",
34 "Nick Kurshev/Peter Schüller",
35 "???",
39 LIBAD_EXTERN(hwac3)
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)
48 int length = 0;
49 int flags = 0;
50 int sample_rate = 0;
51 int bit_rate = 0;
53 sh_audio->a_in_buffer_len = 0;
54 /* sync frame:*/
55 while(1)
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);
62 if(c<0)
63 return -1; /* EOF*/
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);
70 if(length >= 12)
72 if(isdts != 1)
74 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
75 isdts = 1;
77 break;
80 else
82 length = a52_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
83 if(length >= 7 && length <= 3840)
85 if(isdts != 0)
87 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
88 isdts = 0;
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");
108 #endif
110 return length;
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;
119 sh->channels = 2;
120 sh->samplesize = 2;
121 sh->sample_format = AF_FORMAT_AC3;
122 return 1;
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");
132 return 0;
134 if(ac3dts_fillbuff(sh_audio) < 0)
136 a52_free(a52_state);
137 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "AC3/DTS sync failed\n");
138 return 0;
140 sh_audio->context = a52_state;
141 return 1;
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, ...)
151 switch(cmd)
153 case ADCTRL_RESYNC_STREAM:
154 case ADCTRL_SKIP_FRAME:
155 ac3dts_fillbuff(sh);
156 return CONTROL_TRUE;
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;
166 if(len <= 0)
167 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
168 return len; /*EOF*/
169 sh_audio->a_in_buffer_len = 0;
171 if(isdts == 1)
173 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
175 else if(isdts == 0)
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);
185 #else
186 swab(sh_audio->a_in_buffer, buf + 8, len);
187 if (len & 1) {
188 buf[8+len-1] = 0;
189 buf[8+len] = sh_audio->a_in_buffer[len-1];
190 len++;
192 #endif
193 memset(buf + 8 + len, 0, 6144 - 8 - len);
195 return 6144;
197 else
198 return -1;
202 static const int DTS_SAMPLEFREQS[16] =
205 8000,
206 16000,
207 32000,
208 64000,
209 128000,
210 11025,
211 22050,
212 44100,
213 88200,
214 176400,
215 12000,
216 24000,
217 48000,
218 96000,
219 192000
222 static const int DTS_BITRATES[30] =
224 32000,
225 56000,
226 64000,
227 96000,
228 112000,
229 128000,
230 192000,
231 224000,
232 256000,
233 320000,
234 384000,
235 448000,
236 512000,
237 576000,
238 640000,
239 768000,
240 896000,
241 1024000,
242 1152000,
243 1280000,
244 1344000,
245 1408000,
246 1411200,
247 1472000,
248 1536000,
249 1920000,
250 2048000,
251 3072000,
252 3840000,
253 4096000
256 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
258 int ftype;
259 int surp;
260 int unknown_bit;
261 int fsize;
262 int amode;
264 int word_mode;
265 int le_mode;
267 unsigned int first4bytes = indata_ptr[0] << 24 | indata_ptr[1] << 16
268 | indata_ptr[2] << 8 | indata_ptr[3];
270 switch(first4bytes)
272 /* 14 bits LE */
273 case 0xff1f00e8:
274 /* Also make sure frame type is 1. */
275 if ((indata_ptr[4]&0xf0) != 0xf0 || indata_ptr[5] != 0x07)
276 return -1;
277 word_mode = 0;
278 le_mode = 1;
279 break;
280 /* 14 bits BE */
281 case 0x1fffe800:
282 /* Also make sure frame type is 1. */
283 if (indata_ptr[4] != 0x07 || (indata_ptr[5]&0xf0) != 0xf0)
284 return -1;
285 word_mode = 0;
286 le_mode = 0;
287 break;
288 /* 16 bits LE */
289 case 0xfe7f0180:
290 word_mode = 1;
291 le_mode = 1;
292 break;
293 /* 16 bits BE */
294 case 0x7ffe8001:
295 word_mode = 1;
296 le_mode = 0;
297 break;
298 default:
299 return -1;
302 if(word_mode)
304 /* First bit after first 32 bits:
305 Frame type ( 1: Normal frame; 0: Termination frame ) */
306 ftype = indata_ptr[4+le_mode] >> 7;
308 if(ftype != 1)
310 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
311 return -1;
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. */
325 ++(*nblks);
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;
332 ++fsize;
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);
343 else
345 /* in the case judgement, we assure this */
346 ftype = 1;
347 surp = 0;
348 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
349 /* Bits usage:
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. */
367 ++(*nblks);
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;
374 ++fsize;
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;
386 #if 0
387 if(*sfreq != 13)
389 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
390 return -1;
392 #endif
393 if((fsize > 8192) || (fsize < 96))
395 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
396 return -1;
399 if(*nblks != 8 &&
400 *nblks != 16 &&
401 *nblks != 32 &&
402 *nblks != 64 &&
403 *nblks != 128 &&
404 ftype == 1)
406 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
407 return -1;
410 return fsize;
413 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
415 int nblks;
416 int fsize;
417 int rate;
418 int sfreq;
420 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
421 if(fsize >= 0)
423 if(rate >= 0 && rate <= 29)
424 *bit_rate = DTS_BITRATES[rate];
425 else
426 *bit_rate = 0;
427 if(sfreq >= 1 && sfreq <= 15)
428 *sample_rate = DTS_SAMPLEFREQS[sfreq];
429 else
430 *sample_rate = 0;
432 return fsize;
435 static int convert_14bits_to_16bits(const unsigned char *src,
436 unsigned char *dest,
437 int len,
438 int is_le)
440 uint16_t *p = (uint16_t *)dest;
441 uint16_t buf = 0;
442 int spacebits = 16;
443 if (len <= 0) return 0;
444 while (len > 0) {
445 uint16_t v;
446 if (len == 1)
447 v = is_le ? src[0] : src[0] << 8;
448 else
449 v = is_le ? src[1] << 8 | src[0] : src[0] << 8 | src[1];
450 v <<= 2;
451 src += 2;
452 len -= 2;
453 buf |= v >> (16 - spacebits);
454 spacebits -= 14;
455 if (spacebits < 0) {
456 *p++ = buf;
457 spacebits += 16;
458 buf = v << (spacebits - 2);
461 *p++ = buf;
462 return (unsigned char *)p - dest;
465 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
467 int nblks;
468 int fsize;
469 int rate;
470 int sfreq;
471 int nr_samples;
472 int convert_16bits = 0;
473 uint16_t *buf16 = (uint16_t *)buf;
475 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
476 if(fsize < 0)
477 return -1;
478 nr_samples = nblks * 32;
480 buf16[0] = 0xf872; /* iec 61937 */
481 buf16[1] = 0x4e1f; /* syncword */
482 switch(nr_samples)
484 case 512:
485 buf16[2] = 0x000b; /* DTS-1 (512-sample bursts) */
486 break;
487 case 1024:
488 buf16[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
489 break;
490 case 2048:
491 buf16[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
492 break;
493 default:
494 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
495 buf16[2] = 0x0000;
496 break;
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,
510 &buf[8],
511 fsize,
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);
518 convert_16bits = 1;
520 else
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
528 /* BE stream */
529 if (indata_ptr[0] == 0x1f || indata_ptr[0] == 0x7f)
530 #else
531 /* LE stream */
532 if (indata_ptr[0] == 0xff || indata_ptr[0] == 0xfe)
533 #endif
534 memcpy(&buf[8], indata_ptr, fsize);
535 else
537 swab(indata_ptr, &buf[8], fsize);
538 if (fsize & 1) {
539 buf[8+fsize-1] = 0;
540 buf[8+fsize] = indata_ptr[fsize-1];
541 fsize++;
545 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
547 return nr_samples * 2 * 2;