cleanup: shut up more warnings
[mplayer/greg.git] / libmpcodecs / ad_hwac3.c
blobf5d6fbc933aa693fe1a8dcb2c1f72c666e959911
1 /*
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
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "mpbswap.h"
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",
43 "hwac3",
44 "Nick Kurshev/Peter Schüller",
45 "???",
49 LIBAD_EXTERN(hwac3)
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,
60 512, 576, 640};
61 int frmsizecod;
62 int bitrate;
63 int half;
65 if (buf[0] != 0x0b || buf[1] != 0x77) /* syncword */
66 return 0;
68 if (buf[5] >= 0x60) /* bsid >= 12 */
69 return 0;
70 half = buf[5] >> 3;
71 half = FFMAX(half - 8, 0);
73 frmsizecod = buf[4] & 63;
74 if (frmsizecod >= 38)
75 return 0;
76 bitrate = rate[frmsizecod >> 1];
77 *bit_rate = (bitrate * 1000) >> half;
79 switch (buf[4] & 0xc0) {
80 case 0:
81 *sample_rate = 48000 >> half;
82 return 4 * bitrate;
83 case 0x40:
84 *sample_rate = 44100 >> half;
85 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
86 case 0x80:
87 *sample_rate = 32000 >> half;
88 return 6 * bitrate;
89 default:
90 return 0;
94 static int ac3dts_fillbuff(sh_audio_t *sh_audio)
96 int length = 0;
97 int flags = 0;
98 int sample_rate = 0;
99 int bit_rate = 0;
101 sh_audio->a_in_buffer_len = 0;
102 /* sync frame:*/
103 while(1)
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);
110 if(c<0)
111 return -1; /* EOF*/
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);
118 if(length >= 12)
120 if(isdts != 1)
122 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
123 isdts = 1;
125 break;
128 else
130 length = a52_syncinfo(sh_audio->a_in_buffer, &sample_rate, &bit_rate);
131 if(length >= 7 && length <= 3840)
133 if(isdts != 0)
135 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
136 isdts = 0;
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;
152 return 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;
161 sh->channels = 2;
162 sh->samplesize = 2;
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;
167 return 1;
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");
176 return 0;
178 return 1;
181 static void uninit(sh_audio_t *sh)
185 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
187 switch(cmd)
189 case ADCTRL_RESYNC_STREAM:
190 case ADCTRL_SKIP_FRAME:
191 ac3dts_fillbuff(sh);
192 return CONTROL_TRUE;
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;
202 if(len <= 0)
203 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
204 return len; /*EOF*/
205 sh_audio->a_in_buffer_len = 0;
207 if(isdts == 1)
209 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
211 else if(isdts == 0)
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);
221 return 6144;
223 else
224 return -1;
228 static const int DTS_SAMPLEFREQS[16] =
231 8000,
232 16000,
233 32000,
234 64000,
235 128000,
236 11025,
237 22050,
238 44100,
239 88200,
240 176400,
241 12000,
242 24000,
243 48000,
244 96000,
245 192000
248 static const int DTS_BITRATES[30] =
250 32000,
251 56000,
252 64000,
253 96000,
254 112000,
255 128000,
256 192000,
257 224000,
258 256000,
259 320000,
260 384000,
261 448000,
262 512000,
263 576000,
264 640000,
265 768000,
266 896000,
267 1024000,
268 1152000,
269 1280000,
270 1344000,
271 1408000,
272 1411200,
273 1472000,
274 1536000,
275 1920000,
276 2048000,
277 3072000,
278 3840000,
279 4096000
282 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
284 int ftype;
285 int surp;
286 int unknown_bit av_unused;
287 int fsize;
288 int amode av_unused;
290 int word_mode;
291 int le_mode;
293 unsigned int first4bytes = indata_ptr[0] << 24 | indata_ptr[1] << 16
294 | indata_ptr[2] << 8 | indata_ptr[3];
296 switch(first4bytes)
298 /* 14 bits LE */
299 case 0xff1f00e8:
300 /* Also make sure frame type is 1. */
301 if ((indata_ptr[4]&0xf0) != 0xf0 || indata_ptr[5] != 0x07)
302 return -1;
303 word_mode = 0;
304 le_mode = 1;
305 break;
306 /* 14 bits BE */
307 case 0x1fffe800:
308 /* Also make sure frame type is 1. */
309 if (indata_ptr[4] != 0x07 || (indata_ptr[5]&0xf0) != 0xf0)
310 return -1;
311 word_mode = 0;
312 le_mode = 0;
313 break;
314 /* 16 bits LE */
315 case 0xfe7f0180:
316 word_mode = 1;
317 le_mode = 1;
318 break;
319 /* 16 bits BE */
320 case 0x7ffe8001:
321 word_mode = 1;
322 le_mode = 0;
323 break;
324 default:
325 return -1;
328 if(word_mode)
330 /* First bit after first 32 bits:
331 Frame type ( 1: Normal frame; 0: Termination frame ) */
332 ftype = indata_ptr[4+le_mode] >> 7;
334 if(ftype != 1)
336 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
337 return -1;
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. */
351 ++(*nblks);
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;
358 ++fsize;
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);
369 else
371 /* in the case judgement, we assure this */
372 ftype = 1;
373 surp = 0;
374 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
375 /* Bits usage:
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. */
393 ++(*nblks);
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;
400 ++fsize;
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;
412 #if 0
413 if(*sfreq != 13)
415 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
416 return -1;
418 #endif
419 if((fsize > 8192) || (fsize < 96))
421 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
422 return -1;
425 if(*nblks != 8 &&
426 *nblks != 16 &&
427 *nblks != 32 &&
428 *nblks != 64 &&
429 *nblks != 128 &&
430 ftype == 1)
432 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
433 return -1;
436 return fsize;
439 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
441 int nblks;
442 int fsize;
443 int rate;
444 int sfreq;
446 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
447 if(fsize >= 0)
449 if(rate >= 0 && rate <= 29)
450 *bit_rate = DTS_BITRATES[rate];
451 else
452 *bit_rate = 0;
453 if(sfreq >= 1 && sfreq <= 15)
454 *sample_rate = DTS_SAMPLEFREQS[sfreq];
455 else
456 *sample_rate = 0;
458 return fsize;
461 static int convert_14bits_to_16bits(const unsigned char *src,
462 unsigned char *dest,
463 int len,
464 int is_le)
466 uint16_t *p = (uint16_t *)dest;
467 uint16_t buf = 0;
468 int spacebits = 16;
469 if (len <= 0) return 0;
470 while (len > 0) {
471 uint16_t v;
472 if (len == 1)
473 v = is_le ? src[0] : src[0] << 8;
474 else
475 v = is_le ? src[1] << 8 | src[0] : src[0] << 8 | src[1];
476 v <<= 2;
477 src += 2;
478 len -= 2;
479 buf |= v >> (16 - spacebits);
480 spacebits -= 14;
481 if (spacebits < 0) {
482 *p++ = buf;
483 spacebits += 16;
484 buf = v << (spacebits - 2);
487 *p++ = buf;
488 return (unsigned char *)p - dest;
491 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
493 int nblks;
494 int fsize;
495 int rate;
496 int sfreq;
497 int nr_samples;
498 int convert_16bits = 0;
499 uint16_t *buf16 = (uint16_t *)buf;
501 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
502 if(fsize < 0)
503 return -1;
504 nr_samples = nblks * 32;
506 buf16[0] = 0xf872; /* iec 61937 */
507 buf16[1] = 0x4e1f; /* syncword */
508 switch(nr_samples)
510 case 512:
511 buf16[2] = 0x000b; /* DTS-1 (512-sample bursts) */
512 break;
513 case 1024:
514 buf16[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
515 break;
516 case 2048:
517 buf16[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
518 break;
519 default:
520 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
521 buf16[2] = 0x0000;
522 break;
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,
536 &buf[8],
537 fsize,
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);
544 convert_16bits = 1;
546 else
547 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: more data than fits\n");
550 buf16[3] = fsize << 3;
552 if (!convert_16bits) {
553 #if HAVE_BIGENDIAN
554 /* BE stream */
555 if (indata_ptr[0] == 0x1f || indata_ptr[0] == 0x7f)
556 #else
557 /* LE stream */
558 if (indata_ptr[0] == 0xff || indata_ptr[0] == 0xfe)
559 #endif
560 memcpy(&buf[8], indata_ptr, fsize);
561 else
563 swab(indata_ptr, &buf[8], fsize);
564 if (fsize & 1) {
565 buf[8+fsize-1] = 0;
566 buf[8+fsize] = indata_ptr[fsize-1];
567 fsize++;
571 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
573 return nr_samples * 2 * 2;