audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / libmpcodecs / ad_hwac3.c
bloba11c2c90e70767be34054133da986ee65eb6088f
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 <libavutil/intreadwrite.h>
30 #include <libavutil/common.h>
32 #include "config.h"
33 #include "mp_msg.h"
34 #include "mpbswap.h"
36 #include "ad_internal.h"
39 static int isdts = -1;
41 static const ad_info_t info =
43 "AC3/DTS pass-through S/PDIF",
44 "hwac3",
45 "Nick Kurshev/Peter Schüller",
46 "???",
50 LIBAD_EXTERN(hwac3)
53 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate);
54 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf);
57 static int a52_syncinfo (uint8_t *buf, int *sample_rate, int *bit_rate)
59 static const uint16_t rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
60 128, 160, 192, 224, 256, 320, 384, 448,
61 512, 576, 640};
62 int frmsizecod;
63 int bitrate;
64 int half;
66 if (buf[0] != 0x0b || buf[1] != 0x77) /* syncword */
67 return 0;
69 if (buf[5] >= 0x60) /* bsid >= 12 */
70 return 0;
71 half = buf[5] >> 3;
72 half = FFMAX(half - 8, 0);
74 frmsizecod = buf[4] & 63;
75 if (frmsizecod >= 38)
76 return 0;
77 bitrate = rate[frmsizecod >> 1];
78 *bit_rate = (bitrate * 1000) >> half;
80 switch (buf[4] & 0xc0) {
81 case 0:
82 *sample_rate = 48000 >> half;
83 return 4 * bitrate;
84 case 0x40:
85 *sample_rate = 44100 >> half;
86 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
87 case 0x80:
88 *sample_rate = 32000 >> half;
89 return 6 * bitrate;
90 default:
91 return 0;
95 static int ac3dts_fillbuff(sh_audio_t *sh_audio)
97 int length = 0;
98 int flags = 0;
99 int sample_rate = 0;
100 int bit_rate = 0;
102 sh_audio->a_in_buffer_len = 0;
103 /* sync frame:*/
104 while(1)
106 // Original code DTS has a 10 bytes header.
107 // Now max 12 bytes for 14 bits DTS header.
108 while(sh_audio->a_in_buffer_len < 12)
110 int c = demux_getc(sh_audio->ds);
111 if(c<0)
112 return -1; /* EOF*/
113 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++] = c;
116 if (sh_audio->format == 0x2001)
118 length = dts_syncinfo(sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
119 if(length >= 12)
121 if(isdts != 1)
123 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to DTS, %d bps, %d Hz\n", bit_rate, sample_rate);
124 isdts = 1;
126 break;
129 else
131 length = a52_syncinfo(sh_audio->a_in_buffer, &sample_rate, &bit_rate);
132 if(length >= 7 && length <= 3840)
134 if(isdts != 0)
136 mp_msg(MSGT_DECAUDIO, MSGL_STATUS, "hwac3: switched to AC3, %d bps, %d Hz\n", bit_rate, sample_rate);
137 isdts = 0;
139 break; /* we're done.*/
142 /* bad file => resync*/
143 memcpy(sh_audio->a_in_buffer, sh_audio->a_in_buffer + 1, 11);
144 --sh_audio->a_in_buffer_len;
146 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);
148 sh_audio->samplerate = sample_rate;
149 sh_audio->i_bps = bit_rate / 8;
150 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer + 12, length - 12);
151 sh_audio->a_in_buffer_len = length;
153 return length;
157 static int preinit(sh_audio_t *sh)
159 /* Dolby AC3 audio: */
160 sh->audio_out_minsize = 128 * 32 * 2 * 2; // DTS seems to need more than AC3
161 sh->audio_in_minsize = 8192;
162 sh->channels = 2;
163 sh->samplesize = 2;
164 sh->sample_format = AF_FORMAT_AC3_BE;
165 // HACK for DTS where useless swapping can't easily be removed
166 if (sh->format == 0x2001)
167 sh->sample_format = AF_FORMAT_AC3_NE;
168 return 1;
171 static int init(sh_audio_t *sh_audio)
173 /* Dolby AC3 passthrough:*/
174 if(ac3dts_fillbuff(sh_audio) < 0)
176 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "AC3/DTS sync failed\n");
177 return 0;
179 return 1;
182 static void uninit(sh_audio_t *sh)
186 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
188 switch(cmd)
190 case ADCTRL_RESYNC_STREAM:
191 case ADCTRL_SKIP_FRAME:
192 ac3dts_fillbuff(sh);
193 return CONTROL_TRUE;
195 return CONTROL_UNKNOWN;
199 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
201 int len = sh_audio->a_in_buffer_len;
203 if(len <= 0)
204 if((len = ac3dts_fillbuff(sh_audio)) <= 0)
205 return len; /*EOF*/
206 sh_audio->a_in_buffer_len = 0;
208 if(isdts == 1)
210 return decode_audio_dts(sh_audio->a_in_buffer, len, buf);
212 else if(isdts == 0)
214 AV_WB16(buf, 0xF872); // iec 61937 syncword 1
215 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2
216 buf[4] = sh_audio->a_in_buffer[5] & 0x7; // bsmod
217 buf[5] = 0x01; // data-type ac3
218 AV_WB16(buf + 6, len << 3); // number of bits in payload
219 memcpy(buf + 8, sh_audio->a_in_buffer, len);
220 memset(buf + 8 + len, 0, 6144 - 8 - len);
222 return 6144;
224 else
225 return -1;
229 static const int DTS_SAMPLEFREQS[16] =
232 8000,
233 16000,
234 32000,
235 64000,
236 128000,
237 11025,
238 22050,
239 44100,
240 88200,
241 176400,
242 12000,
243 24000,
244 48000,
245 96000,
246 192000
249 static const int DTS_BITRATES[30] =
251 32000,
252 56000,
253 64000,
254 96000,
255 112000,
256 128000,
257 192000,
258 224000,
259 256000,
260 320000,
261 384000,
262 448000,
263 512000,
264 576000,
265 640000,
266 768000,
267 896000,
268 1024000,
269 1152000,
270 1280000,
271 1344000,
272 1408000,
273 1411200,
274 1472000,
275 1536000,
276 1920000,
277 2048000,
278 3072000,
279 3840000,
280 4096000
283 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
285 int ftype;
286 int surp;
287 int unknown_bit av_unused;
288 int fsize;
289 int amode av_unused;
291 int word_mode;
292 int le_mode;
294 unsigned int first4bytes = indata_ptr[0] << 24 | indata_ptr[1] << 16
295 | indata_ptr[2] << 8 | indata_ptr[3];
297 switch(first4bytes)
299 /* 14 bits LE */
300 case 0xff1f00e8:
301 /* Also make sure frame type is 1. */
302 if ((indata_ptr[4]&0xf0) != 0xf0 || indata_ptr[5] != 0x07)
303 return -1;
304 word_mode = 0;
305 le_mode = 1;
306 break;
307 /* 14 bits BE */
308 case 0x1fffe800:
309 /* Also make sure frame type is 1. */
310 if (indata_ptr[4] != 0x07 || (indata_ptr[5]&0xf0) != 0xf0)
311 return -1;
312 word_mode = 0;
313 le_mode = 0;
314 break;
315 /* 16 bits LE */
316 case 0xfe7f0180:
317 word_mode = 1;
318 le_mode = 1;
319 break;
320 /* 16 bits BE */
321 case 0x7ffe8001:
322 word_mode = 1;
323 le_mode = 0;
324 break;
325 default:
326 return -1;
329 if(word_mode)
331 /* First bit after first 32 bits:
332 Frame type ( 1: Normal frame; 0: Termination frame ) */
333 ftype = indata_ptr[4+le_mode] >> 7;
335 if(ftype != 1)
337 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Termination frames not handled, REPORT BUG\n");
338 return -1;
340 /* Next 5 bits: Surplus Sample Count V SURP 5 bits */
341 surp = indata_ptr[4+le_mode] >> 2 & 0x1f;
342 /* Number of surplus samples */
343 surp = (surp + 1) % 32;
345 /* One unknown bit, crc? */
346 unknown_bit = indata_ptr[4+le_mode] >> 1 & 0x01;
348 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
349 *nblks = (indata_ptr[4+le_mode] & 0x01) << 6 | indata_ptr[5-le_mode] >> 2;
350 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
351 encoded in the current frame per channel. */
352 ++(*nblks);
354 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
355 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
356 FSIZE defines the byte size of the current audio frame. */
357 fsize = (indata_ptr[5-le_mode] & 0x03) << 12 | indata_ptr[6+le_mode] << 4
358 | indata_ptr[7-le_mode] >> 4;
359 ++fsize;
361 /* Audio Channel Arrangement ACC AMODE 6 bits */
362 amode = (indata_ptr[7-le_mode] & 0x0f) << 2 | indata_ptr[8+le_mode] >> 6;
364 /* Source Sampling rate ACC SFREQ 4 bits */
365 *sfreq = indata_ptr[8+le_mode] >> 2 & 0x0f;
366 /* Transmission Bit Rate ACC RATE 5 bits */
367 *rate = (indata_ptr[8+le_mode] & 0x03) << 3
368 | (indata_ptr[9-le_mode] >> 5 & 0x07);
370 else
372 /* in the case judgement, we assure this */
373 ftype = 1;
374 surp = 0;
375 /* 14 bits support, every 2 bytes, & 0x3fff, got used 14 bits */
376 /* Bits usage:
377 32 bits: Sync code (28 + 4) 1th and 2th word, 4 bits in 3th word
378 1 bits: Frame type 1 bits in 3th word
379 5 bits: SURP 5 bits in 3th word
380 1 bits: crc? 1 bits in 3th word
381 7 bits: NBLKS 3 bits in 3th word, 4 bits in 4th word
382 14 bits: FSIZE 10 bits in 4th word, 4 bits in 5th word
383 in 14 bits mode, FSIZE = FSIZE*8/14*2
384 6 bits: AMODE 6 bits in 5th word
385 4 bits: SFREQ 4 bits in 5th word
386 5 bits: RATE 5 bits in 6th word
387 total bits: 75 bits */
389 /* NBLKS 7 bits: Valid Range=5-127, Invalid Range=0-4 */
390 *nblks = (indata_ptr[5-le_mode] & 0x07) << 4
391 | (indata_ptr[6+le_mode] & 0x3f) >> 2;
392 /* NBLKS+1 indicates the number of 32 sample PCM audio blocks per channel
393 encoded in the current frame per channel. */
394 ++(*nblks);
396 /* Frame Byte Size V FSIZE 14 bits: 0-94=Invalid, 95-8191=Valid range-1
397 (ie. 96 bytes to 8192 bytes), 8192-16383=Invalid
398 FSIZE defines the byte size of the current audio frame. */
399 fsize = (indata_ptr[6+le_mode] & 0x03) << 12 | indata_ptr[7-le_mode] << 4
400 | (indata_ptr[8+le_mode] & 0x3f) >> 2;
401 ++fsize;
402 fsize = fsize * 8 / 14 * 2;
404 /* Audio Channel Arrangement ACC AMODE 6 bits */
405 amode = (indata_ptr[8+le_mode] & 0x03) << 4
406 | (indata_ptr[9-le_mode] & 0xf0) >> 4;
408 /* Source Sampling rate ACC SFREQ 4 bits */
409 *sfreq = indata_ptr[9-le_mode] & 0x0f;
410 /* Transmission Bit Rate ACC RATE 5 bits */
411 *rate = (indata_ptr[10+le_mode] & 0x3f) >> 1;
413 #if 0
414 if(*sfreq != 13)
416 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: Only 48kHz supported, REPORT BUG\n");
417 return -1;
419 #endif
420 if((fsize > 8192) || (fsize < 96))
422 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: fsize: %d invalid, REPORT BUG\n", fsize);
423 return -1;
426 if(*nblks != 8 &&
427 *nblks != 16 &&
428 *nblks != 32 &&
429 *nblks != 64 &&
430 *nblks != 128 &&
431 ftype == 1)
433 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: nblks %d not valid for normal frame, REPORT BUG\n", *nblks);
434 return -1;
437 return fsize;
440 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate)
442 int nblks;
443 int fsize;
444 int rate;
445 int sfreq;
447 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
448 if(fsize >= 0)
450 if(rate >= 0 && rate <= 29)
451 *bit_rate = DTS_BITRATES[rate];
452 else
453 *bit_rate = 0;
454 if(sfreq >= 1 && sfreq <= 15)
455 *sample_rate = DTS_SAMPLEFREQS[sfreq];
456 else
457 *sample_rate = 0;
459 return fsize;
462 static int convert_14bits_to_16bits(const unsigned char *src,
463 unsigned char *dest,
464 int len,
465 int is_le)
467 uint16_t *p = (uint16_t *)dest;
468 uint16_t buf = 0;
469 int spacebits = 16;
470 if (len <= 0) return 0;
471 while (len > 0) {
472 uint16_t v;
473 if (len == 1)
474 v = is_le ? src[0] : src[0] << 8;
475 else
476 v = is_le ? src[1] << 8 | src[0] : src[0] << 8 | src[1];
477 v <<= 2;
478 src += 2;
479 len -= 2;
480 buf |= v >> (16 - spacebits);
481 spacebits -= 14;
482 if (spacebits < 0) {
483 *p++ = buf;
484 spacebits += 16;
485 buf = v << (spacebits - 2);
488 *p++ = buf;
489 return (unsigned char *)p - dest;
492 static int decode_audio_dts(unsigned char *indata_ptr, int len, unsigned char *buf)
494 int nblks;
495 int fsize;
496 int rate;
497 int sfreq;
498 int nr_samples;
499 int convert_16bits = 0;
500 uint16_t *buf16 = (uint16_t *)buf;
502 fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
503 if(fsize < 0)
504 return -1;
505 nr_samples = nblks * 32;
507 buf16[0] = 0xf872; /* iec 61937 */
508 buf16[1] = 0x4e1f; /* syncword */
509 switch(nr_samples)
511 case 512:
512 buf16[2] = 0x000b; /* DTS-1 (512-sample bursts) */
513 break;
514 case 1024:
515 buf16[2] = 0x000c; /* DTS-2 (1024-sample bursts) */
516 break;
517 case 2048:
518 buf16[2] = 0x000d; /* DTS-3 (2048-sample bursts) */
519 break;
520 default:
521 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: %d-sample bursts not supported\n", nr_samples);
522 buf16[2] = 0x0000;
523 break;
526 if(fsize + 8 > nr_samples * 2 * 2)
528 // dts wav (14bits LE) match this condition, one way to passthrough
529 // is not add iec 61937 header, decoders will notice the dts header
530 // and identify the dts stream. Another way here is convert
531 // the stream from 14 bits to 16 bits.
532 if ((indata_ptr[0] == 0xff || indata_ptr[0] == 0x1f)
533 && fsize * 14 / 16 + 8 <= nr_samples * 2 * 2) {
534 // The input stream is 14 bits, we can shrink it to 16 bits
535 // to save space for add the 61937 header
536 fsize = convert_14bits_to_16bits(indata_ptr,
537 &buf[8],
538 fsize,
539 indata_ptr[0] == 0xff /* is LE */
541 mp_msg(MSGT_DECAUDIO, MSGL_DBG3, "DTS: shrink 14 bits stream to "
542 "16 bits %02x%02x%02x%02x => %02x%02x%02x%02x, new size %d.\n",
543 indata_ptr[0], indata_ptr[1], indata_ptr[2], indata_ptr[3],
544 buf[8], buf[9], buf[10], buf[11], fsize);
545 convert_16bits = 1;
547 else
548 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "DTS: more data than fits\n");
551 buf16[3] = fsize << 3;
553 if (!convert_16bits) {
554 #if HAVE_BIGENDIAN
555 /* BE stream */
556 if (indata_ptr[0] == 0x1f || indata_ptr[0] == 0x7f)
557 #else
558 /* LE stream */
559 if (indata_ptr[0] == 0xff || indata_ptr[0] == 0xfe)
560 #endif
561 memcpy(&buf[8], indata_ptr, fsize);
562 else
564 swab(indata_ptr, &buf[8], fsize);
565 if (fsize & 1) {
566 buf[8+fsize-1] = 0;
567 buf[8+fsize] = indata_ptr[fsize-1];
568 fsize++;
572 memset(&buf[fsize + 8], 0, nr_samples * 2 * 2 - (fsize + 8));
574 return nr_samples * 2 * 2;