From 6bd887bcdbf93a5b1225a41a2d23a53b71c165e5 Mon Sep 17 00:00:00 2001 From: Guenter Bartsch Date: Mon, 28 May 2001 13:25:56 +0000 Subject: latest updates from walken CVS patchset: 93 CVS date: 2001/05/28 13:25:56 --- src/libac3/bit_allocate.c | 509 +++++++++++++++++----------------------------- src/libac3/bit_allocate.h | 6 +- src/libac3/parse.c | 73 ++++--- 3 files changed, 234 insertions(+), 354 deletions(-) (limited to 'src') diff --git a/src/libac3/bit_allocate.c b/src/libac3/bit_allocate.c index 407626f08..74c5ec902 100644 --- a/src/libac3/bit_allocate.c +++ b/src/libac3/bit_allocate.c @@ -27,360 +27,225 @@ #include "ac3_internal.h" #include "bit_allocate.h" - - -static int16_t calc_lowcomp(int16_t a,int16_t b0,int16_t b1,int16_t bin); -static inline uint16_t min(int16_t a,int16_t b); -static inline uint16_t max(int16_t a,int16_t b); - -static void ba_compute_mask(int16_t start, int16_t end, uint16_t fscod, - uint16_t deltbae, uint16_t deltnseg, - uint16_t deltoffst[], uint16_t deltba[], - uint16_t deltlen[], int16_t excite[], - int16_t mask[]); -static void ba_compute_bap(int16_t start, int16_t end, int16_t snroffset, - int8_t exp[], int16_t mask[], int16_t bap[]); - -/* Misc LUTs for bit allocation process */ - -static int16_t slowdec[] = { 0x0f, 0x11, 0x13, 0x15 }; -static int16_t fastdec[] = { 0x3f, 0x53, 0x67, 0x7b }; -static int16_t slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 }; -static int16_t dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 }; - -static uint16_t floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 }; -static int16_t fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 }; - - -static int16_t bndtab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26, 27, 28, 31, - 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, - 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 }; - -static int16_t bndsz[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, - 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, - 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 }; - -static int16_t masktab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29, - 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, - 34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37, - 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40, - 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, - 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, - 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, - 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, - 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, - 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, - 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, - 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, - 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, - 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, - 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 0, 0, 0 }; - - -static int16_t latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039, - 0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032, - 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c, - 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026, - 0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021, - 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c, - 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018, - 0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015, - 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012, - 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f, - 0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d, - 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b, - 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009, - 0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, - 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006, - 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005, - 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004, - 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, - 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, - 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002, - 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, - 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, - 0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, - 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, - 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, - 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, - 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, - 0x0000, 0x0000, 0x0000, 0x0000}; - -static int16_t hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0, - 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, - 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350, - 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, - 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420, - 0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800, - 0x0840, 0x0840 }, - { 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, - 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, - 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, - 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, - 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0, - 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630, - 0x0840, 0x0840 }, - { 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, - 0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, - 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390, - 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, - 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310, - 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440, - 0x0450, 0x04e0 }}; - - -static int16_t baptab[] = { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, - 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, - 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, - 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 }; - -static int16_t sdecay; -static int16_t fdecay; -static int16_t sgain; -static int16_t dbknee; -static int16_t floor; -static int16_t bndpsd[256]; -static int16_t excite[256]; -static int16_t mask[256]; - -static inline uint16_t max(int16_t a,int16_t b) -{ - return (a > b ? a : b); -} - -static inline uint16_t min(int16_t a,int16_t b) +static int hthtab[3][50] = { + {0x730, 0x730, 0x7c0, 0x800, 0x820, 0x840, 0x850, 0x850, 0x860, 0x860, + 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x890, 0x890, + 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900, + 0x910, 0x910, 0x910, 0x910, 0x900, 0x8f0, 0x8c0, 0x870, 0x820, 0x7e0, + 0x7a0, 0x770, 0x760, 0x7a0, 0x7c0, 0x7c0, 0x6e0, 0x400, 0x3c0, 0x3c0}, + {0x710, 0x710, 0x7a0, 0x7f0, 0x820, 0x830, 0x840, 0x850, 0x850, 0x860, + 0x860, 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880, + 0x890, 0x890, 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8e0, 0x8f0, + 0x900, 0x910, 0x910, 0x910, 0x910, 0x900, 0x8e0, 0x8b0, 0x870, 0x820, + 0x7e0, 0x7b0, 0x760, 0x770, 0x7a0, 0x7c0, 0x780, 0x5d0, 0x3c0, 0x3c0}, + {0x680, 0x680, 0x750, 0x7b0, 0x7e0, 0x810, 0x820, 0x830, 0x840, 0x850, + 0x850, 0x850, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, + 0x870, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880, 0x890, 0x8a0, 0x8b0, + 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900, 0x910, 0x910, 0x910, 0x900, 0x8f0, + 0x8d0, 0x8b0, 0x840, 0x7f0, 0x790, 0x760, 0x7a0, 0x7c0, 0x7b0, 0x720} +}; + +static int8_t baptab[305] = { + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, // 93 padding entries + + 15, 15, 15, 15, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, + 14, 13, 13, 13, 13, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, + 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, + 6, 5, 5, 4, 4, 3, 3, 3, 2, 2, 1, 1, 1, 1, 1, 0, + + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 // 148 padding entries +}; + +static int bndtab[30] = {21, 22, 23, 24, 25, 26, 27, 28, 31, 34, + 37, 40, 43, 46, 49, 55, 61, 67, 73, 79, + 85, 97, 109, 121, 133, 157, 181, 205, 229, 253}; + +static int8_t latab[256] = { + -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, + -52, -52, -51, -50, -49, -48, -47, -47, -46, -45, -44, -44, + -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, + -35, -34, -33, -33, -32, -32, -31, -30, -30, -29, -29, -28, + -28, -27, -27, -26, -26, -25, -25, -24, -24, -23, -23, -22, + -22, -21, -21, -21, -20, -20, -19, -19, -19, -18, -18, -18, + -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, + -13, -13, -13, -13, -12, -12, -12, -12, -11, -11, -11, -11, + -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -8, -8, + -8, -8, -8, -8, -7, -7, -7, -7, -7, -7, -6, -6, + -6, -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, -5, + -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, + -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, + -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, + -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 +}; + +#define UPDATE_LEAK() \ +do { \ + fastleak += fdecay; \ + if (fastleak > psd + fgain) \ + fastleak = psd + fgain; \ + slowleak += sdecay; \ + if (slowleak > psd + sgain) \ + slowleak = psd + sgain; \ +} while (0) + +#define COMPUTE_MASK() \ +do { \ + if (psd > dbknee) \ + mask -= (psd - dbknee) >> 2; \ + if (mask > hth[i]) \ + mask = hth[i]; \ + if (deltba != NULL) \ + mask -= 128 * deltba[i]; \ + mask -= snroffset; \ + mask = (mask > 0) ? 0 : ((-mask) >> 5); \ + mask -= floor; \ +} while (0) + +void bit_allocate(int fscod, audblk_t * audblk, ac3_ba_t * ba, + int bndstart, int start, int end, int fastleak, int slowleak, + uint8_t * exp, int8_t * bap) { - return (a < b ? a : b); -} + static int slowgain[4] = {0x540, 0x4d8, 0x478, 0x410}; + static int dbpbtab[4] = {0xc00, 0x500, 0x300, 0x100}; + static int floortab[8] = {0x910, 0x950, 0x990, 0x9d0, + 0xa10, 0xa90, 0xb10, 0x1400}; -void bit_allocate(int fscod, audblk_t * audblk, ac3_ba_t * ba, uint16_t start, - uint16_t end, int16_t fastleak, int16_t slowleak, - uint8_t * exp, uint16_t * bap, int is_lfe) -{ - int16_t fgain; - uint16_t snroffset; int i, j; - int bndstrt; - int lowcomp; - - /* Do some setup before we do the bit alloc */ - sdecay = slowdec[audblk->sdcycod]; - fdecay = fastdec[audblk->fdcycod]; + int fdecay, fgain, sdecay, sgain, dbknee, floor, snroffset; + int psd, mask; + int8_t * deltba; + int * hth; + + fdecay = 63 + 20 * audblk->fdcycod; + fgain = 128 + 128 * ba->fgaincod; + sdecay = 15 + 2 * audblk->sdcycod; sgain = slowgain[audblk->sgaincod]; dbknee = dbpbtab[audblk->dbpbcod]; + hth = hthtab[fscod]; + deltba = (ba->deltbae == DELTA_BIT_NONE) ? NULL : ba->deltba; floor = floortab[audblk->floorcod]; + snroffset = 960 - 64 * audblk->csnroffst - 4 * ba->fsnroffst + floor; + floor >>= 5; - fgain = fastgain[ba->fgaincod]; - snroffset = 64 * audblk->csnroffst + 4 * ba->fsnroffst - 960; - - i = start; - j = bndstrt = masktab[start]; - do { - int psd, lastbin; - - lastbin = min (bndtab[j] + bndsz[j], end); - psd = 128 * exp[i++]; - while (i < lastbin) { - int next, delta; - - next = 128 * exp[i++]; - delta = next - psd; - switch (delta >> 9) { - case -6: case -5: case -4: case -3: case -2: - psd = next; - break; - case -1: - psd = next - latab[(-delta) >> 1]; - break; - case 0: - psd -= latab[delta >> 1]; - break; - } - } - bndpsd[j++] = 3072 - psd; - } while (i < end); - - // j = bndend - j--; + i = bndstart; + j = start; + if (start == 0) { // not the coupling channel + int lowcomp; - i = bndstrt; - lowcomp = 0; - if (i == 0) { // not the coupling channel + lowcomp = 0; + j = end - 1; do { if (i < j) { - if (bndpsd[i+1] == bndpsd[i] + 256) + if (exp[i+1] == exp[i] - 2) lowcomp = 384; - else if (lowcomp && (bndpsd[i+1] < bndpsd[i])) + else if (lowcomp && (exp[i+1] > exp[i])) lowcomp -= 64; } - excite[i++] = bndpsd[i] - fgain - lowcomp; - } while ((i < 3) || ((i < 7) && (bndpsd[i] < bndpsd[i-1]))); - fastleak = bndpsd[i-1] - fgain; - slowleak = bndpsd[i-1] - sgain; + psd = 128 * exp[i]; + mask = psd + fgain + lowcomp; + COMPUTE_MASK (); + bap[i++] = (baptab+156)[mask + 4 * exp[i]]; + } while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1]))); + fastleak = psd + fgain; + slowleak = psd + sgain; while (i < 7) { if (i < j) { - if (bndpsd[i+1] == bndpsd[i] + 256) + if (exp[i+1] == exp[i] - 2) lowcomp = 384; - else if (lowcomp && (bndpsd[i+1] < bndpsd[i])) + else if (lowcomp && (exp[i+1] > exp[i])) lowcomp -= 64; } - fastleak -= fdecay; - if (fastleak < bndpsd[i] - fgain) - fastleak = bndpsd[i] - fgain; - slowleak -= sdecay; - if (slowleak < bndpsd[i] - sgain) - slowleak = bndpsd[i] - sgain; - excite[i++] = ((fastleak - lowcomp > slowleak) ? - fastleak - lowcomp : slowleak); + psd = 128 * exp[i]; + UPDATE_LEAK (); + mask = ((fastleak + lowcomp < slowleak) ? + fastleak + lowcomp : slowleak); + COMPUTE_MASK (); + bap[i++] = (baptab+156)[mask + 4 * exp[i]]; } - if (i > j) // lfe channel - goto done_excite; + + if (end == 7) // lfe channel + return; do { - if (bndpsd[i+1] == bndpsd[i] + 256) + if (exp[i+1] == exp[i] - 2) lowcomp = 320; - else if (lowcomp && (bndpsd[i+1] < bndpsd[i])) + else if (lowcomp && (exp[i+1] > exp[i])) lowcomp -= 64; - fastleak -= fdecay; - if (fastleak < bndpsd[i] - fgain) - fastleak = bndpsd[i] - fgain; - slowleak -= sdecay; - if (slowleak < bndpsd[i] - sgain) - slowleak = bndpsd[i] - sgain; - excite[i++] = ((fastleak - lowcomp > slowleak) ? - fastleak - lowcomp : slowleak); + psd = 128 * exp[i]; + UPDATE_LEAK (); + mask = ((fastleak + lowcomp < slowleak) ? + fastleak + lowcomp : slowleak); + COMPUTE_MASK (); + bap[i++] = (baptab+156)[mask + 4 * exp[i]]; } while (i < 20); while (lowcomp > 128) { // two iterations maximum lowcomp -= 128; - fastleak -= fdecay; - if (fastleak < bndpsd[i] - fgain) - fastleak = bndpsd[i] - fgain; - slowleak -= sdecay; - if (slowleak < bndpsd[i] - sgain) - slowleak = bndpsd[i] - sgain; - excite[i++] = ((fastleak - lowcomp > slowleak) ? - fastleak - lowcomp : slowleak); + psd = 128 * exp[i]; + UPDATE_LEAK (); + mask = ((fastleak + lowcomp < slowleak) ? + fastleak + lowcomp : slowleak); + COMPUTE_MASK (); + bap[i++] = (baptab+156)[mask + 4 * exp[i]]; } + j = i; } do { - fastleak -= fdecay; - if (fastleak < bndpsd[i] - fgain) - fastleak = bndpsd[i] - fgain; - slowleak -= sdecay; - if (slowleak < bndpsd[i] - sgain) - slowleak = bndpsd[i] - sgain; - excite[i++] = (fastleak > slowleak) ? fastleak : slowleak; - } while (i <= j); - -done_excite: - - ba_compute_mask(start, end, fscod, ba->deltbae, ba->deltnseg, - ba->deltoffst, ba->deltba, ba->deltlen, excite, mask); - ba_compute_bap(start, end, snroffset, exp, mask, bap); -} - - -static void ba_compute_mask(int16_t start, int16_t end, uint16_t fscod, - uint16_t deltbae, uint16_t deltnseg, - uint16_t deltoffst[], uint16_t deltba[], - uint16_t deltlen[], int16_t excite[], - int16_t mask[]) -{ - int bin,k; - int16_t bndstrt; - int16_t bndend; - int16_t delta; - - bndstrt = masktab[start]; - bndend = masktab[end - 1] + 1; - - /* Compute the masking curve */ - - for (bin = bndstrt; bin < bndend; bin++) { - if (bndpsd[bin] < dbknee) - excite[bin] += ((dbknee - bndpsd[bin]) >> 2); - mask[bin] = max(excite[bin], hth[fscod][bin]); - } - - /* Perform delta bit modulation if necessary */ - if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) { - int16_t band = 0; - int16_t seg = 0; - - for (seg = 0; seg < deltnseg+1; seg++) { - band += deltoffst[seg]; - if (deltba[seg] >= 4) - delta = (deltba[seg] - 3) << 7; - else - delta = (deltba[seg] - 4) << 7; - - for (k = 0; k < deltlen[seg]; k++) { - mask[band] += delta; - band++; - } - } - } -} - -static void ba_compute_bap(int16_t start, int16_t end, int16_t snroffset, - int8_t exps[], int16_t mask[], int16_t bap[]) -{ - int i,j,k; - int16_t lastbin = 0; - int16_t address = 0; + int startband, endband; - /* Compute the bit allocation pointer for each bin */ - i = start; - j = masktab[start]; - - do { - lastbin = min(bndtab[j] + bndsz[j], end); - mask[j] -= snroffset; - mask[j] -= floor; - - if (mask[j] < 0) - mask[j] = 0; - - mask[j] &= 0x1fe0; - mask[j] += floor; - for (k = i; k < lastbin; k++) { - address = (3072 - 128 * exps[i] - mask[j]) >> 5; - address = min(63, max(0, address)); - bap[i] = baptab[address]; - i++; - } - j++; - } while (end > lastbin); -} + startband = j; + endband = ((bndtab-20)[i] < end) ? (bndtab-20)[i] : end; + psd = 128 * exp[j++]; + while (j < endband) { + int next, delta; -static int16_t calc_lowcomp(int16_t a,int16_t b0,int16_t b1,int16_t bin) -{ - if (bin < 7) { - if ((b0 + 256) == b1) - a = 384; - else if (b0 > b1) - a = max(0, a - 64); - } else if (bin < 20) { - if ((b0 + 256) == b1) - a = 320; - else if (b0 > b1) - a = max(0, a - 64) ; - } else - a = max(0, a - 128); - - return(a); + next = 128 * exp[j++]; + delta = next - psd; + switch (delta >> 9) { + case -6: case -5: case -4: case -3: case -2: + psd = next; + break; + case -1: + psd = next + latab[(-delta) >> 1]; + break; + case 0: + psd += latab[delta >> 1]; + break; + } + } + // minpsd = -289 + UPDATE_LEAK (); + mask = (fastleak < slowleak) ? fastleak : slowleak; + COMPUTE_MASK (); + i++; + j = startband; + do { + // max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp + // min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5 + bap[j++] = (baptab+156)[mask + 4 * exp[j]]; + } while (j < endband); + } while (j < end); } - diff --git a/src/libac3/bit_allocate.h b/src/libac3/bit_allocate.h index cdf73414a..089acc56c 100644 --- a/src/libac3/bit_allocate.h +++ b/src/libac3/bit_allocate.h @@ -21,6 +21,6 @@ * */ -void bit_allocate(int fscod, audblk_t * audblk, ac3_ba_t * ba, uint16_t start, - uint16_t end, int16_t fastleak, int16_t slowleak, - uint8_t * exp, uint16_t * bap, int ls_lfe); +void bit_allocate(int fscod, audblk_t * audblk, ac3_ba_t * ba, + int bndstart, int start, int end, int fastleak, int slowleak, + uint8_t * exp, int8_t * bap); diff --git a/src/libac3/parse.c b/src/libac3/parse.c index 0596eaa49..98b8cb6ac 100644 --- a/src/libac3/parse.c +++ b/src/libac3/parse.c @@ -24,7 +24,6 @@ #include #include -#include #include "ac3.h" #include "ac3_internal.h" @@ -185,6 +184,30 @@ static int parse_exponents (int expstr, int ngrps, uint8_t exponent, return 0; } +static int parse_deltba (int8_t * deltba) +{ + int deltnseg, deltlen, delta, j; + + memset (deltba, 0, 50); + + deltnseg = bitstream_get (3); + j = 0; + do { + j += bitstream_get (5); + deltlen = bitstream_get (4); + delta = bitstream_get (3); + delta -= (delta >= 4) ? 3 : 4; + if (!deltlen) + continue; + if (j + deltlen >= 50) + return 1; + while (deltlen--) + deltba[j++] = delta; + } while (deltnseg--); + + return 0; +} + static inline int zero_snr_offsets (ac3_state_t * state, audblk_t * audblk) { int i; @@ -284,7 +307,7 @@ int parse_audblk (ac3_state_t * state, audblk_t * audblk) audblk->endmant[i] = audblk->cplstrtmant; else { audblk->chbwcod[i] = bitstream_get (6); - audblk->endmant[i] = ((audblk->chbwcod[i] + 12) * 3) + 37; + audblk->endmant[i] = audblk->chbwcod[i] * 3 + 73; } } @@ -358,25 +381,13 @@ int parse_audblk (ac3_state_t * state, audblk_t * audblk) audblk->cplba.deltbae = bitstream_get (2); for (i = 0; i < state->nfchans; i++) audblk->ba[i].deltbae = bitstream_get (2); - if (audblk->cplinu && (audblk->cplba.deltbae == DELTA_BIT_NEW)) { - audblk->cplba.deltnseg = bitstream_get (3); - for (i = 0; i < audblk->cplba.deltnseg + 1; i++) { - audblk->cplba.deltoffst[i] = bitstream_get (5); - audblk->cplba.deltlen[i] = bitstream_get (4); - audblk->cplba.deltba[i] = bitstream_get (3); - } - } - for (i = 0; i < state->nfchans; i++) { - if (audblk->ba[i].deltbae == DELTA_BIT_NEW) { - int j; - audblk->ba[i].deltnseg = bitstream_get (3); - for (j = 0; j < audblk->ba[i].deltnseg + 1; j++) { - audblk->ba[i].deltoffst[j] = bitstream_get (5); - audblk->ba[i].deltlen[j] = bitstream_get (4); - audblk->ba[i].deltba[j] = bitstream_get (3); - } - } - } + if (audblk->cplinu && (audblk->cplba.deltbae == DELTA_BIT_NEW) && + parse_deltba (audblk->cplba.deltba)) + return 1; + for (i = 0; i < state->nfchans; i++) + if ((audblk->ba[i].deltbae == DELTA_BIT_NEW) && + parse_deltba (audblk->ba[i].deltba)) + return 1; } if (do_bit_alloc) { @@ -385,20 +396,24 @@ int parse_audblk (ac3_state_t * state, audblk_t * audblk) memset (audblk->fbw_bap, 0, sizeof (audblk->fbw_bap)); memset (audblk->lfe_bap, 0, sizeof (audblk->lfe_bap)); } else { + static int bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44, + 45, 45, 46, 46, 47, 47, 48, 48}; + if (audblk->cplinu) bit_allocate (state->fscod, audblk, &audblk->cplba, - audblk->cplstrtmant, audblk->cplendmant, - (audblk->cplfleak << 8) + 768, - (audblk->cplsleak << 8) + 768, - audblk->cpl_exp, audblk->cpl_bap, 0); + bndtab[audblk->cplbegf], audblk->cplstrtmant, + audblk->cplendmant, + 2304 - (audblk->cplfleak << 8), + 2304 - (audblk->cplsleak << 8), + audblk->cpl_exp, audblk->cpl_bap); for (i = 0; i < state->nfchans; i++) - bit_allocate (state->fscod, audblk, audblk->ba + i, 0, + bit_allocate (state->fscod, audblk, audblk->ba + i, 0, 0, audblk->endmant[i], 0, 0, - audblk->fbw_exp[i], audblk->fbw_bap[i], 0); + audblk->fbw_exp[i], audblk->fbw_bap[i]); if (state->lfeon) { audblk->lfeba.deltbae = DELTA_BIT_NONE; - bit_allocate (state->fscod, audblk, &audblk->lfeba, 0, 7, 0, 0, - audblk->lfe_exp, audblk->lfe_bap, 1); + bit_allocate (state->fscod, audblk, &audblk->lfeba, 0, 0, 7, + 0, 0, audblk->lfe_exp, audblk->lfe_bap); } } } -- cgit v1.2.3