Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / ad_hwac3.c
blob34850f2a6aa5532f6c8048f8d7928657cf15bae8
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 #ifdef USE_LIBA52
16 #include "mp_msg.h"
17 #include "help_mp.h"
18 #include "mpbswap.h"
20 #include "ad_internal.h"
22 #include "liba52/a52.h"
25 static int isdts = -1;
27 static ad_info_t info =
29 "AC3/DTS pass-through S/PDIF",
30 "hwac3",
31 "Nick Kurshev/Peter Schüller",
32 "???",
36 LIBAD_EXTERN(hwac3)
39 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate);
40 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf);
43 static int ac3dts_fillbuff(sh_audio_t *sh_audio)
45 int length = 0;
46 int flags = 0;
47 int sample_rate = 0;
48 int bit_rate = 0;
50 sh_audio->a_in_buffer_len = 0;
51 /* sync frame:*/
52 while(1)
54 // Original code DTS has a 10 bytes header.
55 // Now max 12 bytes for 14 bits DTS header.
56 while(sh_audio->a_in_buffer_len < 12)
58 int c = demux_getc(sh_audio->ds);
59 if(c<0)
60 return -1; /* EOF*/
61 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++] = c;
64 length = dts_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
65 if(length >= 12)
67 if(isdts != 1)
69 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
70 isdts = 1;
72 break;
74 length = a52_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
75 if(length >= 7 && length <= 3840)
77 if(isdts != 0)
79 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
80 isdts = 0;
82 break; /* we're done.*/
84 /* bad file => resync*/
85 memcpy(sh_audio->a_in_buffer, sh_audio->a_in_buffer + 1, 11);
86 --sh_audio->a_in_buffer_len;
88 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);
90 sh_audio->samplerate = sample_rate;
91 sh_audio->i_bps = bit_rate / 8;
92 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer + 12, length - 12);
93 sh_audio->a_in_buffer_len = length;
95 // TODO: is DTS also checksummed?
96 if(isdts == 0 && crc16_block(sh_audio->a_in_buffer + 2, length - 2) != 0)
97 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "a52: CRC check failed! \n");
99 return length;
103 static int preinit(sh_audio_t *sh)
105 /* Dolby AC3 audio: */
106 sh->audio_out_minsize = 128 * 32 * 2 * 2; // DTS seems to need more than AC3
107 sh->audio_in_minsize = 8192;
108 sh->channels = 2;
109 sh->samplesize = 2;
110 sh->sample_format = AF_FORMAT_AC3;
111 return 1;
114 static int init(sh_audio_t *sh_audio)
116 /* Dolby AC3 passthrough:*/
117 a52_state_t *a52_state = a52_init(0);
118 if(a52_state == NULL)
120 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "A52 init failed\n");
121 return 0;
123 if(ac3dts_fillbuff(sh_audio) < 0)
125 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "AC3/DTS sync failed\n");
126 return 0;
128 return 1;
131 static void uninit(sh_audio_t *sh)
135 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
137 switch(cmd)
139 case ADCTRL_RESYNC_STREAM:
140 case ADCTRL_SKIP_FRAME:
141 ac3dts_fillbuff(sh);
142 return CONTROL_TRUE;
144 return CONTROL_UNKNOWN;
148 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
150 int len = sh_audio->a_in_buffer_len;
152 if(len <= 0)
153 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
154 return len; /*EOF*/
155 sh_audio->a_in_buffer_len = 0;
157 if(isdts == 1)
159 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
161 else if(isdts == 0)
163 uint16_t *buf16 = (uint16_t *)buf;
164 buf16[0] = 0xF872; // iec 61937 syncword 1
165 buf16[1] = 0x4E1F; // iec 61937 syncword 2
166 buf16[2] = 0x0001; // data-type ac3
167 buf16[2] |= (sh_audio->a_in_buffer[5] & 0x7) << 8; // bsmod
168 buf16[3] = len << 3; // number of bits in payload
169 #ifdef WORDS_BIGENDIAN
170 memcpy(buf + 8, sh_audio->a_in_buffer, len);
171 #else
172 swab(sh_audio->a_in_buffer, buf + 8, len);
173 if (len & 1) {
174 buf[8+len-1] = 0;
175 buf[8+len] = sh_audio->a_in_buffer[len-1];
176 len++;
178 #endif
179 memset(buf + 8 + len, 0, 6144 - 8 - len);
181 return 6144;
183 else
184 return -1;
188 static const int DTS_SAMPLEFREQS[16] =
191 8000,
192 16000,
193 32000,
194 64000,
195 128000,
196 11025,
197 22050,
198 44100,
199 88200,
200 176400,
201 12000,
202 24000,
203 48000,
204 96000,
205 192000
208 static const int DTS_BITRATES[30] =
210 32000,
211 56000,
212 64000,
213 96000,
214 112000,
215 128000,
216 192000,
217 224000,
218 256000,
219 320000,
220 384000,
221 448000,
222 512000,
223 576000,
224 640000,
225 768000,
226 896000,
227 1024000,
228 1152000,
229 1280000,
230 1344000,
231 1408000,
232 1411200,
233 1472000,
234 1536000,
235 1920000,
236 2048000,
237 3072000,
238 3840000,
239 4096000
242 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
244 int ftype;
245 int surp;
246 int unknown_bit;
247 int fsize;
248 int amode;
250 int word_mode;
251 int le_mode;
253 unsigned int first4bytes = indata_ptr[0] << 24 | indata_ptr[1] << 16
254 | indata_ptr[2] << 8 | indata_ptr[3];
256 switch(first4bytes)
258 /* 14 bits LE */
259 case 0xff1f00e8:
260 /* Also make sure frame type is 1. */
261 if ((indata_ptr[4]&0xf0) != 0xf0 || indata_ptr[5] != 0x07)
262 return -1;
263 word_mode = 0;
264 le_mode = 1;
265 break;
266 /* 14 bits BE */
267 case 0x1fffe800:
268 /* Also make sure frame type is 1. */
269 if (indata_ptr[4] != 0x07 || (indata_ptr[5]&0xf0) != 0xf0)
270 return -1;
271 word_mode = 0;
272 le_mode = 0;
273 break;
274 /* 16 bits LE */
275 case 0xfe7f0180:
276 word_mode = 1;
277 le_mode = 1;
278 break;
279 /* 16 bits BE */
280 case 0x7ffe8001:
281 word_mode = 1;
282 le_mode = 0;
283 break;
284 default:
285 return -1;
288 if(word_mode)
290 /* First bit after first 32 bits:
291 Frame type ( 1: Normal frame; 0: Termination frame ) */
292 ftype = indata_ptr[4+le_mode] >> 7;
294 if(ftype != 1)
296 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
297 return -1;
299 /* Next 5 bits: Surplus Sample Count V SURP 5 bits */
300 surp = indata_ptr[4+le_mode] >> 2 & 0x1f;
301 /* Number of surplus samples */
302 surp = (surp + 1) % 32;
304 /* One unknown bit, crc? */
305 unknown_bit = indata_ptr[4+le_mode] >> 1 & 0x01;
307 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
308 *nblks = (indata_ptr[4+le_mode] & 0x01) << 6 | indata_ptr[5-le_mode] >> 2;
309 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
310 encoded in the current frame per channel. */
311 ++(*nblks);
313 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
314 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
315 FSIZE defines the byte size of the current audio frame. */
316 fsize = (indata_ptr[5-le_mode] & 0x03) << 12 | indata_ptr[6+le_mode] << 4
317 | indata_ptr[7-le_mode] >> 4;
318 ++fsize;
320 /* Audio Channel Arrangement ACC AMODE 6 bits */
321 amode = (indata_ptr[7-le_mode] & 0x0f) << 2 | indata_ptr[8+le_mode] >> 6;
323 /* Source Sampling rate ACC SFREQ 4 bits */
324 *sfreq = indata_ptr[8+le_mode] >> 2 & 0x0f;
325 /* Transmission Bit Rate ACC RATE 5 bits */
326 *rate = (indata_ptr[8+le_mode] & 0x03) << 3
327 | (indata_ptr[9-le_mode] >> 5 & 0x07);
329 else
331 /* in the case judgement, we assure this */
332 ftype = 1;
333 surp = 0;
334 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
335 /* Bits usage:
336 32 bits: Sync code (28 + 4) 1th and 2th word, 4 bits in 3th word
337 1 bits: Frame type 1 bits in 3th word
338 5 bits: SURP 5 bits in 3th word
339 1 bits: crc? 1 bits in 3th word
340 7 bits: NBLKS 3 bits in 3th word, 4 bits in 4th word
341 14 bits: FSIZE 10 bits in 4th word, 4 bits in 5th word
342 in 14 bits mode, FSIZE = FSIZE*8/14*2
343 6 bits: AMODE 6 bits in 5th word
344 4 bits: SFREQ 4 bits in 5th word
345 5 bits: RATE 5 bits in 6th word
346 total bits: 75 bits */
348 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
349 *nblks = (indata_ptr[5-le_mode] & 0x07) << 4
350 | (indata_ptr[6+le_mode] & 0x3f) >> 2;
351 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
352 encoded in the current frame per channel. */
353 ++(*nblks);
355 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
356 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
357 FSIZE defines the byte size of the current audio frame. */
358 fsize = (indata_ptr[6+le_mode] & 0x03) << 12 | indata_ptr[7-le_mode] << 4
359 | (indata_ptr[8+le_mode] & 0x3f) >> 2;
360 ++fsize;
361 fsize = fsize * 8 / 14 * 2;
363 /* Audio Channel Arrangement ACC AMODE 6 bits */
364 amode = (indata_ptr[8+le_mode] & 0x03) << 4
365 | (indata_ptr[9-le_mode] & 0xf0) >> 4;
367 /* Source Sampling rate ACC SFREQ 4 bits */
368 *sfreq = indata_ptr[9-le_mode] & 0x0f;
369 /* Transmission Bit Rate ACC RATE 5 bits */
370 *rate = (indata_ptr[10+le_mode] & 0x3f) >> 1;
372 #if 0
373 if(*sfreq != 13)
375 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
376 return -1;
378 #endif
379 if((fsize > 8192) || (fsize < 96))
381 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
382 return -1;
385 if(*nblks != 8 &&
386 *nblks != 16 &&
387 *nblks != 32 &&
388 *nblks != 64 &&
389 *nblks != 128 &&
390 ftype == 1)
392 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
393 return -1;
396 return fsize;
399 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
401 int nblks;
402 int fsize;
403 int rate;
404 int sfreq;
406 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
407 if(fsize >= 0)
409 if(rate >= 0 && rate <= 29)
410 *bit_rate = DTS_BITRATES[rate];
411 else
412 *bit_rate = 0;
413 if(sfreq >= 1 && sfreq <= 15)
414 *sample_rate = DTS_SAMPLEFREQS[sfreq];
415 else
416 *sample_rate = 0;
418 return fsize;
421 static int convert_14bits_to_16bits(const unsigned char *src,
422 unsigned char *dest,
423 int len,
424 int is_le)
426 uint16_t *p = (uint16_t *)dest;
427 uint16_t buf = 0;
428 int spacebits = 16;
429 if (len <= 0) return 0;
430 while (len > 0) {
431 uint16_t v;
432 if (len == 1)
433 v = is_le ? src[0] : src[0] << 8;
434 else
435 v = is_le ? src[1] << 8 | src[0] : src[0] << 8 | src[1];
436 v <<= 2;
437 src += 2;
438 len -= 2;
439 buf |= v >> (16 - spacebits);
440 spacebits -= 14;
441 if (spacebits < 0) {
442 *p++ = buf;
443 spacebits += 16;
444 buf = v << (spacebits - 2);
447 *p++ = buf;
448 return (unsigned char *)p - dest;
451 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
453 int nblks;
454 int fsize;
455 int rate;
456 int sfreq;
457 int nr_samples;
458 int convert_16bits = 0;
459 uint16_t *buf16 = (uint16_t *)buf;
461 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
462 if(fsize < 0)
463 return -1;
464 nr_samples = nblks * 32;
466 buf16[0] = 0xf872; /* iec 61937 */
467 buf16[1] = 0x4e1f; /* syncword */
468 switch(nr_samples)
470 case 512:
471 buf16[2] = 0x000b; /* DTS-1 (512-sample bursts) */
472 break;
473 case 1024:
474 buf16[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
475 break;
476 case 2048:
477 buf16[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
478 break;
479 default:
480 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
481 buf16[2] = 0x0000;
482 break;
485 if(fsize + 8 > nr_samples * 2 * 2)
487 // dts wav (14bits LE) match this condition, one way to passthrough
488 // is not add iec 61937 header, decoders will notice the dts header
489 // and identify the dts stream. Another way here is convert
490 // the stream from 14 bits to 16 bits.
491 if ((indata_ptr[0] == 0xff || indata_ptr[0] == 0x1f)
492 && fsize * 14 / 16 + 8 <= nr_samples * 2 * 2) {
493 // The input stream is 14 bits, we can shrink it to 16 bits
494 // to save space for add the 61937 header
495 fsize = convert_14bits_to_16bits(indata_ptr,
496 &buf[8],
497 fsize,
498 indata_ptr[0] == 0xff /* is LE */
500 mp_msg(MSGT_DECAUDIO, MSGL_DBG3, "DTS: shrink 14 bits stream to "
501 "16 bits %02x%02x%02x%02x => %02x%02x%02x%02x, new size %d.\n",
502 indata_ptr[0], indata_ptr[1], indata_ptr[2], indata_ptr[3],
503 buf[8], buf[9], buf[10], buf[11], fsize);
504 convert_16bits = 1;
506 else
507 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: more data than fits\n");
510 buf16[3] = fsize << 3;
512 if (!convert_16bits) {
513 #ifdef WORDS_BIGENDIAN
514 /* BE stream */
515 if (indata_ptr[0] == 0x1f || indata_ptr[0] == 0x7f)
516 #else
517 /* LE stream */
518 if (indata_ptr[0] == 0xff || indata_ptr[0] == 0xfe)
519 #endif
520 memcpy(&buf[8], indata_ptr, fsize);
521 else
523 swab(indata_ptr, &buf[8], fsize);
524 if (fsize & 1) {
525 buf[8+fsize-1] = 0;
526 buf[8+fsize] = indata_ptr[fsize-1];
527 fsize++;
531 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
533 return nr_samples * 2 * 2;
535 #endif