Makefile: Rebuild FFmpeg libraries when .asm and .o files change
[mplayer/glamo.git] / libmpcodecs / ad_hwac3.c
blob95f072212c1548d4cbed90750cbb6853f270cf9b
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 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "config.h"
14 #include "mp_msg.h"
15 #include "help_mp.h"
16 #include "mpbswap.h"
18 #include "ad_internal.h"
20 #ifdef CONFIG_LIBA52_INTERNAL
21 #include "liba52/a52.h"
22 #else
23 #include <a52dec/a52.h>
24 #endif
27 static int isdts = -1;
29 static const ad_info_t info =
31 "AC3/DTS pass-through S/PDIF",
32 "hwac3",
33 "Nick Kurshev/Peter Schüller",
34 "???",
38 LIBAD_EXTERN(hwac3)
41 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate);
42 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf);
45 static int ac3dts_fillbuff(sh_audio_t *sh_audio)
47 int length = 0;
48 int flags = 0;
49 int sample_rate = 0;
50 int bit_rate = 0;
52 sh_audio->a_in_buffer_len = 0;
53 /* sync frame:*/
54 while(1)
56 // Original code DTS has a 10 bytes header.
57 // Now max 12 bytes for 14 bits DTS header.
58 while(sh_audio->a_in_buffer_len < 12)
60 int c = demux_getc(sh_audio->ds);
61 if(c<0)
62 return -1; /* EOF*/
63 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++] = c;
66 if (sh_audio->format == 0x2001)
68 length = dts_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
69 if(length >= 12)
71 if(isdts != 1)
73 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
74 isdts = 1;
76 break;
79 else
81 length = a52_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
82 if(length >= 7 && length <= 3840)
84 if(isdts != 0)
86 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
87 isdts = 0;
89 break; /* we're done.*/
92 /* bad file => resync*/
93 memcpy(sh_audio->a_in_buffer, sh_audio->a_in_buffer + 1, 11);
94 --sh_audio->a_in_buffer_len;
96 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);
98 sh_audio->samplerate = sample_rate;
99 sh_audio->i_bps = bit_rate / 8;
100 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer + 12, length - 12);
101 sh_audio->a_in_buffer_len = length;
103 // TODO: is DTS also checksummed?
104 #ifdef CONFIG_LIBA52_INTERNAL
105 if(isdts == 0 && crc16_block(sh_audio->a_in_buffer + 2, length - 2) != 0)
106 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "a52: CRC check failed! \n");
107 #endif
109 return length;
113 static int preinit(sh_audio_t *sh)
115 /* Dolby AC3 audio: */
116 sh->audio_out_minsize = 128 * 32 * 2 * 2; // DTS seems to need more than AC3
117 sh->audio_in_minsize = 8192;
118 sh->channels = 2;
119 sh->samplesize = 2;
120 sh->sample_format = AF_FORMAT_AC3;
121 return 1;
124 static int init(sh_audio_t *sh_audio)
126 /* Dolby AC3 passthrough:*/
127 a52_state_t *a52_state = a52_init(0);
128 if(a52_state == NULL)
130 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "A52 init failed\n");
131 return 0;
133 if(ac3dts_fillbuff(sh_audio) < 0)
135 a52_free(a52_state);
136 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "AC3/DTS sync failed\n");
137 return 0;
139 sh_audio->context = a52_state;
140 return 1;
143 static void uninit(sh_audio_t *sh)
145 a52_free(sh->context);
148 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
150 switch(cmd)
152 case ADCTRL_RESYNC_STREAM:
153 case ADCTRL_SKIP_FRAME:
154 ac3dts_fillbuff(sh);
155 return CONTROL_TRUE;
157 return CONTROL_UNKNOWN;
161 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
163 int len = sh_audio->a_in_buffer_len;
165 if(len <= 0)
166 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
167 return len; /*EOF*/
168 sh_audio->a_in_buffer_len = 0;
170 if(isdts == 1)
172 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
174 else if(isdts == 0)
176 uint16_t *buf16 = (uint16_t *)buf;
177 buf16[0] = 0xF872; // iec 61937 syncword 1
178 buf16[1] = 0x4E1F; // iec 61937 syncword 2
179 buf16[2] = 0x0001; // data-type ac3
180 buf16[2] |= (sh_audio->a_in_buffer[5] & 0x7) << 8; // bsmod
181 buf16[3] = len << 3; // number of bits in payload
182 #ifdef WORDS_BIGENDIAN
183 memcpy(buf + 8, sh_audio->a_in_buffer, len);
184 #else
185 swab(sh_audio->a_in_buffer, buf + 8, len);
186 if (len & 1) {
187 buf[8+len-1] = 0;
188 buf[8+len] = sh_audio->a_in_buffer[len-1];
189 len++;
191 #endif
192 memset(buf + 8 + len, 0, 6144 - 8 - len);
194 return 6144;
196 else
197 return -1;
201 static const int DTS_SAMPLEFREQS[16] =
204 8000,
205 16000,
206 32000,
207 64000,
208 128000,
209 11025,
210 22050,
211 44100,
212 88200,
213 176400,
214 12000,
215 24000,
216 48000,
217 96000,
218 192000
221 static const int DTS_BITRATES[30] =
223 32000,
224 56000,
225 64000,
226 96000,
227 112000,
228 128000,
229 192000,
230 224000,
231 256000,
232 320000,
233 384000,
234 448000,
235 512000,
236 576000,
237 640000,
238 768000,
239 896000,
240 1024000,
241 1152000,
242 1280000,
243 1344000,
244 1408000,
245 1411200,
246 1472000,
247 1536000,
248 1920000,
249 2048000,
250 3072000,
251 3840000,
252 4096000
255 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
257 int ftype;
258 int surp;
259 int unknown_bit;
260 int fsize;
261 int amode;
263 int word_mode;
264 int le_mode;
266 unsigned int first4bytes = indata_ptr[0] << 24 | indata_ptr[1] << 16
267 | indata_ptr[2] << 8 | indata_ptr[3];
269 switch(first4bytes)
271 /* 14 bits LE */
272 case 0xff1f00e8:
273 /* Also make sure frame type is 1. */
274 if ((indata_ptr[4]&0xf0) != 0xf0 || indata_ptr[5] != 0x07)
275 return -1;
276 word_mode = 0;
277 le_mode = 1;
278 break;
279 /* 14 bits BE */
280 case 0x1fffe800:
281 /* Also make sure frame type is 1. */
282 if (indata_ptr[4] != 0x07 || (indata_ptr[5]&0xf0) != 0xf0)
283 return -1;
284 word_mode = 0;
285 le_mode = 0;
286 break;
287 /* 16 bits LE */
288 case 0xfe7f0180:
289 word_mode = 1;
290 le_mode = 1;
291 break;
292 /* 16 bits BE */
293 case 0x7ffe8001:
294 word_mode = 1;
295 le_mode = 0;
296 break;
297 default:
298 return -1;
301 if(word_mode)
303 /* First bit after first 32 bits:
304 Frame type ( 1: Normal frame; 0: Termination frame ) */
305 ftype = indata_ptr[4+le_mode] >> 7;
307 if(ftype != 1)
309 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
310 return -1;
312 /* Next 5 bits: Surplus Sample Count V SURP 5 bits */
313 surp = indata_ptr[4+le_mode] >> 2 & 0x1f;
314 /* Number of surplus samples */
315 surp = (surp + 1) % 32;
317 /* One unknown bit, crc? */
318 unknown_bit = indata_ptr[4+le_mode] >> 1 & 0x01;
320 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
321 *nblks = (indata_ptr[4+le_mode] & 0x01) << 6 | indata_ptr[5-le_mode] >> 2;
322 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
323 encoded in the current frame per channel. */
324 ++(*nblks);
326 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
327 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
328 FSIZE defines the byte size of the current audio frame. */
329 fsize = (indata_ptr[5-le_mode] & 0x03) << 12 | indata_ptr[6+le_mode] << 4
330 | indata_ptr[7-le_mode] >> 4;
331 ++fsize;
333 /* Audio Channel Arrangement ACC AMODE 6 bits */
334 amode = (indata_ptr[7-le_mode] & 0x0f) << 2 | indata_ptr[8+le_mode] >> 6;
336 /* Source Sampling rate ACC SFREQ 4 bits */
337 *sfreq = indata_ptr[8+le_mode] >> 2 & 0x0f;
338 /* Transmission Bit Rate ACC RATE 5 bits */
339 *rate = (indata_ptr[8+le_mode] & 0x03) << 3
340 | (indata_ptr[9-le_mode] >> 5 & 0x07);
342 else
344 /* in the case judgement, we assure this */
345 ftype = 1;
346 surp = 0;
347 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
348 /* Bits usage:
349 32 bits: Sync code (28 + 4) 1th and 2th word, 4 bits in 3th word
350 1 bits: Frame type 1 bits in 3th word
351 5 bits: SURP 5 bits in 3th word
352 1 bits: crc? 1 bits in 3th word
353 7 bits: NBLKS 3 bits in 3th word, 4 bits in 4th word
354 14 bits: FSIZE 10 bits in 4th word, 4 bits in 5th word
355 in 14 bits mode, FSIZE = FSIZE*8/14*2
356 6 bits: AMODE 6 bits in 5th word
357 4 bits: SFREQ 4 bits in 5th word
358 5 bits: RATE 5 bits in 6th word
359 total bits: 75 bits */
361 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
362 *nblks = (indata_ptr[5-le_mode] & 0x07) << 4
363 | (indata_ptr[6+le_mode] & 0x3f) >> 2;
364 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
365 encoded in the current frame per channel. */
366 ++(*nblks);
368 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
369 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
370 FSIZE defines the byte size of the current audio frame. */
371 fsize = (indata_ptr[6+le_mode] & 0x03) << 12 | indata_ptr[7-le_mode] << 4
372 | (indata_ptr[8+le_mode] & 0x3f) >> 2;
373 ++fsize;
374 fsize = fsize * 8 / 14 * 2;
376 /* Audio Channel Arrangement ACC AMODE 6 bits */
377 amode = (indata_ptr[8+le_mode] & 0x03) << 4
378 | (indata_ptr[9-le_mode] & 0xf0) >> 4;
380 /* Source Sampling rate ACC SFREQ 4 bits */
381 *sfreq = indata_ptr[9-le_mode] & 0x0f;
382 /* Transmission Bit Rate ACC RATE 5 bits */
383 *rate = (indata_ptr[10+le_mode] & 0x3f) >> 1;
385 #if 0
386 if(*sfreq != 13)
388 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
389 return -1;
391 #endif
392 if((fsize > 8192) || (fsize < 96))
394 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
395 return -1;
398 if(*nblks != 8 &&
399 *nblks != 16 &&
400 *nblks != 32 &&
401 *nblks != 64 &&
402 *nblks != 128 &&
403 ftype == 1)
405 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
406 return -1;
409 return fsize;
412 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
414 int nblks;
415 int fsize;
416 int rate;
417 int sfreq;
419 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
420 if(fsize >= 0)
422 if(rate >= 0 && rate <= 29)
423 *bit_rate = DTS_BITRATES[rate];
424 else
425 *bit_rate = 0;
426 if(sfreq >= 1 && sfreq <= 15)
427 *sample_rate = DTS_SAMPLEFREQS[sfreq];
428 else
429 *sample_rate = 0;
431 return fsize;
434 static int convert_14bits_to_16bits(const unsigned char *src,
435 unsigned char *dest,
436 int len,
437 int is_le)
439 uint16_t *p = (uint16_t *)dest;
440 uint16_t buf = 0;
441 int spacebits = 16;
442 if (len <= 0) return 0;
443 while (len > 0) {
444 uint16_t v;
445 if (len == 1)
446 v = is_le ? src[0] : src[0] << 8;
447 else
448 v = is_le ? src[1] << 8 | src[0] : src[0] << 8 | src[1];
449 v <<= 2;
450 src += 2;
451 len -= 2;
452 buf |= v >> (16 - spacebits);
453 spacebits -= 14;
454 if (spacebits < 0) {
455 *p++ = buf;
456 spacebits += 16;
457 buf = v << (spacebits - 2);
460 *p++ = buf;
461 return (unsigned char *)p - dest;
464 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
466 int nblks;
467 int fsize;
468 int rate;
469 int sfreq;
470 int nr_samples;
471 int convert_16bits = 0;
472 uint16_t *buf16 = (uint16_t *)buf;
474 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
475 if(fsize < 0)
476 return -1;
477 nr_samples = nblks * 32;
479 buf16[0] = 0xf872; /* iec 61937 */
480 buf16[1] = 0x4e1f; /* syncword */
481 switch(nr_samples)
483 case 512:
484 buf16[2] = 0x000b; /* DTS-1 (512-sample bursts) */
485 break;
486 case 1024:
487 buf16[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
488 break;
489 case 2048:
490 buf16[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
491 break;
492 default:
493 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
494 buf16[2] = 0x0000;
495 break;
498 if(fsize + 8 > nr_samples * 2 * 2)
500 // dts wav (14bits LE) match this condition, one way to passthrough
501 // is not add iec 61937 header, decoders will notice the dts header
502 // and identify the dts stream. Another way here is convert
503 // the stream from 14 bits to 16 bits.
504 if ((indata_ptr[0] == 0xff || indata_ptr[0] == 0x1f)
505 && fsize * 14 / 16 + 8 <= nr_samples * 2 * 2) {
506 // The input stream is 14 bits, we can shrink it to 16 bits
507 // to save space for add the 61937 header
508 fsize = convert_14bits_to_16bits(indata_ptr,
509 &buf[8],
510 fsize,
511 indata_ptr[0] == 0xff /* is LE */
513 mp_msg(MSGT_DECAUDIO, MSGL_DBG3, "DTS: shrink 14 bits stream to "
514 "16 bits %02x%02x%02x%02x => %02x%02x%02x%02x, new size %d.\n",
515 indata_ptr[0], indata_ptr[1], indata_ptr[2], indata_ptr[3],
516 buf[8], buf[9], buf[10], buf[11], fsize);
517 convert_16bits = 1;
519 else
520 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: more data than fits\n");
523 buf16[3] = fsize << 3;
525 if (!convert_16bits) {
526 #ifdef WORDS_BIGENDIAN
527 /* BE stream */
528 if (indata_ptr[0] == 0x1f || indata_ptr[0] == 0x7f)
529 #else
530 /* LE stream */
531 if (indata_ptr[0] == 0xff || indata_ptr[0] == 0xfe)
532 #endif
533 memcpy(&buf[8], indata_ptr, fsize);
534 else
536 swab(indata_ptr, &buf[8], fsize);
537 if (fsize & 1) {
538 buf[8+fsize-1] = 0;
539 buf[8+fsize] = indata_ptr[fsize-1];
540 fsize++;
544 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
546 return nr_samples * 2 * 2;