Mercurial > libavcodec.hg
comparison ac3dec.c @ 5304:eff51058fe13 libavcodec
AC-3 decoder, soc revision 26, Jul 5 04:55:15 2006 UTC by cloud9
Mersenne Twister
Dynamic Range
Downmixing
IMDCT
| author | jbr |
|---|---|
| date | Sat, 14 Jul 2007 15:41:27 +0000 |
| parents | b8821ff5c30d |
| children | 5892b4a6380b |
comparison
equal
deleted
inserted
replaced
| 5303:b8821ff5c30d | 5304:eff51058fe13 |
|---|---|
| 22 #include <math.h> | 22 #include <math.h> |
| 23 #include <inttypes.h> | 23 #include <inttypes.h> |
| 24 #include <string.h> | 24 #include <string.h> |
| 25 | 25 |
| 26 #define ALT_BITSTREAM_READER | 26 #define ALT_BITSTREAM_READER |
| 27 | |
| 28 #include "ac3.h" | |
| 29 #include "ac3tab.h" | |
| 27 #include "ac3_decoder.h" | 30 #include "ac3_decoder.h" |
| 28 #include "avcodec.h" | 31 #include "avcodec.h" |
| 29 #include "bitstream.h" | 32 #include "bitstream.h" |
| 30 #include "dsputil.h" | 33 #include "dsputil.h" |
| 31 #include "avutil.h" | 34 #include "avutil.h" |
| 32 | 35 #include "common.h" |
| 33 static const int sampling_rates[3] = { 32000, 44100, 48000 }; | 36 |
| 34 | 37 /* Synchronization information. */ |
| 35 static const struct | 38 typedef struct { |
| 36 { | 39 uint16_t sync_word; //synchronization word = always 0x0b77 |
| 37 int bit_rate; | 40 uint16_t crc1; //crc for the first 5/8 of the frame |
| 38 int frame_sizes[3]; | 41 uint8_t fscod; //sampling rate code |
| 39 } frame_size_table[38] = { | 42 uint8_t frmsizecod; //frame size code |
| 40 { 32, { 96, 69, 64 } }, | 43 |
| 41 { 32, { 96, 70, 64 } }, | 44 /* Derived Attributes */ |
| 42 { 40, { 120, 87, 80 } }, | 45 int sampling_rate; //sampling rate - 48, 44.1 or 32 kHz (value in Hz) |
| 43 { 40, { 120, 88, 80 } }, | 46 int bit_rate; //nominal bit rate (value in kbps) |
| 44 { 48, { 144, 104, 96 } }, | 47 } ac3_sync_info; |
| 45 { 48, { 144, 105, 96 } }, | 48 |
| 46 { 56, { 168, 121, 112 } }, | 49 /* flags for the BSI. */ |
| 47 { 56, { 168, 122, 112 } }, | 50 #define AC3_BSI_LFEON 0x00000001 //low frequency effects channel on |
| 48 { 64, { 192, 139, 128 } }, | 51 #define AC3_BSI_COMPRE 0x00000002 //compression exists |
| 49 { 64, { 192, 140, 128 } }, | 52 #define AC3_BSI_LANGCODE 0x00000004 //langcode exists |
| 50 { 80, { 240, 174, 160 } }, | 53 #define AC3_BSI_AUDPRODIE 0x00000008 //audio production information exists |
| 51 { 80, { 240, 175, 160 } }, | 54 #define AC3_BSI_COMPR2E 0x00000010 //compr2 exists |
| 52 { 96, { 288, 208, 192 } }, | 55 #define AC3_BSI_LANGCOD2E 0x00000020 //langcod2 exists |
| 53 { 96, { 288, 209, 192 } }, | 56 #define AC3_BSI_AUDPRODI2E 0x00000040 //audio production information 2 exists |
| 54 { 112, { 336, 243, 224 } }, | 57 #define AC3_BSI_COPYRIGHTB 0x00000080 //copyright |
| 55 { 112, { 336, 244, 224 } }, | 58 #define AC3_BSI_ORIGBS 0x00000100 //original bit stream |
| 56 { 128, { 384, 278, 256 } }, | 59 #define AC3_BSI_TIMECOD1E 0x00000200 //timecod1 exists |
| 57 { 128, { 384, 279, 256 } }, | 60 #define AC3_BSI_TIMECOD2E 0x00000400 //timecod2 exists |
| 58 { 160, { 480, 348, 320 } }, | 61 #define AC3_BSI_ADDBSIE 0x00000800 //additional bit stream information exists |
| 59 { 160, { 480, 349, 320 } }, | 62 |
| 60 { 192, { 576, 417, 384 } }, | 63 /* Bit Stream Information. */ |
| 61 { 192, { 576, 418, 384 } }, | 64 typedef struct { |
| 62 { 224, { 672, 487, 448 } }, | 65 uint32_t flags; |
| 63 { 224, { 672, 488, 448 } }, | 66 uint8_t bsid; //bit stream identification |
| 64 { 256, { 768, 557, 512 } }, | 67 uint8_t bsmod; //bit stream mode - type of service |
| 65 { 256, { 768, 558, 512 } }, | 68 uint8_t acmod; //audio coding mode - which channels are in use |
| 66 { 320, { 960, 696, 640 } }, | 69 uint8_t cmixlev; //center mix level |
| 67 { 320, { 960, 697, 640 } }, | 70 uint8_t surmixlev; //surround mix level |
| 68 { 384, { 1152, 835, 768 } }, | 71 uint8_t dsurmod; //dynamic surround encoded |
| 69 { 384, { 1152, 836, 768 } }, | 72 uint8_t dialnorm; //dialog normalization |
| 70 { 448, { 1344, 975, 896 } }, | 73 uint8_t compr; //compression gain word |
| 71 { 448, { 1344, 976, 896 } }, | 74 uint8_t langcod; //language code |
| 72 { 512, { 1536, 1114, 1024 } }, | 75 uint8_t mixlevel; //mixing level |
| 73 { 512, { 1536, 1115, 1024 } }, | 76 uint8_t roomtyp; //room type |
| 74 { 576, { 1728, 1253, 1152 } }, | 77 uint8_t dialnorm2; //dialogue normalization for 1+1 mode |
| 75 { 576, { 1728, 1254, 1152 } }, | 78 uint8_t compr2; //compression gain word for 1+1 mode |
| 76 { 640, { 1920, 1393, 1280 } } | 79 uint8_t langcod2; //language code for 1+1 mode |
| 77 }; | 80 uint8_t mixlevel2; //mixing level for 1+1 mode |
| 78 | 81 uint8_t roomtyp2; //room type for 1+1 mode |
| 79 static int | 82 uint16_t timecod1; //timecode 1 |
| 80 ac3_decode_init (AVCodecContext * avctx) | 83 uint16_t timecod2; //timecode 2 |
| 81 { | 84 uint8_t addbsil; //additional bit stream information length |
| 82 AC3DecodeContext *ctx = avctx->priv_data; | 85 |
| 83 | 86 /* Dervied Attributes */ |
| 84 ff_mdct_init (&ctx->mdct_ctx_256, 8, 1); | 87 int nfchans; //number of full bandwidth channels - derived from acmod |
| 85 ff_mdct_init (&ctx->mdct_ctx_512, 9, 1); | 88 } ac3_bsi; |
| 86 ctx->samples = av_mallocz (6 * 6 * 256 * sizeof (float)); | 89 |
| 87 if (!(ctx->samples)) | 90 /* #defs relevant to Audio Block. */ |
| 91 #define MAX_FBW_CHANNELS 5 //maximum full bandwidth channels | |
| 92 #define NUM_LFE_GROUPS 3 //number of LFE Groups | |
| 93 #define MAX_NUM_SEGS 8 //maximum number of segments per delta bit allocation | |
| 94 #define NUM_LFE_MANTS 7 //number of lfe mantissas | |
| 95 #define MAX_CPL_SUBNDS 18 //maximum number of coupling sub bands | |
| 96 #define MAX_CPL_BNDS 18 //maximum number of coupling bands | |
| 97 #define MAX_CPL_GRPS 253 //maximum number of coupling groups | |
| 98 #define MAX_CHNL_GRPS 88 //maximum number of channel groups | |
| 99 #define MAX_NUM_MANTISSAS 256 //maximum number of mantissas | |
| 100 | |
| 101 /* flags for the Audio Block. */ | |
| 102 #define AC3_AB_DYNRNGE 0x00000001 //dynamic range control exists | |
| 103 #define AC3_AB_DYNRNG2E 0x00000002 //dynamic range control 2 exists | |
| 104 #define AC3_AB_CPLSTRE 0x00000004 //coupling strategy exists | |
| 105 #define AC3_AB_CPLINU 0x00000008 //coupling in use | |
| 106 #define AC3_AB_PHSFLGINU 0x00000010 //phase flag in use | |
| 107 #define AC3_AB_REMATSTR 0x00000020 //rematrixing required | |
| 108 #define AC3_AB_LFEEXPSTR 0x00000100 //lfe exponent strategy | |
| 109 #define AC3_AB_BAIE 0x00000200 //bit allocation information exists | |
| 110 #define AC3_AB_SNROFFSTE 0x00000400 //SNR offset exists | |
| 111 #define AC3_AB_CPLLEAKE 0x00000800 //coupling leak initialization exists | |
| 112 #define AC3_AB_DELTBAIE 0x00001000 //delta bit allocation information exists | |
| 113 #define AC3_AB_SKIPLE 0x00002000 //skip length exists | |
| 114 | |
| 115 /* Exponent strategies. */ | |
| 116 #define AC3_EXPSTR_D15 0x01 | |
| 117 #define AC3_EXPSTR_D25 0x02 | |
| 118 #define AC3_EXPSTR_D45 0x03 | |
| 119 #define AC3_EXPSTR_REUSE 0x00 | |
| 120 | |
| 121 /* Bit allocation strategies */ | |
| 122 #define AC3_DBASTR_NEW 0x01 | |
| 123 #define AC3_DBASTR_NONE 0x02 | |
| 124 #define AC3_DBASTR_RESERVED 0x03 | |
| 125 #define AC3_DBASTR_REUSE 0x00 | |
| 126 | |
| 127 /* Audio Block */ | |
| 128 typedef struct { | |
| 129 uint32_t flags; | |
| 130 uint8_t blksw; //block switch flags for channels in use | |
| 131 uint8_t dithflag; //dithering flags for channels in use | |
| 132 int8_t dynrng; //dynamic range word | |
| 133 int8_t dynrng2; //dynamic range word for 1+1 mode | |
| 134 uint8_t chincpl; //channel in coupling flags for channels in use | |
| 135 uint8_t cplbegf; //coupling begin frequency code | |
| 136 uint8_t cplendf; //coupling end frequency code | |
| 137 uint32_t cplbndstrc; //coupling band structure | |
| 138 uint8_t cplcoe; //coupling co-ordinates exists for the channel in use | |
| 139 uint8_t mstrcplco[5]; //master coupling co-ordinate for channels in use | |
| 140 uint8_t cplcoexp[5][18]; //coupling co-ordinate exponenets | |
| 141 uint8_t cplcomant[5][18]; //coupling co-ordinate mantissas | |
| 142 uint32_t phsflg; //phase flag per band | |
| 143 uint8_t rematflg; //rematrixing flag | |
| 144 uint8_t cplexpstr; //coupling exponent strategy | |
| 145 uint8_t chexpstr[5]; //channel exponent strategy | |
| 146 uint8_t lfeexpstr; //lfe exponent strategy | |
| 147 uint8_t chbwcod[5]; //channel bandwdith code for channels in use | |
| 148 uint8_t cplabsexp; //coupling absolute exponent | |
| 149 uint8_t cplexps[72]; //coupling exponents | |
| 150 uint8_t exps[5][88]; //channel exponents | |
| 151 uint8_t gainrng[5]; //gain range | |
| 152 uint8_t lfeexps[3]; //LFE exponents | |
| 153 uint8_t sdcycod; //slow decay code | |
| 154 uint8_t fdcycod; //fast decay code | |
| 155 uint8_t sgaincod; //slow gain code | |
| 156 uint8_t dbpbcod; //dB per bit code | |
| 157 uint8_t floorcod; //masking floor code | |
| 158 uint8_t csnroffst; //coarse SNR offset | |
| 159 uint8_t cplfsnroffst; //coupling fine SNR offset | |
| 160 uint8_t cplfgaincod; //coupling fast gain code | |
| 161 uint8_t fsnroffst[5]; //fine SNR offset for channels in use | |
| 162 uint8_t fgaincod[5]; //fast gain code for channels in use | |
| 163 uint8_t lfefsnroffst; //lfe fine SNR offset | |
| 164 uint8_t lfefgaincod; //lfe fast gain code | |
| 165 uint8_t cplfleak; //coupling fast leak initialization value | |
| 166 uint8_t cplsleak; //coupling slow leak initialization value | |
| 167 uint8_t cpldeltbae; //coupling delta bit allocation exists | |
| 168 uint8_t deltbae[5]; //delta bit allocation exists for channels in use | |
| 169 uint8_t cpldeltnseg; //coupling delta bit allocation number of segments | |
| 170 uint8_t cpldeltoffst[8]; //coupling delta offset | |
| 171 uint8_t cpldeltlen[8]; //coupling delta len | |
| 172 uint8_t cpldeltba[8]; //coupling delta bit allocation | |
| 173 uint8_t deltnseg[5]; //delta bit allocation number of segments per channel | |
| 174 uint8_t deltoffst[5][8]; //delta offset for channels in use | |
| 175 uint8_t deltlen[5][8]; //delta len for channels in use | |
| 176 uint8_t deltba[5][8]; //delta bit allocation | |
| 177 uint16_t skipl; //skip length | |
| 178 | |
| 179 /* Derived Attributes */ | |
| 180 int ncplsubnd; //number of active coupling sub bands = 3 + cplendf - cplbegf | |
| 181 int ncplbnd; //derived from ncplsubnd and cplbndstrc | |
| 182 int ncplgrps; //derived from ncplsubnd, cplexpstr | |
| 183 int nchgrps[5]; //derived from chexpstr, and cplbegf or chbwcod | |
| 184 int nchmant[5]; //derived from cplbegf or chbwcod | |
| 185 int ncplmant; //derived from ncplsubnd = 12 * ncplsubnd | |
| 186 | |
| 187 uint8_t cplstrtbnd; //coupling start band for bit allocation | |
| 188 uint8_t cplstrtmant; //coupling start mantissa | |
| 189 uint8_t cplendmant; //coupling end mantissa | |
| 190 uint8_t endmant[5]; //channel end mantissas | |
| 191 | |
| 192 uint8_t dcplexps[256]; //decoded coupling exponents | |
| 193 uint8_t dexps[5][256]; //decoded fbw channel exponents | |
| 194 uint8_t dlfeexps[256]; //decoded lfe exponents | |
| 195 uint8_t cplbap[256]; //coupling bit allocation parameters table | |
| 196 uint8_t bap[5][256]; //fbw channels bit allocation parameters table | |
| 197 uint8_t lfebap[256]; //lfe bit allocaiton parameters table | |
| 198 | |
| 199 float cplcoeffs[256]; //temporary storage for coupling transform coefficients | |
| 200 float cplco[5][18]; //coupling co-ordinates | |
| 201 float chcoeffs[6]; //channel coefficients for downmix | |
| 202 } ac3_audio_block; | |
| 203 | |
| 204 | |
| 205 | |
| 206 #define AC3_OUTPUT_UNMODIFIED 0x00 | |
| 207 #define AC3_OUTPUT_MONO 0x01 | |
| 208 #define AC3_OUTPUT_STEREO 0x02 | |
| 209 #define AC3_OUTPUT_DOLBY 0x03 | |
| 210 | |
| 211 #define AC3_INPUT_DUALMONO 0x00 | |
| 212 #define AC3_INPUT_MONO 0x01 | |
| 213 #define AC3_INPUT_STEREO 0x02 | |
| 214 #define AC3_INPUT_3F 0x03 | |
| 215 #define AC3_INPUT_2F_1R 0x04 | |
| 216 #define AC3_INPUT_3F_1R 0x05 | |
| 217 #define AC3_INPUT_2F_2R 0x06 | |
| 218 #define AC3_INPUT_3F_2R 0x07 | |
| 219 | |
| 220 /* BEGIN Mersenne Twister Code. */ | |
| 221 #define N 624 | |
| 222 #define M 397 | |
| 223 #define MATRIX_A 0x9908b0df | |
| 224 #define UPPER_MASK 0x80000000 | |
| 225 #define LOWER_MASK 0x7fffffff | |
| 226 | |
| 227 typedef struct { | |
| 228 uint32_t mt[N]; | |
| 229 int mti; | |
| 230 } dither_state; | |
| 231 | |
| 232 static void dither_seed(dither_state *state, uint32_t seed) | |
| 233 { | |
| 234 if (seed == 0) | |
| 235 seed = 0x1f2e3d4c; | |
| 236 | |
| 237 state->mt[0] = seed; | |
| 238 for (state->mti = 1; state->mti < N; state->mti++) | |
| 239 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1); | |
| 240 } | |
| 241 | |
| 242 static uint32_t dither_uint32(dither_state *state) | |
| 243 { | |
| 244 uint32_t y; | |
| 245 static const uint32_t mag01[2] = { 0x00, MATRIX_A }; | |
| 246 int kk; | |
| 247 | |
| 248 if (state->mti >= N) { | |
| 249 for (kk = 0; kk < N - M; kk++) { | |
| 250 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
| 251 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x01]; | |
| 252 } | |
| 253 for (;kk < N - 1; kk++) { | |
| 254 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); | |
| 255 state->mt[kk] = state->mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x01]; | |
| 256 } | |
| 257 y = (state->mt[N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK); | |
| 258 state->mt[N - 1] = state->mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x01]; | |
| 259 | |
| 260 state->mti = 0; | |
| 261 } | |
| 262 | |
| 263 y = state->mt[state->mti++]; | |
| 264 y ^= (y >> 11); | |
| 265 y ^= ((y << 7) & 0x9d2c5680); | |
| 266 y ^= ((y << 15) & 0xefc60000); | |
| 267 y ^= (y >> 18); | |
| 268 | |
| 269 return y; | |
| 270 } | |
| 271 | |
| 272 static inline int16_t dither_int16(dither_state *state) | |
| 273 { | |
| 274 return ((dither_uint32(state) << 16) >> 16); | |
| 275 } | |
| 276 | |
| 277 /* END Mersenne Twister */ | |
| 278 | |
| 279 /* AC3 Context. */ | |
| 280 typedef struct { | |
| 281 ac3_sync_info sync_info; | |
| 282 ac3_bsi bsi; | |
| 283 ac3_audio_block audio_block; | |
| 284 float *samples; | |
| 285 int output; | |
| 286 dither_state state; | |
| 287 MDCTContext imdct_ctx_256; | |
| 288 MDCTContext imdct_ctx_512; | |
| 289 GetBitContext gb; | |
| 290 } AC3DecodeContext; | |
| 291 | |
| 292 | |
| 293 static int ac3_decode_init(AVCodecContext *avctx) | |
| 294 { | |
| 295 AC3DecodeContext *ctx = avctx->priv_data; | |
| 296 | |
| 297 ac3_common_init(); | |
| 298 | |
| 299 ff_mdct_init(&ctx->imdct_ctx_256, 8, 1); | |
| 300 ff_mdct_init(&ctx->imdct_ctx_512, 9, 1); | |
| 301 ctx->samples = av_mallocz(6 * 256 * sizeof (float)); | |
| 302 if (!ctx->samples) { | |
| 303 av_log(avctx, AV_LOG_ERROR, "Cannot allocate memory for samples\n"); | |
| 304 return -1; | |
| 305 } | |
| 306 dither_seed(&ctx->state, 0); | |
| 307 | |
| 308 return 0; | |
| 309 } | |
| 310 | |
| 311 static int ac3_synchronize(uint8_t *buf, int buf_size) | |
| 312 { | |
| 313 int i; | |
| 314 | |
| 315 for (i = 0; i < buf_size - 1; i++) | |
| 316 if (buf[i] == 0x0b && buf[i + 1] == 0x77) | |
| 317 return i; | |
| 318 | |
| 88 return -1; | 319 return -1; |
| 89 | |
| 90 return 0; | |
| 91 } | |
| 92 | |
| 93 static int | |
| 94 ac3_synchronize (uint8_t * buf, int buf_size) | |
| 95 { | |
| 96 int i; | |
| 97 | |
| 98 for (i = 0; i < buf_size - 1; i++) | |
| 99 if (buf[i] == 0x0b && buf[i + 1] == 0x77) | |
| 100 return i; | |
| 101 | |
| 102 return -1; | |
| 103 } | 320 } |
| 104 | 321 |
| 105 //Returns -1 when 'fscod' is not valid; | 322 //Returns -1 when 'fscod' is not valid; |
| 106 static int | 323 static int ac3_parse_sync_info(AC3DecodeContext *ctx) |
| 107 ac3_parse_sync_info (AC3DecodeContext * ctx) | 324 { |
| 108 { | 325 ac3_sync_info *sync_info = &ctx->sync_info; |
| 109 ac3_sync_info *sync_info = &ctx->sync_info; | 326 GetBitContext *gb = &ctx->gb; |
| 110 GetBitContext *gb = &ctx->gb; | 327 |
| 111 | 328 sync_info->sync_word = get_bits(gb, 16); |
| 112 sync_info->sync_word = get_bits_long (gb, 16); | 329 sync_info->crc1 = get_bits(gb, 16); |
| 113 sync_info->crc1 = get_bits_long (gb, 16); | 330 sync_info->fscod = get_bits(gb, 2); |
| 114 sync_info->fscod = get_bits_long (gb, 2); | 331 if (sync_info->fscod == 0x03) |
| 115 if (sync_info->fscod == 0x03) | 332 return -1; |
| 116 return -1; | 333 sync_info->frmsizecod = get_bits(gb, 6); |
| 117 sync_info->frmsizecod = get_bits_long (gb, 6); | 334 if (sync_info->frmsizecod >= 0x38) |
| 118 if (sync_info->frmsizecod >= 0x38) | 335 return -1; |
| 119 return -1; | 336 sync_info->sampling_rate = ac3_freqs[sync_info->fscod]; |
| 120 sync_info->sampling_rate = sampling_rates[sync_info->fscod]; | 337 sync_info->bit_rate = ac3_bitratetab[sync_info->frmsizecod >> 1]; |
| 121 sync_info->bit_rate = frame_size_table[sync_info->frmsizecod].bit_rate; | 338 |
| 122 sync_info->frame_size = frame_size_table[sync_info->frmsizecod].frame_sizes[sync_info->fscod]; | 339 return 0; |
| 123 | 340 } |
| 124 return 0; | |
| 125 } | |
| 126 | |
| 127 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 }; | |
| 128 | 341 |
| 129 //Returns -1 when | 342 //Returns -1 when |
| 130 static int | 343 static int ac3_parse_bsi(AC3DecodeContext *ctx) |
| 131 ac3_parse_bsi (AC3DecodeContext * ctx) | 344 { |
| 132 { | 345 ac3_bsi *bsi = &ctx->bsi; |
| 133 ac3_bsi *bsi = &ctx->bsi; | 346 uint32_t *flags = &bsi->flags; |
| 134 uint32_t *flags = &bsi->flags; | 347 GetBitContext *gb = &ctx->gb; |
| 135 GetBitContext *gb = &ctx->gb; | 348 |
| 136 | 349 *flags = 0; |
| 137 *flags = 0; | 350 bsi->cmixlev = 0; |
| 138 bsi->cmixlev = 0; | 351 bsi->surmixlev = 0; |
| 139 bsi->surmixlev = 0; | 352 bsi->dsurmod = 0; |
| 140 bsi->dsurmod = 0; | 353 |
| 141 | 354 bsi->bsid = get_bits(gb, 5); |
| 142 bsi->bsid = get_bits_long (gb, 5); | 355 if (bsi->bsid > 0x08) |
| 143 if (bsi->bsid > 0x08) | 356 return -1; |
| 144 return -1; | 357 bsi->bsmod = get_bits(gb, 3); |
| 145 bsi->bsmod = get_bits_long (gb, 3); | 358 bsi->acmod = get_bits(gb, 3); |
| 146 bsi->acmod = get_bits_long (gb, 3); | 359 if (bsi->acmod & 0x01 && bsi->acmod != 0x01) |
| 147 if (bsi->acmod & 0x01 && bsi->acmod != 0x01) | 360 bsi->cmixlev = get_bits(gb, 2); |
| 148 bsi->cmixlev = get_bits_long (gb, 2); | 361 if (bsi->acmod & 0x04) |
| 149 if (bsi->acmod & 0x04) | 362 bsi->surmixlev = get_bits(gb, 2); |
| 150 bsi->surmixlev = get_bits_long (gb, 2); | 363 if (bsi->acmod == 0x02) |
| 151 if (bsi->acmod == 0x02) | 364 bsi->dsurmod = get_bits(gb, 2); |
| 152 bsi->dsurmod = get_bits_long (gb, 2); | 365 if (get_bits(gb, 1)) |
| 153 if (get_bits_long (gb, 1)) | 366 *flags |= AC3_BSI_LFEON; |
| 154 *flags |= AC3_BSI_LFEON; | 367 bsi->dialnorm = get_bits(gb, 5); |
| 155 bsi->dialnorm = get_bits_long (gb, 5); | 368 if (get_bits(gb, 1)) { |
| 156 if (get_bits_long (gb, 1)) { | 369 *flags |= AC3_BSI_COMPRE; |
| 157 *flags |= AC3_BSI_COMPRE; | 370 bsi->compr = get_bits(gb, 5); |
| 158 bsi->compr = get_bits_long (gb, 5); | 371 } |
| 159 } | 372 if (get_bits(gb, 1)) { |
| 160 if (get_bits_long (gb, 1)) { | 373 *flags |= AC3_BSI_LANGCODE; |
| 161 *flags |= AC3_BSI_LANGCODE; | 374 bsi->langcod = get_bits(gb, 8); |
| 162 bsi->langcod = get_bits_long (gb, 8); | 375 } |
| 163 } | 376 if (get_bits(gb, 1)) { |
| 164 if (get_bits_long (gb, 1)) { | 377 *flags |= AC3_BSI_AUDPRODIE; |
| 165 *flags |= AC3_BSI_AUDPRODIE; | 378 bsi->mixlevel = get_bits(gb, 5); |
| 166 bsi->mixlevel = get_bits_long (gb, 5); | 379 bsi->roomtyp = get_bits(gb, 2); |
| 167 bsi->roomtyp = get_bits_long (gb, 2); | 380 } |
| 168 } | 381 if (bsi->acmod == 0x00) { |
| 169 if (bsi->acmod == 0x00) { | 382 bsi->dialnorm2 = get_bits(gb, 5); |
| 170 bsi->dialnorm2 = get_bits_long (gb, 5); | 383 if (get_bits(gb, 1)) { |
| 171 if (get_bits_long (gb, 1)) { | 384 *flags |= AC3_BSI_COMPR2E; |
| 172 *flags |= AC3_BSI_COMPR2E; | 385 bsi->compr2 = get_bits(gb, 5); |
| 173 bsi->compr2 = get_bits_long (gb, 5); | 386 } |
| 174 } | 387 if (get_bits(gb, 1)) { |
| 175 if (get_bits_long (gb, 1)) { | 388 *flags |= AC3_BSI_LANGCOD2E; |
| 176 *flags |= AC3_BSI_LANGCOD2E; | 389 bsi->langcod2 = get_bits(gb, 8); |
| 177 bsi->langcod2 = get_bits_long (gb, 8); | 390 } |
| 178 } | 391 if (get_bits(gb, 1)) { |
| 179 if (get_bits_long (gb, 1)) { | 392 *flags |= AC3_BSI_AUDPRODIE; |
| 180 *flags |= AC3_BSI_AUDPRODIE; | 393 bsi->mixlevel2 = get_bits(gb, 5); |
| 181 bsi->mixlevel2 = get_bits_long (gb, 5); | 394 bsi->roomtyp2 = get_bits(gb, 2); |
| 182 bsi->roomtyp2 = get_bits_long (gb, 2); | 395 } |
| 183 } | 396 } |
| 184 } | 397 if (get_bits(gb, 1)) |
| 185 if (get_bits_long (gb, 1)) | 398 *flags |= AC3_BSI_COPYRIGHTB; |
| 186 *flags |= AC3_BSI_COPYRIGHTB; | 399 if (get_bits(gb, 1)) |
| 187 if (get_bits_long (gb, 1)) | 400 *flags |= AC3_BSI_ORIGBS; |
| 188 *flags |= AC3_BSI_ORIGBS; | 401 if (get_bits(gb, 1)) { |
| 189 if (get_bits_long (gb, 1)) { | 402 *flags |= AC3_BSI_TIMECOD1E; |
| 190 *flags |= AC3_BSI_TIMECOD1E; | 403 bsi->timecod1 = get_bits(gb, 14); |
| 191 bsi->timecod1 = get_bits_long (gb, 14); | 404 } |
| 192 } | 405 if (get_bits(gb, 1)) { |
| 193 if (get_bits_long (gb, 1)) { | 406 *flags |= AC3_BSI_TIMECOD2E; |
| 194 *flags |= AC3_BSI_TIMECOD2E; | 407 bsi->timecod2 = get_bits(gb, 14); |
| 195 bsi->timecod2 = get_bits_long (gb, 14); | 408 } |
| 196 } | 409 if (get_bits(gb, 1)) { |
| 197 if (get_bits_long (gb, 1)) { | 410 *flags |= AC3_BSI_ADDBSIE; |
| 198 *flags |= AC3_BSI_ADDBSIE; | 411 bsi->addbsil = get_bits(gb, 6); |
| 199 bsi->addbsil = get_bits_long (gb, 6); | 412 do { |
| 200 do { | 413 get_bits(gb, 8); |
| 201 get_bits_long (gb, 8); | 414 } while (bsi->addbsil--); |
| 202 } while (bsi->addbsil--); | 415 } |
| 203 } | 416 |
| 204 | 417 bsi->nfchans = nfchans_tbl[bsi->acmod]; |
| 205 bsi->nfchans = nfchans_tbl[bsi->acmod]; | 418 |
| 206 return 0; | 419 return 0; |
| 207 } | 420 } |
| 208 | 421 |
| 209 static int bands[16] = | 422 /* Decodes the grouped exponents (gexps) and stores them |
| 210 { 31, 35, 37, 39, 41, 42, 43, 44, | |
| 211 45, 45, 46, 46, 47, 47, 48, 48 }; | |
| 212 | |
| 213 static const int diff_exps_M1[128] = | |
| 214 { -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, | |
| 215 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
| 216 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 217 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 218 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
| 219 25, 25, 25 }; | |
| 220 | |
| 221 static const int diff_exps_M2[128] = | |
| 222 { -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
| 223 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
| 224 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
| 225 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
| 226 -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, | |
| 227 25, 25, 25 }; | |
| 228 | |
| 229 static const int diff_exps_M3[128] = | |
| 230 { -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, | |
| 231 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, | |
| 232 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, | |
| 233 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, | |
| 234 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, | |
| 235 25, 25, 25 }; | |
| 236 | |
| 237 /* Decodes the grouped exponents (gexps) and stores them | |
| 238 * in decoded exponents (dexps). | 423 * in decoded exponents (dexps). |
| 424 * The code is derived from liba52. | |
| 425 * Uses liba52 tables. | |
| 239 */ | 426 */ |
| 240 static int | 427 static int _decode_exponents(int expstr, int ngrps, uint8_t absexp, uint8_t *gexps, uint8_t *dexps) |
| 241 _decode_exponents (int expstr, int ngrps, uint8_t absexp, uint8_t * gexps, uint8_t * dexps) | 428 { |
| 242 { | 429 int exps; |
| 243 int i = 0, exp; | 430 int i = 0; |
| 244 while (ngrps--) { | 431 |
| 245 exp = gexps[i++]; | 432 while (ngrps--) { |
| 246 | 433 exps = gexps[i++]; |
| 247 absexp += diff_exps_M1[exp]; | 434 |
| 248 if (absexp > 24) | 435 absexp += exp_1[exps]; |
| 249 return -1; | 436 assert(absexp <= 24); |
| 250 if (expstr == AC3_EXPSTR_D45) { | 437 switch (expstr) { |
| 251 *(dexps++) = absexp; | 438 case AC3_EXPSTR_D45: |
| 252 *(dexps++) = absexp; | 439 *(dexps++) = absexp; |
| 253 } | 440 *(dexps++) = absexp; |
| 254 else if (expstr == AC3_EXPSTR_D25) | 441 case AC3_EXPSTR_D25: |
| 255 *(dexps++) = absexp; | 442 *(dexps++) = absexp; |
| 256 else | 443 case AC3_EXPSTR_D15: |
| 257 *(dexps++) = absexp; | 444 *(dexps++) = absexp; |
| 258 | 445 } |
| 259 absexp += diff_exps_M2[exp]; | 446 absexp += exp_2[exps]; |
| 260 if (absexp > 24) | 447 assert(absexp <= 24); |
| 261 return -1; | 448 switch (expstr) { |
| 262 if (expstr == AC3_EXPSTR_D45) { | 449 case AC3_EXPSTR_D45: |
| 263 *(dexps++) = absexp; | 450 *(dexps++) = absexp; |
| 264 *(dexps++) = absexp; | 451 *(dexps++) = absexp; |
| 265 } | 452 case AC3_EXPSTR_D25: |
| 266 else if (expstr == AC3_EXPSTR_D25) | 453 *(dexps++) = absexp; |
| 267 *(dexps++) = absexp; | 454 case AC3_EXPSTR_D15: |
| 268 else | 455 *(dexps++) = absexp; |
| 269 *(dexps++) = absexp; | 456 } |
| 270 | 457 |
| 271 absexp += diff_exps_M3[exp]; | 458 absexp += exp_3[exps]; |
| 272 if (absexp > 24) | 459 assert(absexp <= 24); |
| 273 return -1; | 460 switch (expstr) { |
| 274 if (expstr == AC3_EXPSTR_D45) { | 461 case AC3_EXPSTR_D45: |
| 275 *(dexps++) = absexp; | 462 *(dexps++) = absexp; |
| 276 *(dexps++) = absexp; | 463 *(dexps++) = absexp; |
| 277 } | 464 case AC3_EXPSTR_D25: |
| 278 else if (expstr == AC3_EXPSTR_D25) | 465 *(dexps++) = absexp; |
| 279 *(dexps++) = absexp; | 466 case AC3_EXPSTR_D15: |
| 280 else | 467 *(dexps++) = absexp; |
| 281 *(dexps++) = absexp; | 468 } |
| 282 } | 469 } |
| 283 | 470 |
| 284 return 0; | 471 return 0; |
| 285 } | 472 } |
| 286 | 473 |
| 287 static int | 474 static int decode_exponents(AC3DecodeContext *ctx) |
| 288 decode_exponents (AC3DecodeContext * ctx) | 475 { |
| 289 { | 476 ac3_audio_block *ab = &ctx->audio_block; |
| 290 ac3_audio_block *ab = &ctx->audio_block; | 477 int i; |
| 291 int i; | 478 uint8_t *exps; |
| 292 uint8_t *exps; | 479 uint8_t *dexps; |
| 293 uint8_t *dexps; | 480 |
| 294 | 481 if (ab->flags & AC3_AB_CPLINU && ab->cplexpstr != AC3_EXPSTR_REUSE) |
| 295 if (ab->flags & AC3_AB_CPLINU && ab->cplexpstr != AC3_EXPSTR_REUSE) | 482 if (_decode_exponents(ab->cplexpstr, ab->ncplgrps, ab->cplabsexp, |
| 296 if (_decode_exponents (ab->cplexpstr, ab->ncplgrps, ab->cplabsexp, | 483 ab->cplexps, ab->dcplexps + ab->cplstrtmant)) |
| 297 ab->cplexps, ab->dcplexps + ab->cplstrtmant)) | 484 return -1; |
| 298 return -1; | 485 for (i = 0; i < ctx->bsi.nfchans; i++) |
| 299 for (i = 0; i < ctx->bsi.nfchans; i++) | 486 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) { |
| 300 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) { | 487 exps = ab->exps[i]; |
| 301 exps = ab->exps[i]; | 488 dexps = ab->dexps[i]; |
| 302 dexps = ab->dexps[i]; | 489 if (_decode_exponents(ab->chexpstr[i], ab->nchgrps[i], exps[0], exps + 1, dexps + 1)) |
| 303 if (_decode_exponents (ab->chexpstr[i], ab->nchgrps[i], exps[0], exps + 1, dexps + 1)) | 490 return -1; |
| 304 return -1; | 491 } |
| 305 } | 492 if (ctx->bsi.flags & AC3_BSI_LFEON && ab->lfeexpstr != AC3_EXPSTR_REUSE) |
| 306 if (ctx->bsi.flags & AC3_BSI_LFEON && ab->lfeexpstr != AC3_EXPSTR_REUSE) | 493 if (_decode_exponents(ab->lfeexpstr, 2, ab->lfeexps[0], ab->lfeexps + 1, ab->dlfeexps)) |
| 307 if (_decode_exponents (ab->lfeexpstr, 2, ab->lfeexps[0], ab->lfeexps + 1, ab->dlfeexps)) | 494 return -1; |
| 308 return -1; | 495 return 0; |
| 309 return 0; | 496 } |
| 310 } | 497 |
| 311 | 498 static inline int16_t logadd(int16_t a, int16_t b) |
| 312 static const int16_t slowdec[4] = { 0x0f, 0x11, 0x13, 0x15 }; /* slow decay table */ | 499 { |
| 313 static const int16_t fastdec[4] = { 0x3f, 0x53, 0x67, 0x7b }; /* fast decay table */ | 500 int16_t c = a - b; |
| 314 static const int16_t slowgain[4] = { 0x540, 0x4d8, 0x478, 0x410 }; /* slow gain table */ | 501 uint8_t address = FFMIN((ABS(c) >> 1), 255); |
| 315 static const int16_t dbpbtab[4] = { 0x000, 0x700, 0x900, 0xb00 }; /* dB/bit table */ | 502 |
| 316 | 503 return ((c >= 0) ? (a + latab[address]) : (b + latab[address])); |
| 317 static const int16_t floortab[8] = /* floor table */ | 504 } |
| 318 { 0x2f0, 0x2b0, 0x270, 0x230, | 505 |
| 319 0x1f0, 0x170, 0x0f0, 0xf800 }; | 506 static inline int16_t calc_lowcomp(int16_t a, int16_t b0, int16_t b1, uint8_t bin) |
| 320 | 507 { |
| 321 static const int16_t fastgain[8] = /* fast gain table */ | 508 if (bin < 7) { |
| 322 { 0x080, 0x100, 0x180, 0x200, | 509 if ((b0 + 256) == b1) |
| 323 0x280, 0x300, 0x380, 0x400 }; | 510 a = 384; |
| 324 | 511 else if (b0 > b1) |
| 325 static const int16_t bndtab[50] = /* start band table */ | 512 a = FFMAX(0, a - 64); |
| 326 { 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, | 513 } |
| 327 27, 28, 31, 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 }; | 514 else if (bin < 20) { |
| 328 | 515 if ((b0 + 256) == b1) |
| 329 static const int16_t bndsz[50] = /* band size table */ | 516 a = 320; |
| 330 { 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, | 517 else if (b0 > b1) |
| 331 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 }; | 518 a = FFMAX(0, a - 64); |
| 332 | 519 } |
| 333 static const int16_t masktab[256] = /* masking table */ | 520 else { |
| 334 { 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, | 521 a = FFMAX(0, a - 128); |
| 335 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, | 522 } |
| 336 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, | 523 |
| 337 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, | 524 return a; |
| 338 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, | |
| 339 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, | |
| 340 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, | |
| 341 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, | |
| 342 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, | |
| 343 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, | |
| 344 49, 49, 49, 0, 0, 0 }; | |
| 345 | |
| 346 static const int16_t latab[256] = /* log addition table */ | |
| 347 { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039, 0x0038, 0x0037, 0x0036, 0x0035, | |
| 348 0x0034, 0x0034, 0x0033, 0x0032, 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c, | |
| 349 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026, 0x0025, 0x0024, 0x0024, 0x0023, | |
| 350 0x0023, 0x0022, 0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c, | |
| 351 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018, 0x0018, 0x0017, 0x0017, 0x0016, | |
| 352 0x0016, 0x0015, 0x0015, 0x0015, 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012, | |
| 353 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f, 0x000f, 0x000e, 0x000e, 0x000e, | |
| 354 0x000d, 0x000d, 0x000d, 0x000d, 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b, | |
| 355 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0008, 0x0008, | |
| 356 0x0008, 0x0008, 0x0008, 0x0008, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006, | |
| 357 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, | |
| 358 0x0005, 0x0005, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, | |
| 359 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, | |
| 360 0x0003, 0x0003, 0x0003, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, | |
| 361 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0001, 0x0001, | |
| 362 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | |
| 363 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, | |
| 364 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
| 365 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
| 366 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
| 367 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, | |
| 368 0x0000, 0x0000, 0x0000, 0x0000 }; | |
| 369 | |
| 370 static const int16_t hth[3][50] = /* hearing threshold table */ | |
| 371 { | |
| 372 {0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, | |
| 373 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350, | |
| 374 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310, | |
| 375 0x0340, 0x0390, 0x03e0, 0x0420, 0x0460, 0x0490, 0x04a0, 0x0440, 0x0440, 0x0400, 0x0520, 0x0800, | |
| 376 0x0840, 0x0840}, | |
| 377 {0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, | |
| 378 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, | |
| 379 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300, | |
| 380 0x0320, 0x0350, 0x0390, 0x03e0, 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630, | |
| 381 0x0840, 0x0840}, | |
| 382 {0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03b0, | |
| 383 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390, | |
| 384 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, | |
| 385 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440, | |
| 386 0x0450, 0x04e0} | |
| 387 }; | |
| 388 | |
| 389 static const uint8_t baptab[64] = /* bit allocation pointer table */ | |
| 390 { 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, | |
| 391 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15, | |
| 392 15, 15, 15, 15, 15, 15, 15, 15 }; | |
| 393 | |
| 394 static inline int16_t | |
| 395 logadd (int16_t a, int16_t b) | |
| 396 { | |
| 397 int16_t c = a - b; | |
| 398 uint8_t address = FFMIN ((ABS (c) >> 1), 255); | |
| 399 | |
| 400 return ((c >= 0) ? (a + latab[address]) : (b + latab[address])); | |
| 401 } | |
| 402 | |
| 403 static inline int16_t | |
| 404 calc_lowcomp (int16_t a, int16_t b0, int16_t b1, uint8_t bin) | |
| 405 { | |
| 406 if (bin < 7) { | |
| 407 if ((b0 + 256) == b1) | |
| 408 a = 384; | |
| 409 else if (b0 > b1) | |
| 410 a = FFMAX (0, a - 64); | |
| 411 } | |
| 412 else if (bin < 20) { | |
| 413 if ((b0 + 256) == b1) | |
| 414 a = 320; | |
| 415 else if (b0 > b1) | |
| 416 a = FFMAX (0, a - 64); | |
| 417 } | |
| 418 else { | |
| 419 a = FFMAX (0, a - 128); | |
| 420 } | |
| 421 | |
| 422 return a; | |
| 423 } | 525 } |
| 424 | 526 |
| 425 /* do the bit allocation for chnl. | 527 /* do the bit allocation for chnl. |
| 426 * chnl = 0 to 4 - fbw channel | 528 * chnl = 0 to 4 - fbw channel |
| 427 * chnl = 5 coupling channel | 529 * chnl = 5 coupling channel |
| 428 * chnl = 6 lfe channel | 530 * chnl = 6 lfe channel |
| 429 */ | 531 */ |
| 430 static int | 532 static int _do_bit_allocation(AC3DecodeContext *ctx, int chnl) |
| 431 _do_bit_allocation (AC3DecodeContext * ctx, int chnl) | 533 { |
| 432 { | 534 ac3_audio_block *ab = &ctx->audio_block; |
| 433 ac3_audio_block *ab = &ctx->audio_block; | 535 int16_t sdecay, fdecay, sgain, dbknee, floor; |
| 434 int16_t sdecay, fdecay, sgain, dbknee, floor; | 536 int16_t lowcomp, fgain, snroffset, fastleak, slowleak; |
| 435 int16_t lowcomp, fgain, snroffset, fastleak, slowleak; | 537 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta; |
| 436 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta; | 538 uint8_t start, end, bin, i, j, k, lastbin, bndstrt, bndend, begin, deltnseg, band, seg, address; |
| 437 uint8_t start, end, bin, i, j, k, lastbin, bndstrt, bndend, begin, deltnseg, band, seg, address; | 539 uint8_t fscod = ctx->sync_info.fscod; |
| 438 uint8_t fscod = ctx->sync_info.fscod; | 540 uint8_t *exps, *deltoffst, *deltlen, *deltba; |
| 439 uint8_t *exps, *deltoffst, *deltlen, *deltba; | 541 uint8_t *baps; |
| 440 uint8_t *baps; | 542 int do_delta = 0; |
| 441 int do_delta = 0; | 543 |
| 442 | 544 /* initialization */ |
| 443 /* initialization */ | 545 sdecay = sdecaytab[ab->sdcycod]; |
| 444 sdecay = slowdec[ab->sdcycod]; | 546 fdecay = fdecaytab[ab->fdcycod]; |
| 445 fdecay = fastdec[ab->fdcycod]; | 547 sgain = sgaintab[ab->sgaincod]; |
| 446 sgain = slowgain[ab->sgaincod]; | 548 dbknee = dbkneetab[ab->dbpbcod]; |
| 447 dbknee = dbpbtab[ab->dbpbcod]; | 549 floor = floortab[ab->floorcod]; |
| 448 floor = dbpbtab[ab->floorcod]; | 550 |
| 449 | 551 if (chnl == 5) { |
| 450 if (chnl == 5) { | 552 start = ab->cplstrtmant; |
| 451 start = ab->cplstrtmant; | 553 end = ab->cplendmant; |
| 452 end = ab->cplendmant; | 554 fgain = fgaintab[ab->cplfgaincod]; |
| 453 fgain = fastgain[ab->cplfgaincod]; | 555 snroffset = (((ab->csnroffst - 15) << 4) + ab->cplfsnroffst) << 2; |
| 454 snroffset = (((ab->csnroffst - 15) << 4) + ab->cplfsnroffst) << 2; | 556 fastleak = (ab->cplfleak << 8) + 768; |
| 455 fastleak = (ab->cplfleak << 8) + 768; | 557 slowleak = (ab->cplsleak << 8) + 768; |
| 456 slowleak = (ab->cplsleak << 8) + 768; | 558 exps = ab->dcplexps; |
| 457 exps = ab->dcplexps; | 559 baps = ab->cplbap; |
| 458 baps = ab->cplbap; | 560 if (ab->cpldeltbae == 0 || ab->cpldeltbae == 1) { |
| 459 if (ab->cpldeltbae == 0 || ab->cpldeltbae == 1) { | 561 do_delta = 1; |
| 460 do_delta = 1; | 562 deltnseg = ab->cpldeltnseg; |
| 461 deltnseg = ab->cpldeltnseg; | 563 deltoffst = ab->cpldeltoffst; |
| 462 deltoffst = ab->cpldeltoffst; | 564 deltlen = ab->cpldeltlen; |
| 463 deltlen = ab->cpldeltlen; | 565 deltba = ab->cpldeltba; |
| 464 deltba = ab->cpldeltba; | 566 } |
| 465 } | 567 } |
| 466 } | 568 else if (chnl == 6) { |
| 467 else if (chnl == 6) { | 569 start = 0; |
| 468 start = 0; | 570 end = 7; |
| 469 end = 7; | 571 lowcomp = 0; |
| 470 lowcomp = 0; | 572 fgain = fgaintab[ab->lfefgaincod]; |
| 471 fgain = fastgain[ab->lfefgaincod]; | 573 snroffset = (((ab->csnroffst - 15) << 4) + ab->lfefsnroffst) << 2; |
| 472 snroffset = (((ab->csnroffst - 15) << 4) + ab->lfefsnroffst) << 2; | 574 exps = ab->dlfeexps; |
| 473 exps = ab->dlfeexps; | 575 baps = ab->lfebap; |
| 474 baps = ab->lfebap; | 576 } |
| 475 } | 577 else { |
| 476 else { | 578 start = 0; |
| 477 start = 0; | 579 end = ab->endmant[chnl]; |
| 478 end = ab->endmant[chnl]; | 580 lowcomp = 0; |
| 479 lowcomp = 0; | 581 fgain = fgaintab[ab->fgaincod[chnl]]; |
| 480 fgain = fastgain[ab->fgaincod[chnl]]; | 582 snroffset = (((ab->csnroffst - 15) << 4) + ab->fsnroffst[chnl]) << 2; |
| 481 snroffset = (((ab->csnroffst - 15) << 4) + ab->fsnroffst[chnl]) << 2; | 583 exps = ab->dexps[chnl]; |
| 482 exps = ab->dexps[chnl]; | 584 baps = ab->bap[chnl]; |
| 483 baps = ab->bap[chnl]; | 585 if (ab->deltbae[chnl] == 0 || ab->deltbae[chnl] == 1) { |
| 484 if (ab->deltbae[chnl] == 0 || ab->deltbae[chnl] == 1) { | 586 do_delta = 1; |
| 485 do_delta = 1; | 587 deltnseg = ab->deltnseg[chnl]; |
| 486 deltnseg = ab->deltnseg[chnl]; | 588 deltoffst = ab->deltoffst[chnl]; |
| 487 deltoffst = ab->deltoffst[chnl]; | 589 deltlen = ab->deltlen[chnl]; |
| 488 deltlen = ab->deltlen[chnl]; | 590 deltba = ab->deltba[chnl]; |
| 489 deltba = ab->deltba[chnl]; | 591 } |
| 490 } | 592 } |
| 491 } | 593 |
| 492 | 594 for (bin = start; bin < end; bin++) /* exponent mapping into psd */ |
| 493 for (bin = start; bin < end; bin++) /* exponent mapping into psd */ | 595 psd[bin] = (3072 - ((int16_t) (exps[bin] << 7))); |
| 494 psd[bin] = (3072 - ((int16_t) (exps[bin] << 7))); | 596 |
| 495 | 597 /* psd integration */ |
| 496 /* psd integration */ | 598 j = start; |
| 497 j = start; | 599 k = masktab[start]; |
| 498 k = masktab[start]; | 600 do { |
| 499 do { | 601 lastbin = FFMIN(bndtab[k] + bndsz[k], end); |
| 500 lastbin = FFMIN (bndtab[k] + bndsz[k], end); | 602 bndpsd[k] = psd[j]; |
| 501 bndpsd[k] = psd[j]; | 603 j++; |
| 502 j++; | 604 for (i = j; i < lastbin; i++) { |
| 503 for (i = j; i < lastbin; i++) { | 605 bndpsd[k] = logadd(bndpsd[k], psd[j]); |
| 504 bndpsd[k] = logadd (bndpsd[k], psd[j]); | 606 j++; |
| 505 j++; | 607 } |
| 506 } | 608 k++; |
| 507 k++; | 609 } while (end > lastbin); |
| 508 } while (end > lastbin); | 610 |
| 509 | 611 /* compute the excite function */ |
| 510 /* compute the excite function */ | 612 bndstrt = masktab[start]; |
| 511 bndstrt = masktab[start]; | 613 bndend = masktab[end - 1] + 1; |
| 512 bndend = masktab[end - 1] + 1; | 614 if (bndstrt == 0) { |
| 513 if (bndstrt == 0) { | 615 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0); |
| 514 lowcomp = calc_lowcomp (lowcomp, bndpsd[0], bndpsd[1], 0); | 616 excite[0] = bndpsd[0] - fgain - lowcomp; |
| 515 excite[0] = bndpsd[0] - fgain - lowcomp; | 617 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1); |
| 516 lowcomp = calc_lowcomp (lowcomp, bndpsd[1], bndpsd[2], 1); | 618 excite[1] = bndpsd[1] - fgain - lowcomp; |
| 517 excite[1] = bndpsd[1] - fgain - lowcomp; | 619 begin = 7; |
| 518 begin = 7; | 620 for (bin = 2; bin < 7; bin++) { |
| 519 for (bin = 2; bin < 7; bin++) { | 621 if (bndend != 7 || bin != 6) |
| 520 if (bndend != 7 || bin != 6) | 622 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); |
| 521 lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); | 623 fastleak = bndpsd[bin] - fgain; |
| 522 fastleak = bndpsd[bin] - fgain; | 624 slowleak = bndpsd[bin] - sgain; |
| 523 slowleak = bndpsd[bin] - sgain; | 625 excite[bin] = fastleak - lowcomp; |
| 524 excite[bin] = fastleak - lowcomp; | 626 if (bndend != 7 || bin != 6) |
| 525 if (bndend != 7 || bin != 6) | 627 if (bndpsd[bin] <= bndpsd[bin + 1]) { |
| 526 if (bndpsd[bin] <= bndpsd[bin + 1]) { | 628 begin = bin + 1; |
| 527 begin = bin + 1; | 629 break; |
| 528 break; | 630 } |
| 529 } | 631 } |
| 530 } | 632 for (bin = begin; bin < (FFMIN(bndend, 22)); bin++) { |
| 531 for (bin = begin; bin < (FFMIN (bndend, 22)); bin++) { | 633 if (bndend != 7 || bin != 6) |
| 532 if (bndend != 7 || bin != 6) | 634 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); |
| 533 lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); | 635 fastleak -= fdecay; |
| 534 fastleak -= fdecay; | 636 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain); |
| 535 fastleak = FFMAX (fastleak, bndpsd[bin] - fgain); | 637 slowleak -= sdecay; |
| 536 slowleak -= sdecay; | 638 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain); |
| 537 slowleak = FFMAX (slowleak, bndpsd[bin] - sgain); | 639 excite[bin] = FFMAX(fastleak - lowcomp, slowleak); |
| 538 excite[bin] = FFMAX (fastleak - lowcomp, slowleak); | 640 } |
| 539 } | 641 begin = 22; |
| 540 begin = 22; | 642 } |
| 541 } | 643 else { |
| 542 else { | 644 begin = bndstrt; |
| 543 begin = bndstrt; | 645 } |
| 544 } | 646 for (bin = begin; bin < bndend; bin++) { |
| 545 for (bin = begin; bin < bndend; bin++) { | 647 fastleak -= fdecay; |
| 546 fastleak -= fdecay; | 648 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain); |
| 547 fastleak = FFMAX (fastleak, bndpsd[bin] - fgain); | 649 slowleak -= sdecay; |
| 548 slowleak -= sdecay; | 650 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain); |
| 549 slowleak = FFMAX (slowleak, bndpsd[bin] - sgain); | 651 excite[bin] = FFMAX(fastleak, slowleak); |
| 550 excite[bin] = FFMAX (fastleak, slowleak); | 652 } |
| 551 } | 653 |
| 552 | 654 /* compute the masking curve */ |
| 553 /* compute the masking curve */ | 655 for (bin = bndstrt; bin < bndend; bin++) { |
| 554 for (bin = bndstrt; bin < bndend; bin++) { | 656 if (bndpsd[bin] < dbknee) |
| 555 if (bndpsd[bin] < dbknee) | 657 excite[bin] += ((dbknee - bndpsd[bin]) >> 2); |
| 556 excite[bin] += ((dbknee - bndpsd[bin]) >> 2); | 658 mask[bin] = FFMAX(excite[bin], hth[bin][fscod]); |
| 557 mask[bin] = FFMAX (excite[bin], hth[fscod][bin]); | 659 } |
| 558 } | 660 |
| 559 | 661 /* apply the delta bit allocation */ |
| 560 /* apply the delta bit allocation */ | 662 if (do_delta) { |
| 561 if (do_delta) { | 663 band = 0; |
| 562 band = 0; | 664 for (seg = 0; seg < deltnseg + 1; seg++) { |
| 563 for (seg = 0; seg < deltnseg + 1; seg++) { | 665 band += deltoffst[seg]; |
| 564 band += deltoffst[seg]; | 666 if (deltba[seg] >= 4) |
| 565 if (deltba[seg] >= 4) | 667 delta = (deltba[seg] - 3) << 7; |
| 566 delta = (deltba[seg] - 3) << 7; | 668 else |
| 567 else | 669 delta = (deltba[seg] - 4) << 7; |
| 568 delta = (deltba[seg] - 4) << 7; | 670 for (k = 0; k < deltlen[seg]; k++) { |
| 569 for (k = 0; k < deltlen[seg]; k++) { | 671 mask[band] += delta; |
| 570 mask[band] += delta; | 672 band++; |
| 571 band++; | 673 } |
| 572 } | 674 } |
| 573 } | 675 } |
| 574 } | 676 |
| 575 | 677 /*compute the bit allocation */ |
| 576 /*compute the bit allocation */ | 678 i = start; |
| 577 i = start; | 679 j = masktab[start]; |
| 578 j = masktab[start]; | 680 do { |
| 579 do { | 681 lastbin = FFMIN(bndtab[j] + bndsz[j], end); |
| 580 lastbin = FFMIN (bndtab[j] + bndsz[j], end); | 682 mask[j] -= snroffset; |
| 581 mask[j] -= snroffset; | 683 mask[j] -= floor; |
| 582 mask[j] -= floor; | 684 if (mask[j] < 0) |
| 583 if (mask[j] < 0) | 685 mask[j] = 0; |
| 584 mask[j] = 0; | 686 mask[j] &= 0x1fe0; |
| 585 mask[j] &= 0x1fe0; | 687 mask[j] += floor; |
| 586 mask[j] += floor; | 688 for (k = i; k < lastbin; k++) { |
| 587 for (k = i; k < lastbin; k++) { | 689 address = (psd[i] - mask[j]) >> 5; |
| 588 address = (psd[i] - mask[j]) >> 5; | 690 address = FFMIN(63, (FFMAX(0, address))); |
| 589 address = FFMIN (63, (FFMAX (0, address))); | 691 baps[i] = baptab[address]; |
| 590 baps[i] = baptab[address]; | 692 i++; |
| 591 i++; | 693 } |
| 592 } | 694 j++; |
| 593 j++; | 695 } while (end > lastbin); |
| 594 } while (end > lastbin); | 696 |
| 595 | |
| 596 return 0; | |
| 597 } | |
| 598 | |
| 599 static int | |
| 600 do_bit_allocation (AC3DecodeContext * ctx, int flags) | |
| 601 { | |
| 602 ac3_audio_block *ab = &ctx->audio_block; | |
| 603 int i, snroffst = 0; | |
| 604 | |
| 605 if (!flags) /* bit allocation is not required */ | |
| 606 return 0; | 697 return 0; |
| 607 | 698 } |
| 608 if (ab->flags & AC3_AB_SNROFFSTE) { /* check whether snroffsts are zero */ | 699 |
| 609 snroffst += ab->csnroffst; | 700 static int do_bit_allocation(AC3DecodeContext *ctx, int flags) |
| 701 { | |
| 702 ac3_audio_block *ab = &ctx->audio_block; | |
| 703 int i, snroffst = 0; | |
| 704 | |
| 705 if (!flags) /* bit allocation is not required */ | |
| 706 return 0; | |
| 707 | |
| 708 if (ab->flags & AC3_AB_SNROFFSTE) { /* check whether snroffsts are zero */ | |
| 709 snroffst += ab->csnroffst; | |
| 710 if (ab->flags & AC3_AB_CPLINU) | |
| 711 snroffst += ab->cplfsnroffst; | |
| 712 for (i = 0; i < ctx->bsi.nfchans; i++) | |
| 713 snroffst += ab->fsnroffst[i]; | |
| 714 if (ctx->bsi.flags & AC3_BSI_LFEON) | |
| 715 snroffst += ab->lfefsnroffst; | |
| 716 if (!snroffst) { | |
| 717 memset(ab->cplbap, 0, sizeof (ab->cplbap)); | |
| 718 for (i = 0; i < ctx->bsi.nfchans; i++) | |
| 719 memset(ab->bap[i], 0, sizeof (ab->bap[i])); | |
| 720 memset(ab->lfebap, 0, sizeof (ab->lfebap)); | |
| 721 | |
| 722 return 0; | |
| 723 } | |
| 724 } | |
| 725 | |
| 726 /* perform bit allocation */ | |
| 727 if ((ab->flags & AC3_AB_CPLINU) && (flags & 64)) | |
| 728 if (_do_bit_allocation(ctx, 5)) | |
| 729 return -1; | |
| 730 for (i = 0; i < ctx->bsi.nfchans; i++) | |
| 731 if (flags & (1 << i)) | |
| 732 if (_do_bit_allocation(ctx, i)) | |
| 733 return -1; | |
| 734 if ((ctx->bsi.flags & AC3_BSI_LFEON) && (flags & 32)) | |
| 735 if (_do_bit_allocation(ctx, 6)) | |
| 736 return -1; | |
| 737 | |
| 738 return 0; | |
| 739 } | |
| 740 | |
| 741 static inline float to_float(uint8_t exp, int16_t mantissa) | |
| 742 { | |
| 743 return ((float) (mantissa * scale_factors[exp])); | |
| 744 } | |
| 745 | |
| 746 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */ | |
| 747 uint8_t gcodes[3]; | |
| 748 uint8_t gcptr; | |
| 749 } mant_group; | |
| 750 | |
| 751 /* Get the transform coefficients for particular channel */ | |
| 752 static int _get_transform_coeffs(uint8_t *exps, uint8_t *bap, float chcoeff, | |
| 753 float *samples, int start, int end, int dith_flag, GetBitContext *gb, | |
| 754 dither_state *state) | |
| 755 { | |
| 756 int16_t mantissa; | |
| 757 int i; | |
| 758 int gcode; | |
| 759 mant_group l3_grp, l5_grp, l11_grp; | |
| 760 | |
| 761 for (i = 0; i < 3; i++) | |
| 762 l3_grp.gcodes[i] = l5_grp.gcodes[i] = l11_grp.gcodes[i] = -1; | |
| 763 l3_grp.gcptr = l5_grp.gcptr = 3; | |
| 764 l11_grp.gcptr = 2; | |
| 765 | |
| 766 i = 0; | |
| 767 while (i < start) | |
| 768 samples[i++] = 0; | |
| 769 | |
| 770 for (i = start; i < end; i++) { | |
| 771 switch (bap[i]) { | |
| 772 case 0: | |
| 773 if (!dith_flag) | |
| 774 mantissa = 0; | |
| 775 else | |
| 776 mantissa = dither_int16(state); | |
| 777 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 778 break; | |
| 779 | |
| 780 case 1: | |
| 781 if (l3_grp.gcptr > 2) { | |
| 782 gcode = get_bits(gb, qntztab[1]); | |
| 783 if (gcode > 26) | |
| 784 return -1; | |
| 785 l3_grp.gcodes[0] = gcode / 9; | |
| 786 l3_grp.gcodes[1] = (gcode % 9) / 3; | |
| 787 l3_grp.gcodes[2] = (gcode % 9) % 3; | |
| 788 l3_grp.gcptr = 0; | |
| 789 } | |
| 790 mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]]; | |
| 791 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 792 break; | |
| 793 | |
| 794 case 2: | |
| 795 if (l5_grp.gcptr > 2) { | |
| 796 gcode = get_bits(gb, qntztab[2]); | |
| 797 if (gcode > 124) | |
| 798 return -1; | |
| 799 l5_grp.gcodes[0] = gcode / 25; | |
| 800 l5_grp.gcodes[1] = (gcode % 25) / 5; | |
| 801 l5_grp.gcodes[2] = (gcode % 25) % 5; | |
| 802 l5_grp.gcptr = 0; | |
| 803 } | |
| 804 mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]]; | |
| 805 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 806 break; | |
| 807 | |
| 808 case 3: | |
| 809 mantissa = get_bits(gb, qntztab[3]); | |
| 810 if (mantissa > 6) | |
| 811 return -1; | |
| 812 mantissa = l7_q_tab[mantissa]; | |
| 813 samples[i] = to_float(exps[i], mantissa); | |
| 814 break; | |
| 815 | |
| 816 case 4: | |
| 817 if (l11_grp.gcptr > 1) { | |
| 818 gcode = get_bits(gb, qntztab[4]); | |
| 819 if (gcode > 120) | |
| 820 return -1; | |
| 821 l11_grp.gcodes[0] = gcode / 11; | |
| 822 l11_grp.gcodes[1] = gcode % 11; | |
| 823 } | |
| 824 mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]]; | |
| 825 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 826 break; | |
| 827 | |
| 828 case 5: | |
| 829 mantissa = get_bits(gb, qntztab[5]); | |
| 830 if (mantissa > 14) | |
| 831 return -1; | |
| 832 mantissa = l15_q_tab[mantissa]; | |
| 833 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 834 break; | |
| 835 | |
| 836 default: | |
| 837 mantissa = get_bits(gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]); | |
| 838 samples[i] = to_float(exps[i], mantissa) * chcoeff; | |
| 839 break; | |
| 840 } | |
| 841 } | |
| 842 | |
| 843 i = end; | |
| 844 while (i < 256) | |
| 845 samples[i++] = 0; | |
| 846 | |
| 847 return 0; | |
| 848 } | |
| 849 | |
| 850 static int uncouple_channels(AC3DecodeContext * ctx) | |
| 851 { | |
| 852 ac3_audio_block *ab = &ctx->audio_block; | |
| 853 int ch, sbnd, bin; | |
| 854 int index; | |
| 855 float (*samples)[256]; | |
| 856 int16_t mantissa; | |
| 857 | |
| 858 samples = (float (*)[256])((ctx->bsi.flags & AC3_BSI_LFEON) ? (ctx->samples + 256) : (ctx->samples)); | |
| 859 | |
| 860 /* uncouple channels */ | |
| 861 for (ch = 0; ch < ctx->bsi.nfchans; ch++) | |
| 862 if (ab->chincpl & (1 << ch)) | |
| 863 for (sbnd = ab->cplbegf; sbnd < 3 + ab->cplendf; sbnd++) | |
| 864 for (bin = 0; bin < 12; bin++) { | |
| 865 index = sbnd * 12 + bin + 37; | |
| 866 samples[ch][index] = ab->cplcoeffs[index] * ab->cplco[ch][sbnd] * ab->chcoeffs[ch]; | |
| 867 } | |
| 868 | |
| 869 /* generate dither if required */ | |
| 870 for (ch = 0; ch < ctx->bsi.nfchans; ch++) | |
| 871 if ((ab->chincpl & (1 << ch)) && (ab->dithflag & (1 << ch))) | |
| 872 for (index = 0; index < ab->endmant[ch]; index++) | |
| 873 if (!ab->bap[ch][index]) { | |
| 874 mantissa = dither_int16(&ctx->state); | |
| 875 samples[ch][index] = to_float(ab->dexps[ch][index], mantissa) * ab->chcoeffs[ch]; | |
| 876 } | |
| 877 | |
| 878 return 0; | |
| 879 } | |
| 880 | |
| 881 static int get_transform_coeffs(AC3DecodeContext * ctx) | |
| 882 { | |
| 883 int i; | |
| 884 ac3_audio_block *ab = &ctx->audio_block; | |
| 885 float *samples = ctx->samples; | |
| 886 int got_cplchan = 0; | |
| 887 int dithflag = 0; | |
| 888 | |
| 889 samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0; | |
| 890 for (i = 0; i < ctx->bsi.nfchans; i++) { | |
| 891 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i))) | |
| 892 dithflag = 0; /* don't generate dither until channels are decoupled */ | |
| 893 else | |
| 894 dithflag = ab->dithflag & (1 << i); | |
| 895 /* transform coefficients for individual channel */ | |
| 896 if (_get_transform_coeffs(ab->dexps[i], ab->bap[i], ab->chcoeffs[i], samples + (i * 256), | |
| 897 0, ab->endmant[i], dithflag, &ctx->gb, &ctx->state)) | |
| 898 return -1; | |
| 899 /* tranform coefficients for coupling channels */ | |
| 900 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)) && !got_cplchan) { | |
| 901 if (_get_transform_coeffs(ab->dcplexps, ab->cplbap, 1.0f, ab->cplcoeffs, | |
| 902 ab->cplstrtmant, ab->cplendmant, 0, &ctx->gb, &ctx->state)) | |
| 903 return -1; | |
| 904 got_cplchan = 1; | |
| 905 } | |
| 906 } | |
| 907 if (ctx->bsi.flags & AC3_BSI_LFEON) | |
| 908 if (_get_transform_coeffs(ab->lfeexps, ab->lfebap, 1.0f, samples - 256, 0, 7, 0, &ctx->gb, &ctx->state)) | |
| 909 return -1; | |
| 910 | |
| 911 /* uncouple the channels from the coupling channel */ | |
| 610 if (ab->flags & AC3_AB_CPLINU) | 912 if (ab->flags & AC3_AB_CPLINU) |
| 611 snroffst += ab->cplfsnroffst; | 913 if (uncouple_channels(ctx)) |
| 612 for (i = 0; i < ctx->bsi.nfchans; i++) | 914 return -1; |
| 613 snroffst += ab->fsnroffst[i]; | 915 |
| 614 if (ctx->bsi.flags & AC3_BSI_LFEON) | 916 return 0; |
| 615 snroffst += ab->lfefsnroffst; | |
| 616 if (!snroffst) { | |
| 617 memset (ab->cplbap, 0, sizeof (ab->cplbap)); | |
| 618 for (i = 0; i < ctx->bsi.nfchans; i++) | |
| 619 memset (ab->bap[i], 0, sizeof (ab->bap[i])); | |
| 620 memset (ab->lfebap, 0, sizeof (ab->lfebap)); | |
| 621 | |
| 622 return 0; | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 /* perform bit allocation */ | |
| 627 if ((ab->flags & AC3_AB_CPLINU) && (flags & 64)) | |
| 628 if (_do_bit_allocation (ctx, 5)) | |
| 629 return -1; | |
| 630 for (i = 0; i < ctx->bsi.nfchans; i++) | |
| 631 if (flags & (1 << i)) | |
| 632 if (_do_bit_allocation (ctx, i)) | |
| 633 return -1; | |
| 634 if ((ctx->bsi.flags & AC3_BSI_LFEON) && (flags & 32)) | |
| 635 if (_do_bit_allocation (ctx, 6)) | |
| 636 return -1; | |
| 637 | |
| 638 return 0; | |
| 639 } | |
| 640 | |
| 641 /* table for exponent to scale_factor mapping | |
| 642 * scale_factor[i] = 2 ^ -(i + 15) | |
| 643 */ | |
| 644 static const float scale_factors[25] = { | |
| 645 0.000030517578125000000000000000000000000, | |
| 646 0.000015258789062500000000000000000000000, | |
| 647 0.000007629394531250000000000000000000000, | |
| 648 0.000003814697265625000000000000000000000, | |
| 649 0.000001907348632812500000000000000000000, | |
| 650 0.000000953674316406250000000000000000000, | |
| 651 0.000000476837158203125000000000000000000, | |
| 652 0.000000238418579101562500000000000000000, | |
| 653 0.000000119209289550781250000000000000000, | |
| 654 0.000000059604644775390625000000000000000, | |
| 655 0.000000029802322387695312500000000000000, | |
| 656 0.000000014901161193847656250000000000000, | |
| 657 0.000000007450580596923828125000000000000, | |
| 658 0.000000003725290298461914062500000000000, | |
| 659 0.000000001862645149230957031250000000000, | |
| 660 0.000000000931322574615478515625000000000, | |
| 661 0.000000000465661287307739257812500000000, | |
| 662 0.000000000232830643653869628906250000000, | |
| 663 0.000000000116415321826934814453125000000, | |
| 664 0.000000000058207660913467407226562500000, | |
| 665 0.000000000029103830456733703613281250000, | |
| 666 0.000000000014551915228366851806640625000, | |
| 667 0.000000000007275957614183425903320312500, | |
| 668 0.000000000003637978807091712951660156250, | |
| 669 0.000000000001818989403545856475830078125 | |
| 670 }; | |
| 671 | |
| 672 static const int16_t l3_q_tab[3] = { /* 3-level quantization table */ | |
| 673 (-2 << 15) / 3, 0, (2 << 15) / 3 | |
| 674 }; | |
| 675 | |
| 676 static const int16_t l5_q_tab[5] = { /* 5-level quantization table */ | |
| 677 (-4 << 15) / 5, (-2 << 15) / 5, 0, (2 << 15) / 5, (4 << 15) / 5 | |
| 678 }; | |
| 679 | |
| 680 static const int16_t l7_q_tab[7] = { /* 7-level quantization table */ | |
| 681 (-6 << 15) / 7, (-4 << 15) / 7, (-2 << 15) / 7, 0, | |
| 682 (2 << 15) / 7, (4 << 15) / 7, (6 << 15) / 7 | |
| 683 }; | |
| 684 | |
| 685 static const int16_t l11_q_tab[11] = { /* 11-level quantization table */ | |
| 686 (-10 << 15) / 11, (-8 << 15) / 11, (-6 << 15) / 11, (-4 << 15) / 11, (-2 << 15) / 11, 0, | |
| 687 (2 << 15) / 11, (4 << 15) / 11, (6 << 15) / 11, (8 << 15) / 11, (10 << 15) / 11 | |
| 688 }; | |
| 689 | |
| 690 static const int16_t l15_q_tab[15] = { /* 15-level quantization table */ | |
| 691 (-14 << 15) / 15, (-12 << 15) / 15, (-10 << 15) / 15, (-8 << 15) / 15, | |
| 692 (-6 << 15) / 15, (-4 << 15) / 15, (-2 << 15) / 15, 0, | |
| 693 (2 << 15) / 15, (4 << 15) / 15, (6 << 15) / 15, (8 << 15) / 15, | |
| 694 (10 << 15) / 15, (12 << 15) / 15, (14 << 15) / 15 | |
| 695 }; | |
| 696 | |
| 697 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 12, 12, 14, 16 }; | |
| 698 | |
| 699 static inline float | |
| 700 to_float (uint8_t exp, int16_t mantissa) | |
| 701 { | |
| 702 return ((float) (mantissa * scale_factors[exp])); | |
| 703 } | |
| 704 | |
| 705 typedef struct | |
| 706 { /* grouped mantissas for 3-level 5-leve and 11-level quantization */ | |
| 707 uint8_t gcodes[3]; | |
| 708 uint8_t gcptr; | |
| 709 } mant_group; | |
| 710 | |
| 711 /* Get the transform coefficients for particular channel */ | |
| 712 static int | |
| 713 _get_transform_coeffs (uint8_t * exps, uint8_t * bap, float *samples, | |
| 714 int start, int end, int dith_flag, GetBitContext * gb) | |
| 715 { | |
| 716 int16_t mantissa; | |
| 717 int i; | |
| 718 int gcode; | |
| 719 mant_group l3_grp, l5_grp, l11_grp; | |
| 720 | |
| 721 for (i = 0; i < 3; i++) | |
| 722 l3_grp.gcodes[i] = l5_grp.gcodes[i] = l11_grp.gcodes[i] = -1; | |
| 723 l3_grp.gcptr = l5_grp.gcptr = 3; | |
| 724 l11_grp.gcptr = 2; | |
| 725 | |
| 726 i = 0; | |
| 727 while (i < start) | |
| 728 samples[i++] = 0; | |
| 729 | |
| 730 for (i = start; i < end; i++) { | |
| 731 switch (bap[i]) { | |
| 732 case 0: | |
| 733 if (!dith_flag) | |
| 734 mantissa = 0; | |
| 735 else | |
| 736 mantissa = gen_dither (); | |
| 737 samples[i] = to_float (exps[i], mantissa); | |
| 738 break; | |
| 739 | |
| 740 case 1: | |
| 741 if (l3_grp.gcptr > 2) { | |
| 742 gcode = get_bits_long (gb, qntztab[1]); | |
| 743 if (gcode > 26) | |
| 744 return -1; | |
| 745 l3_grp.gcodes[0] = gcode / 9; | |
| 746 l3_grp.gcodes[1] = (gcode % 9) / 3; | |
| 747 l3_grp.gcodes[2] = (gcode % 9) % 3; | |
| 748 l3_grp.gcptr = 0; | |
| 749 } | |
| 750 mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]]; | |
| 751 samples[i] = to_float (exps[i], mantissa); | |
| 752 break; | |
| 753 | |
| 754 case 2: | |
| 755 if (l5_grp.gcptr > 2) { | |
| 756 gcode = get_bits_long (gb, qntztab[2]); | |
| 757 if (gcode > 124) | |
| 758 return -1; | |
| 759 l5_grp.gcodes[0] = gcode / 25; | |
| 760 l5_grp.gcodes[1] = (gcode % 25) / 5; | |
| 761 l5_grp.gcodes[2] = (gcode % 25) % 5; | |
| 762 l5_grp.gcptr = 0; | |
| 763 } | |
| 764 mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]]; | |
| 765 samples[i] = to_float (exps[i], mantissa); | |
| 766 break; | |
| 767 | |
| 768 case 3: | |
| 769 mantissa = get_bits_long (gb, qntztab[3]); | |
| 770 if (mantissa > 6) | |
| 771 return -1; | |
| 772 mantissa = l7_q_tab[mantissa]; | |
| 773 samples[i] = to_float (exps[i], mantissa); | |
| 774 break; | |
| 775 | |
| 776 case 4: | |
| 777 if (l11_grp.gcptr > 1) { | |
| 778 gcode = get_bits_long (gb, qntztab[4]); | |
| 779 if (gcode > 120) | |
| 780 return -1; | |
| 781 l11_grp.gcodes[0] = gcode / 11; | |
| 782 l11_grp.gcodes[1] = gcode % 11; | |
| 783 } | |
| 784 mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]]; | |
| 785 samples[i] = to_float (exps[i], mantissa); | |
| 786 break; | |
| 787 | |
| 788 case 5: | |
| 789 mantissa = get_bits_long (gb, qntztab[5]); | |
| 790 if (mantissa > 14) | |
| 791 return -1; | |
| 792 mantissa = l15_q_tab[mantissa]; | |
| 793 break; | |
| 794 | |
| 795 default: | |
| 796 mantissa = get_bits_long (gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]); | |
| 797 samples[i] = to_float (exps[i], mantissa); | |
| 798 break; | |
| 799 } | |
| 800 } | |
| 801 | |
| 802 i = end; | |
| 803 while (i < 256) | |
| 804 samples[i++] = 0; | |
| 805 | |
| 806 return 0; | |
| 807 } | |
| 808 | |
| 809 static int | |
| 810 uncouple_channels (AC3DecodeContext * ctx) | |
| 811 { | |
| 812 ac3_audio_block *ab = &ctx->audio_block; | |
| 813 int ch, sbnd, bin; | |
| 814 int index; | |
| 815 float (*samples)[256]; | |
| 816 int16_t mantissa; | |
| 817 | |
| 818 samples = (float (*)[256]) (ab->ab_samples); | |
| 819 samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0; | |
| 820 | |
| 821 /* uncouple channels */ | |
| 822 for (ch = 0; ch < ctx->bsi.nfchans; ch++) | |
| 823 if (ab->chincpl & (1 << ch)) | |
| 824 for (sbnd = ab->cplbegf; sbnd < 3 + ab->cplendf; sbnd++) | |
| 825 for (bin = 0; bin < 12; bin++) { | |
| 826 index = sbnd * 12 + bin + 37; | |
| 827 samples[ch][index] = ab->cplcoeffs[index] * ab->cplco[ch][sbnd] * 8; | |
| 828 } | |
| 829 | |
| 830 /* generate dither if required */ | |
| 831 for (ch = 0; ch < ctx->bsi.nfchans; ch++) | |
| 832 if ((ab->chincpl & (1 << ch)) && (ab->dithflag & (1 << ch))) | |
| 833 for (index = 0; index < ab->endmant[ch]; index++) | |
| 834 if (!ab->bap[ch][index]) { | |
| 835 mantissa = gen_dither (); | |
| 836 samples[ch][index] = to_float (ab->dexps[ch][index], mantissa); | |
| 837 } | |
| 838 | |
| 839 return 0; | |
| 840 } | |
| 841 | |
| 842 static int | |
| 843 get_transform_coeffs (AC3DecodeContext * ctx) | |
| 844 { | |
| 845 int i; | |
| 846 ac3_audio_block *ab = &ctx->audio_block; | |
| 847 float *samples = ab->ab_samples; | |
| 848 int got_cplchan = 0; | |
| 849 int dithflag = 0; | |
| 850 | |
| 851 samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0; | |
| 852 for (i = 0; i < ctx->bsi.nfchans; i++) { | |
| 853 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i))) | |
| 854 dithflag = 0; /* don't generate dither until channels are decoupled */ | |
| 855 else | |
| 856 dithflag = ab->dithflag & (1 << i); | |
| 857 /* transform coefficients for individual channel */ | |
| 858 if (_get_transform_coeffs (ab->dexps[i], ab->bap[i], samples + (i * 256), | |
| 859 0, ab->endmant[i], dithflag, &ctx->gb)) | |
| 860 return -1; | |
| 861 /* tranform coefficients for coupling channels */ | |
| 862 if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)) && !got_cplchan) { | |
| 863 if (_get_transform_coeffs (ab->dcplexps, ab->cplbap, ab->cplcoeffs, | |
| 864 ab->cplstrtmant, ab->cplendmant, 0, &ctx->gb)) | |
| 865 return -1; | |
| 866 got_cplchan = 1; | |
| 867 } | |
| 868 } | |
| 869 | |
| 870 /* uncouple the channels from the coupling channel */ | |
| 871 if (ab->flags & AC3_AB_CPLINU) | |
| 872 if (uncouple_channels (ctx)) | |
| 873 return -1; | |
| 874 | |
| 875 return 0; | |
| 876 } | 917 } |
| 877 | 918 |
| 878 /* generate coupling co-ordinates for each coupling subband | 919 /* generate coupling co-ordinates for each coupling subband |
| 879 * from coupling co-ordinates of each band and coupling band | 920 * from coupling co-ordinates of each band and coupling band |
| 880 * structure information | 921 * structure information |
| 881 */ | 922 */ |
| 882 static int | 923 static int generate_coupling_coordinates(AC3DecodeContext * ctx) |
| 883 generate_coupling_coordinates (AC3DecodeContext * ctx) | 924 { |
| 884 { | 925 ac3_audio_block *ab = &ctx->audio_block; |
| 885 ac3_audio_block *ab = &ctx->audio_block; | 926 uint8_t exp, mstrcplco; |
| 886 uint8_t exp, mstrcplco; | 927 int16_t mant; |
| 887 int16_t mant; | 928 uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1; |
| 888 uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1; | 929 int ch, bnd, sbnd; |
| 889 int ch, bnd, sbnd; | 930 float cplco; |
| 890 float cplco; | 931 |
| 891 | 932 if (ab->cplcoe) |
| 892 if (ab->cplcoe) | 933 for (ch = 0; ch < ctx->bsi.nfchans; ch++) |
| 893 for (ch = 0; ch < ctx->bsi.nfchans; ch++) | 934 if (ab->cplcoe & (1 << ch)) { |
| 894 if (ab->cplcoe & (1 << ch)) { | 935 mstrcplco = 3 * ab->mstrcplco[ch]; |
| 895 mstrcplco = 3 * ab->mstrcplco[ch]; | 936 sbnd = ab->cplbegf; |
| 896 sbnd = ab->cplbegf; | 937 for (bnd = 0; bnd < ab->ncplbnd; bnd++) { |
| 897 for (bnd = 0; bnd < ab->ncplbnd; bnd++) { | 938 exp = ab->cplcoexp[ch][bnd]; |
| 898 exp = ab->cplcoexp[ch][bnd]; | 939 if (exp == 15) |
| 899 if (exp == 15) | 940 mant = ab->cplcomant[ch][bnd] <<= 14; |
| 900 mant = ab->cplcomant[ch][bnd] <<= 14; | 941 else |
| 901 else | 942 mant = (ab->cplcomant[ch][bnd] | 0x10) << 13; |
| 902 mant = (ab->cplcomant[ch][bnd] | 0x10) << 13; | 943 cplco = to_float(exp + mstrcplco, mant); |
| 903 cplco = to_float (exp + mstrcplco, mant); | 944 if (ctx->bsi.acmod == 0x02 && (ab->flags & AC3_AB_PHSFLGINU) && ch == 1 |
| 904 if (ctx->bsi.acmod == 0x02 && (ab->flags & AC3_AB_PHSFLGINU) && ch == 1 | 945 && (ab->phsflg & (1 << bnd))) |
| 905 && (ab->phsflg & (1 << bnd))) | 946 cplco = -cplco; /* invert the right channel */ |
| 906 cplco = -cplco; /* invert the right channel */ | 947 ab->cplco[ch][sbnd++] = cplco; |
| 907 ab->cplco[ch][sbnd++] = cplco; | 948 while (cplbndstrc & ab->cplbndstrc) { |
| 908 while (cplbndstrc & ab->cplbndstrc) { | 949 cplbndstrc >>= 1; |
| 909 cplbndstrc >>= 1; | 950 ab->cplco[ch][sbnd++] = cplco; |
| 910 ab->cplco[ch][sbnd++] = cplco; | 951 } |
| 911 } | 952 cplbndstrc >>= 1; |
| 912 cplbndstrc >>= 1; | 953 } |
| 913 } | 954 } |
| 914 } | 955 |
| 915 | 956 return 0; |
| 916 return 0; | 957 } |
| 917 } | 958 |
| 918 | 959 static int _do_rematrixing(AC3DecodeContext *ctx, int start, int end) |
| 919 static int | 960 { |
| 920 ac3_parse_audio_block (AC3DecodeContext * ctx, int index) | 961 float tmp0, tmp1; |
| 921 { | 962 |
| 922 ac3_audio_block *ab = &ctx->audio_block; | 963 while (start < end) { |
| 923 int nfchans = ctx->bsi.nfchans; | 964 tmp0 = ctx->samples[start]; |
| 924 int acmod = ctx->bsi.acmod; | 965 tmp1 = (ctx->samples + 256)[start]; |
| 925 int i, bnd, rbnd, grp, seg; | 966 ctx->samples[start] = tmp0 + tmp1; |
| 926 GetBitContext *gb = &ctx->gb; | 967 (ctx->samples + 256)[start] = tmp0 - tmp1; |
| 927 uint32_t *flags = &ab->flags; | 968 start++; |
| 928 int bit_alloc_flags = 0; | 969 } |
| 929 | 970 |
| 930 *flags = 0; | 971 return 0; |
| 931 ab->blksw = 0; | 972 } |
| 932 for (i = 0; i < nfchans; i++) /*block switch flag */ | 973 |
| 933 ab->blksw |= get_bits_long (gb, 1) << i; | 974 static void do_rematrixing(AC3DecodeContext *ctx) |
| 934 ab->dithflag = 0; | 975 { |
| 935 for (i = 0; i < nfchans; i++) /* dithering flag */ | 976 ac3_audio_block *ab = &ctx->audio_block; |
| 936 ab->dithflag |= get_bits_long (gb, 1) << i; | 977 uint8_t bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61; |
| 937 if (get_bits_long (gb, 1)) { /* dynamic range */ | 978 uint8_t bndend; |
| 938 *flags |= AC3_AB_DYNRNGE; | 979 |
| 939 ab->dynrng = get_bits_long (gb, 8); | 980 bndend = FFMIN(ab->endmant[0], ab->endmant[1]); |
| 940 } | 981 if (ab->rematflg & 1) |
| 941 if (acmod == 0x00) { /* dynamic range 1+1 mode */ | 982 _do_rematrixing(ctx, bnd1, bnd2); |
| 942 if (get_bits_long (gb, 1)) { | 983 if (ab->rematflg & 2) |
| 943 *flags |= AC3_AB_DYNRNG2E; | 984 _do_rematrixing(ctx, bnd2, bnd3); |
| 944 ab->dynrng2 = get_bits_long (gb, 8); | 985 if (ab->rematflg & 4) { |
| 945 } | 986 if (ab->cplbegf > 0 && ab->cplbegf <= 2 && (ab->flags & AC3_AB_CPLINU)) |
| 946 } | 987 _do_rematrixing(ctx, bnd3, bndend); |
| 947 ab->chincpl = 0; | 988 else { |
| 948 if (get_bits_long (gb, 1)) { /* coupling strategy */ | 989 _do_rematrixing(ctx, bnd3, bnd4); |
| 949 *flags |= AC3_AB_CPLSTRE; | 990 if (ab->rematflg & 8) |
| 950 ab->cplbndstrc = 0; | 991 _do_rematrixing(ctx, bnd4, bndend); |
| 951 if (get_bits_long (gb, 1)) { /* coupling in use */ | 992 } |
| 952 *flags |= AC3_AB_CPLINU; | 993 } |
| 953 for (i = 0; i < nfchans; i++) | 994 } |
| 954 ab->chincpl |= get_bits_long (gb, 1) << i; | 995 |
| 955 if (acmod == 0x02) | 996 static void get_downmix_coeffs(AC3DecodeContext *ctx) |
| 956 if (get_bits_long (gb, 1)) /* phase flag in use */ | 997 { |
| 957 *flags |= AC3_AB_PHSFLGINU; | 998 int from = ctx->bsi.acmod; |
| 958 ab->cplbegf = get_bits_long (gb, 4); | 999 int to = ctx->output; |
| 959 ab->cplendf = get_bits_long (gb, 4); | 1000 float clev = clevs[ctx->bsi.cmixlev]; |
| 960 if ((ab->ncplsubnd = 3 + ab->cplendf - ab->cplbegf) < 0) | 1001 float slev = slevs[ctx->bsi.surmixlev]; |
| 961 return -1; | 1002 ac3_audio_block *ab = &ctx->audio_block; |
| 962 ab->ncplbnd = ab->ncplsubnd; | 1003 |
| 963 for (i = 0; i < ab->ncplsubnd - 1; i++) /* coupling band structure */ | 1004 if (to == AC3_OUTPUT_UNMODIFIED) |
| 964 if (get_bits_long (gb, 1)) { | 1005 return 0; |
| 965 ab->cplbndstrc |= 1 << i; | 1006 |
| 966 ab->ncplbnd--; | 1007 switch (from) { |
| 967 } | 1008 case AC3_INPUT_DUALMONO: |
| 968 } | 1009 switch (to) { |
| 969 } | 1010 case AC3_OUTPUT_MONO: |
| 970 if (*flags & AC3_AB_CPLINU) { | 1011 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */ |
| 971 ab->cplcoe = 0; | 1012 ab->chcoeffs[0] *= LEVEL_MINUS_6DB; |
| 972 for (i = 0; i < nfchans; i++) | 1013 ab->chcoeffs[1] *= LEVEL_MINUS_6DB; |
| 973 if (ab->chincpl & (1 << i)) | 1014 break; |
| 974 if (get_bits_long (gb, 1)) { /* coupling co-ordinates */ | 1015 } |
| 975 ab->cplcoe |= 1 << i; | 1016 break; |
| 976 ab->mstrcplco[i] = get_bits_long (gb, 2); | 1017 case AC3_INPUT_MONO: |
| 977 for (bnd = 0; bnd < ab->ncplbnd; bnd++) { | 1018 switch (to) { |
| 978 ab->cplcoexp[i][bnd] = get_bits_long (gb, 4); | 1019 case AC3_OUTPUT_STEREO: |
| 979 ab->cplcomant[i][bnd] = get_bits_long (gb, 4); | 1020 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 980 } | 1021 break; |
| 981 } | 1022 } |
| 982 } | 1023 break; |
| 983 ab->phsflg = 0; | 1024 case AC3_INPUT_STEREO: |
| 984 if ((acmod == 0x02) && (*flags & AC3_AB_PHSFLGINU) && (ab->cplcoe & 1 || ab->cplcoe & (1 << 1))) { | 1025 switch (to) { |
| 985 for (bnd = 0; bnd < ab->ncplbnd; bnd++) | 1026 case AC3_OUTPUT_MONO: |
| 986 if (get_bits_long (gb, 1)) | 1027 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 987 ab->phsflg |= 1 << bnd; | 1028 ab->chcoeffs[1] *= LEVEL_MINUS_3DB; |
| 988 } | 1029 break; |
| 989 generate_coupling_coordinates (ctx); | 1030 } |
| 990 ab->rematflg = 0; | 1031 break; |
| 991 if (acmod == 0x02) /* rematrixing */ | 1032 case AC3_INPUT_3F: |
| 992 if (get_bits_long (gb, 1)) { | 1033 switch (to) { |
| 993 *flags |= AC3_AB_REMATSTR; | 1034 case AC3_OUTPUT_MONO: |
| 994 if (ab->cplbegf > 2 || !(*flags & AC3_AB_CPLINU)) | 1035 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 995 for (rbnd = 0; rbnd < 4; rbnd++) | 1036 ab->chcoeffs[2] *= LEVEL_MINUS_3DB; |
| 996 ab->rematflg |= get_bits_long (gb, 1) << bnd; | 1037 ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB; |
| 997 else if (ab->cplbegf > 0 && ab->cplbegf <= 2 && *flags & AC3_AB_CPLINU) | 1038 break; |
| 998 for (rbnd = 0; rbnd < 3; rbnd++) | 1039 case AC3_OUTPUT_STEREO: |
| 999 ab->rematflg |= get_bits_long (gb, 1) << bnd; | 1040 ab->chcoeffs[1] *= clev; |
| 1000 else if (!(ab->cplbegf) && *flags & AC3_AB_CPLINU) | 1041 break; |
| 1001 for (rbnd = 0; rbnd < 2; rbnd++) | 1042 } |
| 1002 ab->rematflg |= get_bits_long (gb, 1) << bnd; | 1043 break; |
| 1003 } | 1044 case AC3_INPUT_2F_1R: |
| 1004 if (*flags & AC3_AB_CPLINU) /* coupling exponent strategy */ | 1045 switch (to) { |
| 1005 ab->cplexpstr = get_bits_long (gb, 2); | 1046 case AC3_OUTPUT_MONO: |
| 1006 for (i = 0; i < nfchans; i++) /* channel exponent strategy */ | 1047 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 1007 ab->chexpstr[i] = get_bits_long (gb, 2); | 1048 ab->chcoeffs[1] *= LEVEL_MINUS_3DB; |
| 1008 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponent strategy */ | 1049 ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB; |
| 1009 ab->lfeexpstr = get_bits_long (gb, 1); | 1050 break; |
| 1010 for (i = 0; i < nfchans; i++) /* channel bandwidth code */ | 1051 case AC3_OUTPUT_STEREO: |
| 1011 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) | 1052 ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB; |
| 1012 if (!(ab->chincpl & (1 << i))) { | 1053 break; |
| 1013 ab->chbwcod[i] = get_bits_long (gb, 6); | 1054 case AC3_OUTPUT_DOLBY: |
| 1014 if (ab->chbwcod[i] > 60) | 1055 ab->chcoeffs[2] *= LEVEL_MINUS_3DB; |
| 1015 return -1; | 1056 break; |
| 1016 } | 1057 } |
| 1017 if (*flags & AC3_AB_CPLINU) | 1058 break; |
| 1018 if (ab->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */ | 1059 case AC3_INPUT_3F_1R: |
| 1019 bit_alloc_flags |= 64; | 1060 switch (to) { |
| 1020 ab->cplabsexp = get_bits_long (gb, 4) << 1; | 1061 case AC3_OUTPUT_MONO: |
| 1021 ab->cplstrtmant = (ab->cplbegf * 12) + 37; | 1062 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 1022 ab->cplendmant = ((ab->cplendmant + 3) * 12) + 37; | 1063 ab->chcoeffs[2] *= LEVEL_MINUS_3DB; |
| 1023 ab->ncplgrps = (ab->cplendmant - ab->cplstrtmant) / (3 << (ab->cplexpstr - 1)); | 1064 ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB; |
| 1024 for (grp = 0; grp < ab->ncplgrps; grp++) | 1065 ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB; |
| 1025 ab->cplexps[grp] = get_bits_long (gb, 7); | 1066 break; |
| 1026 } | 1067 case AC3_OUTPUT_STEREO: |
| 1027 for (i = 0; i < nfchans; i++) /* fbw channel exponents */ | 1068 ab->chcoeffs[1] *= clev; |
| 1028 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) { | 1069 ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB; |
| 1029 bit_alloc_flags |= 1 << i; | 1070 break; |
| 1030 if (ab->chincpl & (1 << i)) | 1071 case AC3_OUTPUT_DOLBY: |
| 1031 ab->endmant[i] = (ab->cplbegf * 12) + 37; | 1072 ab->chcoeffs[1] *= LEVEL_MINUS_3DB; |
| 1032 else | 1073 ab->chcoeffs[3] *= LEVEL_MINUS_3DB; |
| 1033 ab->endmant[i] = ((ab->chbwcod[i] + 3) * 12) + 37; | 1074 break; |
| 1034 ab->nchgrps[i] = | 1075 } |
| 1035 (ab->endmant[i] + (3 << (ab->chexpstr[i] - 1)) - 4) / (3 << (ab->chexpstr[i] - 1)); | 1076 break; |
| 1036 ab->exps[i][0] = ab->dexps[i][0] = get_bits_long (gb, 4); | 1077 case AC3_INPUT_2F_2R: |
| 1037 for (grp = 1; grp <= ab->nchgrps[i]; grp++) | 1078 switch (to) { |
| 1038 ab->exps[i][grp] = get_bits_long (gb, 7); | 1079 case AC3_OUTPUT_MONO: |
| 1039 ab->gainrng[i] = get_bits_long (gb, 2); | 1080 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 1040 } | 1081 ab->chcoeffs[1] *= LEVEL_MINUS_3DB; |
| 1041 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponents */ | 1082 ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB; |
| 1042 if (ab->lfeexpstr != AC3_EXPSTR_REUSE) { | 1083 ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB; |
| 1043 bit_alloc_flags |= 32; | 1084 break; |
| 1044 ab->lfeexps[0] = ab->dlfeexps[0] = get_bits_long (gb, 4); | 1085 case AC3_OUTPUT_STEREO: |
| 1045 ab->lfeexps[1] = get_bits_long (gb, 7); | 1086 ab->chcoeffs[2] *= slev; |
| 1046 ab->lfeexps[2] = get_bits_long (gb, 7); | 1087 ab->chcoeffs[3] *= slev; |
| 1047 } | 1088 break; |
| 1048 if (decode_exponents (ctx)) /* decode the exponents for this block */ | 1089 case AC3_OUTPUT_DOLBY: |
| 1049 return -1; | 1090 ab->chcoeffs[2] *= LEVEL_MINUS_3DB; |
| 1050 if (get_bits_long (gb, 1)) { /* bit allocation information */ | 1091 ab->chcoeffs[3] *= LEVEL_MINUS_3DB; |
| 1051 *flags |= AC3_AB_BAIE; | 1092 break; |
| 1052 bit_alloc_flags |= 127; | 1093 } |
| 1053 ab->sdcycod = get_bits_long (gb, 2); | 1094 break; |
| 1054 ab->fdcycod = get_bits_long (gb, 2); | 1095 case AC3_INPUT_3F_2R: |
| 1055 ab->sgaincod = get_bits_long (gb, 2); | 1096 switch (to) { |
| 1056 ab->dbpbcod = get_bits_long (gb, 2); | 1097 case AC3_OUTPUT_MONO: |
| 1057 ab->floorcod = get_bits_long (gb, 3); | 1098 ab->chcoeffs[0] *= LEVEL_MINUS_3DB; |
| 1058 } | 1099 ab->chcoeffs[2] *= LEVEL_MINUS_3DB; |
| 1059 if (get_bits_long (gb, 1)) { /* snroffset */ | 1100 ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB; |
| 1060 *flags |= AC3_AB_SNROFFSTE; | 1101 ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB; |
| 1061 bit_alloc_flags |= 127; | 1102 ab->chcoeffs[4] *= slev * LEVEL_MINUS_3DB; |
| 1062 ab->csnroffst = get_bits_long (gb, 6); | 1103 break; |
| 1063 if (*flags & AC3_AB_CPLINU) { /* couling fine snr offset and fast gain code */ | 1104 case AC3_OUTPUT_STEREO: |
| 1064 ab->cplfsnroffst = get_bits_long (gb, 4); | 1105 ab->chcoeffs[1] *= clev; |
| 1065 ab->cplfgaincod = get_bits_long (gb, 3); | 1106 ab->chcoeffs[3] *= slev; |
| 1066 } | 1107 ab->chcoeffs[4] *= slev; |
| 1067 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */ | 1108 break; |
| 1068 ab->fsnroffst[i] = get_bits_long (gb, 4); | 1109 case AC3_OUTPUT_DOLBY: |
| 1069 ab->fgaincod[i] = get_bits_long (gb, 3); | 1110 ab->chcoeffs[1] *= LEVEL_MINUS_3DB; |
| 1070 } | 1111 ab->chcoeffs[3] *= LEVEL_MINUS_3DB; |
| 1071 if (ctx->bsi.flags & AC3_BSI_LFEON) { /* lfe fine snr offset and fast gain code */ | 1112 ab->chcoeffs[4] *= LEVEL_MINUS_3DB; |
| 1072 ab->lfefsnroffst = get_bits_long (gb, 4); | 1113 break; |
| 1073 ab->lfefgaincod = get_bits_long (gb, 3); | 1114 } |
| 1074 } | 1115 break; |
| 1075 } | 1116 } |
| 1076 if (*flags & AC3_AB_CPLINU) | 1117 } |
| 1077 if (get_bits_long (gb, 1)) { /* coupling leak information */ | 1118 |
| 1078 bit_alloc_flags |= 64; | 1119 static inline void downmix_dualmono_to_mono(float *samples) |
| 1079 *flags |= AC3_AB_CPLLEAKE; | 1120 { |
| 1080 ab->cplfleak = get_bits_long (gb, 3); | 1121 int i; |
| 1081 ab->cplsleak = get_bits_long (gb, 3); | 1122 |
| 1082 } | 1123 for (i = 0; i < 256; i++) { |
| 1083 if (get_bits_long (gb, 1)) { /* delta bit allocation information */ | 1124 samples[i] += samples[i + 256]; |
| 1084 *flags |= AC3_AB_DELTBAIE; | 1125 samples[i + 256] = 0; |
| 1085 bit_alloc_flags |= 127; | 1126 } |
| 1127 } | |
| 1128 | |
| 1129 static inline void downmix_dualmono_to_stereo(float *samples) | |
| 1130 { | |
| 1131 int i; | |
| 1132 float tmp; | |
| 1133 | |
| 1134 for (i = 0; i < 256; i++) { | |
| 1135 tmp = samples[i] + samples[i + 256]; | |
| 1136 samples[i] = samples[i + 256] = tmp; | |
| 1137 } | |
| 1138 } | |
| 1139 | |
| 1140 static inline void downmix_mono_to_stereo(float *samples) | |
| 1141 { | |
| 1142 int i; | |
| 1143 | |
| 1144 for (i = 0; i < 256; i++) | |
| 1145 samples[i + 256] = samples[i]; | |
| 1146 } | |
| 1147 | |
| 1148 static inline void downmix_stereo_to_mono(float *samples) | |
| 1149 { | |
| 1150 int i; | |
| 1151 | |
| 1152 for (i = 0; i < 256; i++) { | |
| 1153 samples[i] += samples[i + 256]; | |
| 1154 samples[i + 256] = 0; | |
| 1155 } | |
| 1156 } | |
| 1157 | |
| 1158 static inline void downmix_3f_to_mono(float *samples) | |
| 1159 { | |
| 1160 int i; | |
| 1161 | |
| 1162 for (i = 0; i < 256; i++) { | |
| 1163 samples[i] += (samples[i + 256] + samples[i + 512]); | |
| 1164 samples[i + 256] = samples[i + 512] = 0; | |
| 1165 } | |
| 1166 } | |
| 1167 | |
| 1168 static inline void downmix_3f_to_stereo(float *samples) | |
| 1169 { | |
| 1170 int i; | |
| 1171 | |
| 1172 for (i = 0; i < 256; i++) { | |
| 1173 samples[i] += samples[i + 256]; | |
| 1174 samples[i + 256] = samples[i + 512]; | |
| 1175 samples[i + 512] = 0; | |
| 1176 } | |
| 1177 } | |
| 1178 | |
| 1179 static inline void downmix_2f_1r_to_mono(float *samples) | |
| 1180 { | |
| 1181 int i; | |
| 1182 | |
| 1183 for (i = 0; i < 256; i++) { | |
| 1184 samples[i] += (samples[i + 256] + samples[i + 512]); | |
| 1185 samples[i + 256] = samples[i + 512] = 0; | |
| 1186 } | |
| 1187 } | |
| 1188 | |
| 1189 static inline void downmix_2f_1r_to_stereo(float *samples) | |
| 1190 { | |
| 1191 int i; | |
| 1192 | |
| 1193 for (i = 0; i < 256; i++) { | |
| 1194 samples[i] += samples[i + 512]; | |
| 1195 samples[i + 256] += samples[i + 512]; | |
| 1196 samples[i + 512] = 0; | |
| 1197 } | |
| 1198 } | |
| 1199 | |
| 1200 static inline void downmix_2f_1r_to_dolby(float *samples) | |
| 1201 { | |
| 1202 int i; | |
| 1203 | |
| 1204 for (i = 0; i < 256; i++) { | |
| 1205 samples[i] -= samples[i + 512]; | |
| 1206 samples[i + 256] += samples[i + 512]; | |
| 1207 samples[i + 512] = 0; | |
| 1208 } | |
| 1209 } | |
| 1210 | |
| 1211 static inline void downmix_3f_1r_to_mono(float *samples) | |
| 1212 { | |
| 1213 int i; | |
| 1214 | |
| 1215 for (i = 0; i < 256; i++) { | |
| 1216 samples[i] += (samples[i + 256] + samples[i + 512] + samples[i + 768]); | |
| 1217 samples[i + 256] = samples[i + 512] = samples[i + 768] = 0; | |
| 1218 } | |
| 1219 } | |
| 1220 | |
| 1221 static inline void downmix_3f_1r_to_stereo(float *samples) | |
| 1222 { | |
| 1223 int i; | |
| 1224 | |
| 1225 for (i = 0; i < 256; i++) { | |
| 1226 samples[i] += (samples[i + 256] + samples[i + 768]); | |
| 1227 samples[i + 256] += (samples[i + 512] + samples[i + 768]); | |
| 1228 samples[i + 512] = samples[i + 768] = 0; | |
| 1229 } | |
| 1230 } | |
| 1231 | |
| 1232 static inline void downmix_3f_1r_to_dolby(float *samples) | |
| 1233 { | |
| 1234 int i; | |
| 1235 | |
| 1236 for (i = 0; i < 256; i++) { | |
| 1237 samples[i] += (samples[i + 256] - samples[i + 768]); | |
| 1238 samples[i + 256] += (samples[i + 512] + samples[i + 768]); | |
| 1239 samples[i + 512] = samples[i + 768] = 0; | |
| 1240 } | |
| 1241 } | |
| 1242 | |
| 1243 static inline void downmix_2f_2r_to_mono(float *samples) | |
| 1244 { | |
| 1245 int i; | |
| 1246 | |
| 1247 for (i = 0; i < 256; i++) { | |
| 1248 samples[i] += (samples[i + 256] + samples[i + 512] + samples[i + 768]); | |
| 1249 samples[i + 256] = samples[i + 512] = samples[i + 768] = 0; | |
| 1250 } | |
| 1251 } | |
| 1252 | |
| 1253 static inline void downmix_2f_2r_to_stereo(float *samples) | |
| 1254 { | |
| 1255 int i; | |
| 1256 | |
| 1257 for (i = 0; i < 256; i++) { | |
| 1258 samples[i] += samples[i + 512]; | |
| 1259 samples[i + 256] = samples[i + 768]; | |
| 1260 samples[i + 512] = samples[i + 768] = 0; | |
| 1261 } | |
| 1262 } | |
| 1263 | |
| 1264 static inline void downmix_2f_2r_to_dolby(float *samples) | |
| 1265 { | |
| 1266 int i; | |
| 1267 | |
| 1268 for (i = 0; i < 256; i++) { | |
| 1269 samples[i] -= samples[i + 512]; | |
| 1270 samples[i + 256] += samples[i + 768]; | |
| 1271 samples[i + 512] = samples[i + 768] = 0; | |
| 1272 } | |
| 1273 } | |
| 1274 | |
| 1275 static inline void downmix_3f_2r_to_mono(float *samples) | |
| 1276 { | |
| 1277 int i; | |
| 1278 | |
| 1279 for (i = 0; i < 256; i++) { | |
| 1280 samples[i] += (samples[i + 256] + samples[i + 512] + samples[i + 768] + samples[i + 1024]); | |
| 1281 samples[i + 256] = samples[i + 512] = samples[i + 768] = samples[i + 1024] = 0; | |
| 1282 } | |
| 1283 } | |
| 1284 | |
| 1285 static inline void downmix_3f_2r_to_stereo(float *samples) | |
| 1286 { | |
| 1287 int i; | |
| 1288 | |
| 1289 for (i = 0; i < 256; i++) { | |
| 1290 samples[i] += (samples[i + 256] + samples[i + 768]); | |
| 1291 samples[i + 256] = (samples[i + 512] + samples[i + 1024]); | |
| 1292 samples[i + 512] = samples[i + 768] = samples[i + 1024] = 0; | |
| 1293 } | |
| 1294 } | |
| 1295 | |
| 1296 static inline void downmix_3f_2r_to_dolby(float *samples) | |
| 1297 { | |
| 1298 int i; | |
| 1299 | |
| 1300 for (i = 0; i < 256; i++) { | |
| 1301 samples[i] += (samples[i + 256] - samples[i + 768]); | |
| 1302 samples[i + 256] = (samples[i + 512] + samples[i + 1024]); | |
| 1303 samples[i + 512] = samples[i + 768] = samples[i + 1024] = 0; | |
| 1304 } | |
| 1305 } | |
| 1306 | |
| 1307 static void do_downmix(AC3DecodeContext *ctx) | |
| 1308 { | |
| 1309 int from = ctx->bsi.acmod; | |
| 1310 int to = ctx->output; | |
| 1311 float *samples = ctx->samples + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0); | |
| 1312 | |
| 1313 switch (from) { | |
| 1314 case AC3_INPUT_DUALMONO: | |
| 1315 switch (to) { | |
| 1316 case AC3_OUTPUT_MONO: | |
| 1317 downmix_dualmono_to_mono(samples); | |
| 1318 break; | |
| 1319 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */ | |
| 1320 downmix_dualmono_to_stereo(samples); | |
| 1321 break; | |
| 1322 } | |
| 1323 break; | |
| 1324 case AC3_INPUT_MONO: | |
| 1325 switch (to) { | |
| 1326 case AC3_OUTPUT_STEREO: | |
| 1327 downmix_mono_to_stereo(samples); | |
| 1328 break; | |
| 1329 } | |
| 1330 break; | |
| 1331 case AC3_INPUT_STEREO: | |
| 1332 switch (to) { | |
| 1333 case AC3_OUTPUT_MONO: | |
| 1334 downmix_stereo_to_mono(samples); | |
| 1335 break; | |
| 1336 } | |
| 1337 break; | |
| 1338 case AC3_INPUT_3F: | |
| 1339 switch (to) { | |
| 1340 case AC3_OUTPUT_MONO: | |
| 1341 downmix_3f_to_mono(samples); | |
| 1342 break; | |
| 1343 case AC3_OUTPUT_STEREO: | |
| 1344 downmix_3f_to_stereo(samples); | |
| 1345 break; | |
| 1346 } | |
| 1347 break; | |
| 1348 case AC3_INPUT_2F_1R: | |
| 1349 switch (to) { | |
| 1350 case AC3_OUTPUT_MONO: | |
| 1351 downmix_2f_1r_to_mono(samples); | |
| 1352 break; | |
| 1353 case AC3_OUTPUT_STEREO: | |
| 1354 downmix_2f_1r_to_stereo(samples); | |
| 1355 break; | |
| 1356 case AC3_OUTPUT_DOLBY: | |
| 1357 downmix_2f_1r_to_dolby(samples); | |
| 1358 break; | |
| 1359 } | |
| 1360 break; | |
| 1361 case AC3_INPUT_3F_1R: | |
| 1362 switch (to) { | |
| 1363 case AC3_OUTPUT_MONO: | |
| 1364 downmix_3f_1r_to_mono(samples); | |
| 1365 break; | |
| 1366 case AC3_OUTPUT_STEREO: | |
| 1367 downmix_3f_1r_to_stereo(samples); | |
| 1368 break; | |
| 1369 case AC3_OUTPUT_DOLBY: | |
| 1370 downmix_3f_1r_to_dolby(samples); | |
| 1371 break; | |
| 1372 } | |
| 1373 break; | |
| 1374 case AC3_INPUT_2F_2R: | |
| 1375 switch (to) { | |
| 1376 case AC3_OUTPUT_MONO: | |
| 1377 downmix_2f_2r_to_mono(samples); | |
| 1378 break; | |
| 1379 case AC3_OUTPUT_STEREO: | |
| 1380 downmix_2f_2r_to_stereo(samples); | |
| 1381 break; | |
| 1382 case AC3_OUTPUT_DOLBY: | |
| 1383 downmix_2f_2r_to_dolby(samples); | |
| 1384 break; | |
| 1385 } | |
| 1386 break; | |
| 1387 case AC3_INPUT_3F_2R: | |
| 1388 switch (to) { | |
| 1389 case AC3_OUTPUT_MONO: | |
| 1390 downmix_3f_2r_to_mono(samples); | |
| 1391 break; | |
| 1392 case AC3_OUTPUT_STEREO: | |
| 1393 downmix_3f_2r_to_stereo(samples); | |
| 1394 break; | |
| 1395 case AC3_OUTPUT_DOLBY: | |
| 1396 downmix_3f_2r_to_dolby(samples); | |
| 1397 break; | |
| 1398 } | |
| 1399 break; | |
| 1400 } | |
| 1401 } | |
| 1402 | |
| 1403 static int ac3_parse_audio_block(AC3DecodeContext * ctx, int index) | |
| 1404 { | |
| 1405 ac3_audio_block *ab = &ctx->audio_block; | |
| 1406 int nfchans = ctx->bsi.nfchans; | |
| 1407 int acmod = ctx->bsi.acmod; | |
| 1408 int i, bnd, rbnd, grp, seg; | |
| 1409 GetBitContext *gb = &ctx->gb; | |
| 1410 uint32_t *flags = &ab->flags; | |
| 1411 int bit_alloc_flags = 0; | |
| 1412 float drange; | |
| 1413 | |
| 1414 *flags = 0; | |
| 1415 ab->blksw = 0; | |
| 1416 for (i = 0; i < 5; i++) | |
| 1417 ab->chcoeffs[i] = 1.0; | |
| 1418 for (i = 0; i < nfchans; i++) /*block switch flag */ | |
| 1419 ab->blksw |= get_bits(gb, 1) << i; | |
| 1420 ab->dithflag = 0; | |
| 1421 for (i = 0; i < nfchans; i++) /* dithering flag */ | |
| 1422 ab->dithflag |= get_bits(gb, 1) << i; | |
| 1423 if (get_bits(gb, 1)) { /* dynamic range */ | |
| 1424 *flags |= AC3_AB_DYNRNGE; | |
| 1425 ab->dynrng = get_bits(gb, 8); | |
| 1426 drange = ((((ab->dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (ab->dynrng >> 5)]); | |
| 1427 for (i = 0; i < nfchans; i++) | |
| 1428 ab->chcoeffs[i] *= drange; | |
| 1429 } | |
| 1430 if (acmod == 0x00) { /* dynamic range 1+1 mode */ | |
| 1431 if (get_bits(gb, 1)) { | |
| 1432 *flags |= AC3_AB_DYNRNG2E; | |
| 1433 ab->dynrng2 = get_bits(gb, 8); | |
| 1434 drange = ((((ab->dynrng2 & 0x1f) | 0x20) << 13) * scale_factors[3 - (ab->dynrng2 >> 5)]); | |
| 1435 ab->chcoeffs[1] *= drange; | |
| 1436 } | |
| 1437 } | |
| 1438 get_downmix_coeffs(ctx); | |
| 1439 ab->chincpl = 0; | |
| 1440 if (get_bits(gb, 1)) { /* coupling strategy */ | |
| 1441 *flags |= AC3_AB_CPLSTRE; | |
| 1442 ab->cplbndstrc = 0; | |
| 1443 if (get_bits(gb, 1)) { /* coupling in use */ | |
| 1444 *flags |= AC3_AB_CPLINU; | |
| 1445 for (i = 0; i < nfchans; i++) | |
| 1446 ab->chincpl |= get_bits(gb, 1) << i; | |
| 1447 if (acmod == 0x02) | |
| 1448 if (get_bits(gb, 1)) /* phase flag in use */ | |
| 1449 *flags |= AC3_AB_PHSFLGINU; | |
| 1450 ab->cplbegf = get_bits(gb, 4); | |
| 1451 ab->cplendf = get_bits(gb, 4); | |
| 1452 assert((ab->ncplsubnd = 3 + ab->cplendf - ab->cplbegf) > 0); | |
| 1453 ab->ncplbnd = ab->ncplsubnd; | |
| 1454 for (i = 0; i < ab->ncplsubnd - 1; i++) /* coupling band structure */ | |
| 1455 if (get_bits(gb, 1)) { | |
| 1456 ab->cplbndstrc |= 1 << i; | |
| 1457 ab->ncplbnd--; | |
| 1458 } | |
| 1459 } | |
| 1460 } | |
| 1086 if (*flags & AC3_AB_CPLINU) { | 1461 if (*flags & AC3_AB_CPLINU) { |
| 1087 ab->cpldeltbae = get_bits_long (gb, 2); | 1462 ab->cplcoe = 0; |
| 1088 if (ab->cpldeltbae == AC3_DBASTR_RESERVED) | 1463 for (i = 0; i < nfchans; i++) |
| 1464 if (ab->chincpl & (1 << i)) | |
| 1465 if (get_bits(gb, 1)) { /* coupling co-ordinates */ | |
| 1466 ab->cplcoe |= 1 << i; | |
| 1467 ab->mstrcplco[i] = get_bits(gb, 2); | |
| 1468 for (bnd = 0; bnd < ab->ncplbnd; bnd++) { | |
| 1469 ab->cplcoexp[i][bnd] = get_bits(gb, 4); | |
| 1470 ab->cplcomant[i][bnd] = get_bits(gb, 4); | |
| 1471 } | |
| 1472 } | |
| 1473 } | |
| 1474 ab->phsflg = 0; | |
| 1475 if ((acmod == 0x02) && (*flags & AC3_AB_PHSFLGINU) && (ab->cplcoe & 1 || ab->cplcoe & (1 << 1))) { | |
| 1476 for (bnd = 0; bnd < ab->ncplbnd; bnd++) | |
| 1477 if (get_bits(gb, 1)) | |
| 1478 ab->phsflg |= 1 << bnd; | |
| 1479 } | |
| 1480 generate_coupling_coordinates(ctx); | |
| 1481 ab->rematflg = 0; | |
| 1482 if (acmod == 0x02) /* rematrixing */ | |
| 1483 if (get_bits(gb, 1)) { | |
| 1484 *flags |= AC3_AB_REMATSTR; | |
| 1485 if (ab->cplbegf > 2 || !(*flags & AC3_AB_CPLINU)) | |
| 1486 for (rbnd = 0; rbnd < 4; rbnd++) | |
| 1487 ab->rematflg |= get_bits(gb, 1) << bnd; | |
| 1488 else if (ab->cplbegf > 0 && ab->cplbegf <= 2 && *flags & AC3_AB_CPLINU) | |
| 1489 for (rbnd = 0; rbnd < 3; rbnd++) | |
| 1490 ab->rematflg |= get_bits(gb, 1) << bnd; | |
| 1491 else if (!(ab->cplbegf) && *flags & AC3_AB_CPLINU) | |
| 1492 for (rbnd = 0; rbnd < 2; rbnd++) | |
| 1493 ab->rematflg |= get_bits(gb, 1) << bnd; | |
| 1494 } | |
| 1495 if (*flags & AC3_AB_CPLINU) /* coupling exponent strategy */ | |
| 1496 ab->cplexpstr = get_bits(gb, 2); | |
| 1497 for (i = 0; i < nfchans; i++) /* channel exponent strategy */ | |
| 1498 ab->chexpstr[i] = get_bits(gb, 2); | |
| 1499 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponent strategy */ | |
| 1500 ab->lfeexpstr = get_bits(gb, 1); | |
| 1501 for (i = 0; i < nfchans; i++) /* channel bandwidth code */ | |
| 1502 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) | |
| 1503 if (!(ab->chincpl & (1 << i))) { | |
| 1504 ab->chbwcod[i] = get_bits(gb, 6); | |
| 1505 assert (ab->chbwcod[i] <= 60); | |
| 1506 } | |
| 1507 if (*flags & AC3_AB_CPLINU) | |
| 1508 if (ab->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */ | |
| 1509 bit_alloc_flags |= 64; | |
| 1510 ab->cplabsexp = get_bits(gb, 4) << 1; | |
| 1511 ab->cplstrtmant = (ab->cplbegf * 12) + 37; | |
| 1512 ab->cplendmant = ((ab->cplendmant + 3) * 12) + 37; | |
| 1513 ab->ncplgrps = (ab->cplendmant - ab->cplstrtmant) / (3 << (ab->cplexpstr - 1)); | |
| 1514 for (grp = 0; grp < ab->ncplgrps; grp++) | |
| 1515 ab->cplexps[grp] = get_bits(gb, 7); | |
| 1516 } | |
| 1517 for (i = 0; i < nfchans; i++) /* fbw channel exponents */ | |
| 1518 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) { | |
| 1519 bit_alloc_flags |= 1 << i; | |
| 1520 if (ab->chincpl & (1 << i)) | |
| 1521 ab->endmant[i] = (ab->cplbegf * 12) + 37; | |
| 1522 else | |
| 1523 ab->endmant[i] = ((ab->chbwcod[i] + 3) * 12) + 37; | |
| 1524 ab->nchgrps[i] = | |
| 1525 (ab->endmant[i] + (3 << (ab->chexpstr[i] - 1)) - 4) / (3 << (ab->chexpstr[i] - 1)); | |
| 1526 ab->exps[i][0] = ab->dexps[i][0] = get_bits(gb, 4); | |
| 1527 for (grp = 1; grp <= ab->nchgrps[i]; grp++) | |
| 1528 ab->exps[i][grp] = get_bits(gb, 7); | |
| 1529 ab->gainrng[i] = get_bits(gb, 2); | |
| 1530 } | |
| 1531 if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponents */ | |
| 1532 if (ab->lfeexpstr != AC3_EXPSTR_REUSE) { | |
| 1533 bit_alloc_flags |= 32; | |
| 1534 ab->lfeexps[0] = ab->dlfeexps[0] = get_bits(gb, 4); | |
| 1535 ab->lfeexps[1] = get_bits(gb, 7); | |
| 1536 ab->lfeexps[2] = get_bits(gb, 7); | |
| 1537 } | |
| 1538 if (decode_exponents(ctx)) {/* decode the exponents for this block */ | |
| 1539 av_log(NULL, AV_LOG_ERROR, "Error parsing exponents\n"); | |
| 1089 return -1; | 1540 return -1; |
| 1090 } | 1541 } |
| 1091 for (i = 0; i < nfchans; i++) { | 1542 |
| 1092 ab->deltbae[i] = get_bits_long (gb, 2); | 1543 if (get_bits(gb, 1)) { /* bit allocation information */ |
| 1093 if (ab->deltbae[i] == AC3_DBASTR_RESERVED) | 1544 *flags |= AC3_AB_BAIE; |
| 1545 bit_alloc_flags |= 127; | |
| 1546 ab->sdcycod = get_bits(gb, 2); | |
| 1547 ab->fdcycod = get_bits(gb, 2); | |
| 1548 ab->sgaincod = get_bits(gb, 2); | |
| 1549 ab->dbpbcod = get_bits(gb, 2); | |
| 1550 ab->floorcod = get_bits(gb, 3); | |
| 1551 } | |
| 1552 if (get_bits(gb, 1)) { /* snroffset */ | |
| 1553 *flags |= AC3_AB_SNROFFSTE; | |
| 1554 bit_alloc_flags |= 127; | |
| 1555 ab->csnroffst = get_bits(gb, 6); | |
| 1556 if (*flags & AC3_AB_CPLINU) { /* couling fine snr offset and fast gain code */ | |
| 1557 ab->cplfsnroffst = get_bits(gb, 4); | |
| 1558 ab->cplfgaincod = get_bits(gb, 3); | |
| 1559 } | |
| 1560 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */ | |
| 1561 ab->fsnroffst[i] = get_bits(gb, 4); | |
| 1562 ab->fgaincod[i] = get_bits(gb, 3); | |
| 1563 } | |
| 1564 if (ctx->bsi.flags & AC3_BSI_LFEON) { /* lfe fine snr offset and fast gain code */ | |
| 1565 ab->lfefsnroffst = get_bits(gb, 4); | |
| 1566 ab->lfefgaincod = get_bits(gb, 3); | |
| 1567 } | |
| 1568 } | |
| 1569 if (*flags & AC3_AB_CPLINU) | |
| 1570 if (get_bits(gb, 1)) { /* coupling leak information */ | |
| 1571 bit_alloc_flags |= 64; | |
| 1572 *flags |= AC3_AB_CPLLEAKE; | |
| 1573 ab->cplfleak = get_bits(gb, 3); | |
| 1574 ab->cplsleak = get_bits(gb, 3); | |
| 1575 } | |
| 1576 if (get_bits(gb, 1)) { /* delta bit allocation information */ | |
| 1577 *flags |= AC3_AB_DELTBAIE; | |
| 1578 bit_alloc_flags |= 127; | |
| 1579 if (*flags & AC3_AB_CPLINU) { | |
| 1580 ab->cpldeltbae = get_bits(gb, 2); | |
| 1581 if (ab->cpldeltbae == AC3_DBASTR_RESERVED) { | |
| 1582 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n"); | |
| 1583 return -1; | |
| 1584 } | |
| 1585 } | |
| 1586 for (i = 0; i < nfchans; i++) { | |
| 1587 ab->deltbae[i] = get_bits(gb, 2); | |
| 1588 if (ab->deltbae[i] == AC3_DBASTR_RESERVED) { | |
| 1589 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n"); | |
| 1590 return -1; | |
| 1591 } | |
| 1592 } | |
| 1593 if (*flags & AC3_AB_CPLINU) | |
| 1594 if (ab->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */ | |
| 1595 ab->cpldeltnseg = get_bits(gb, 3); | |
| 1596 for (seg = 0; seg <= ab->cpldeltnseg; seg++) { | |
| 1597 ab->cpldeltoffst[seg] = get_bits(gb, 5); | |
| 1598 ab->cpldeltlen[seg] = get_bits(gb, 4); | |
| 1599 ab->cpldeltba[seg] = get_bits(gb, 3); | |
| 1600 } | |
| 1601 } | |
| 1602 for (i = 0; i < nfchans; i++) | |
| 1603 if (ab->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */ | |
| 1604 ab->deltnseg[i] = get_bits(gb, 3); | |
| 1605 for (seg = 0; seg <= ab->deltnseg[i]; seg++) { | |
| 1606 ab->deltoffst[i][seg] = get_bits(gb, 5); | |
| 1607 ab->deltlen[i][seg] = get_bits(gb, 4); | |
| 1608 ab->deltba[i][seg] = get_bits(gb, 3); | |
| 1609 } | |
| 1610 } | |
| 1611 } | |
| 1612 if (do_bit_allocation (ctx, bit_alloc_flags)) /* perform the bit allocation */ { | |
| 1613 av_log(NULL, AV_LOG_ERROR, "Error in bit allocation routine\n"); | |
| 1094 return -1; | 1614 return -1; |
| 1095 } | 1615 } |
| 1096 if (*flags & AC3_AB_CPLINU) | 1616 if (get_bits(gb, 1)) { /* unused dummy data */ |
| 1097 if (ab->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */ | 1617 *flags |= AC3_AB_SKIPLE; |
| 1098 ab->cpldeltnseg = get_bits_long (gb, 3); | 1618 ab->skipl = get_bits(gb, 9); |
| 1099 for (seg = 0; seg <= ab->cpldeltnseg; seg++) { | 1619 while (ab->skipl) { |
| 1100 ab->cpldeltoffst[seg] = get_bits_long (gb, 5); | 1620 get_bits(gb, 8); |
| 1101 ab->cpldeltlen[seg] = get_bits_long (gb, 4); | 1621 ab->skipl--; |
| 1102 ab->cpldeltba[seg] = get_bits_long (gb, 3); | 1622 } |
| 1103 } | 1623 } |
| 1104 } | 1624 /* unpack the transform coefficients |
| 1105 for (i = 0; i < nfchans; i++) | 1625 * * this also uncouples channels if coupling is in use. |
| 1106 if (ab->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */ | 1626 */ |
| 1107 ab->deltnseg[i] = get_bits_long (gb, 3); | 1627 if (get_transform_coeffs(ctx)) { |
| 1108 for (seg = 0; seg <= ab->deltnseg[i]; seg++) { | 1628 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n"); |
| 1109 ab->deltoffst[i][seg] = get_bits_long (gb, 5); | 1629 return -1; |
| 1110 ab->deltlen[i][seg] = get_bits_long (gb, 4); | 1630 } |
| 1111 ab->deltba[i][seg] = get_bits_long (gb, 3); | 1631 /* recover coefficients if rematrixing is in use */ |
| 1112 } | 1632 if (*flags & AC3_AB_REMATSTR) |
| 1113 } | 1633 do_rematrixing(ctx); |
| 1114 } | 1634 |
| 1115 if (do_bit_allocation (ctx, bit_alloc_flags)) /* perform the bit allocation */ | 1635 if (ctx->output != AC3_OUTPUT_UNMODIFIED) |
| 1116 return -1; | 1636 do_downmix(ctx); |
| 1117 if (get_bits_long (gb, 1)) { /* unused dummy data */ | 1637 |
| 1118 *flags |= AC3_AB_SKIPLE; | 1638 return 0; |
| 1119 ab->skipl = get_bits_long (gb, 9); | 1639 } |
| 1120 while (ab->skipl) { | 1640 |
| 1121 get_bits_long (gb, 8); | 1641 /**** the following two functions comes from ac3dec */ |
| 1122 ab->skipl--; | 1642 static inline int blah (int32_t i) |
| 1123 } | 1643 { |
| 1124 } | 1644 if (i > 0x43c07fff) |
| 1125 /* point ab_samples to the right place within smaples */ | 1645 return 32767; |
| 1126 if (!index) | 1646 else if (i < 0x43bf8000) |
| 1127 ab->ab_samples = ctx->samples; | 1647 return -32768; |
| 1128 else { | 1648 else |
| 1129 ab->ab_samples = ctx->samples + (i * nfchans * 256); | 1649 return i - 0x43c00000; |
| 1130 ab->ab_samples += ((ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0); | 1650 } |
| 1131 } | 1651 |
| 1132 /* unpack the transform coefficients | 1652 static inline void float_to_int (float * _f, int16_t * s16, int samples) |
| 1133 * this also uncouples channels if coupling is in use. | 1653 { |
| 1134 */ | 1654 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format |
| 1135 if (get_transform_coeffs (ctx)) | 1655 int i; |
| 1136 return -1; | 1656 |
| 1137 | 1657 for (i = 0; i < samples; i++) { |
| 1138 return 0; | 1658 s16[i] = blah (f[i]); |
| 1139 } | 1659 } |
| 1140 | 1660 } |
| 1141 | 1661 /**** end */ |
| 1142 static int | 1662 |
| 1143 ac3_decode_frame (AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, int buf_size) | 1663 |
| 1144 { | 1664 |
| 1145 AC3DecodeContext *ctx = avctx->priv_data; | 1665 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, int buf_size) |
| 1146 int frame_start; | 1666 { |
| 1147 int i; | 1667 AC3DecodeContext *ctx = avctx->priv_data; |
| 1148 | 1668 int frame_start; |
| 1149 //Synchronize the frame. | 1669 int i, j, k, l; |
| 1150 frame_start = ac3_synchronize (buf, buf_size); | 1670 float tmp0[128], tmp1[128], tmp[512]; |
| 1151 if (frame_start == -1) { | 1671 short *out_samples = (short *)data; |
| 1152 *data_size = 0; | 1672 float *samples = ctx->samples; |
| 1153 return -1; | 1673 |
| 1154 } | 1674 //Synchronize the frame. |
| 1155 | 1675 frame_start = ac3_synchronize(buf, buf_size); |
| 1156 //Initialize the GetBitContext with the start of valid AC3 Frame. | 1676 if (frame_start == -1) { |
| 1157 init_get_bits (&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8); | 1677 av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n"); |
| 1158 | 1678 *data_size = 0; |
| 1159 //Parse the syncinfo. | 1679 return -1; |
| 1160 //If 'fscod' is not valid the decoder shall mute as per the standard. | 1680 } |
| 1161 if (ac3_parse_sync_info (ctx)) { | 1681 |
| 1162 *data_size = 0; | 1682 //Initialize the GetBitContext with the start of valid AC3 Frame. |
| 1163 return -1; | 1683 init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8); |
| 1164 } | 1684 //Parse the syncinfo. |
| 1165 | 1685 ////If 'fscod' is not valid the decoder shall mute as per the standard. |
| 1166 //Check for the errors. | 1686 if (ac3_parse_sync_info(ctx)) { |
| 1167 /*if (ac3_error_check(ctx)) | 1687 av_log(avctx, AV_LOG_ERROR, "fscod is not valid\n"); |
| 1168 { | 1688 *data_size = 0; |
| 1169 *data_size = 0; | 1689 return -1; |
| 1170 return -1; | 1690 } |
| 1171 } */ | 1691 |
| 1172 | 1692 //Check for the errors. |
| 1173 //Parse the BSI. | 1693 /* if (ac3_error_check(ctx)) { |
| 1174 //If 'bsid' is not valid decoder shall not decode the audio as per the standard. | 1694 *data_size = 0; |
| 1175 if (ac3_parse_bsi (ctx)) { | 1695 return -1; |
| 1176 *data_size = 0; | 1696 } */ |
| 1177 return -1; | 1697 |
| 1178 } | 1698 //Parse the BSI. |
| 1179 | 1699 //If 'bsid' is not valid decoder shall not decode the audio as per the standard. |
| 1180 //Parse the Audio Blocks. | 1700 if (ac3_parse_bsi(ctx)) { |
| 1181 for (i = 0; i < 6; i++) | 1701 av_log(avctx, AV_LOG_ERROR, "bsid is not valid\n"); |
| 1182 if (ac3_parse_audio_block (ctx, i)) { | 1702 *data_size = 0; |
| 1183 *data_size = 0; | 1703 return -1; |
| 1184 return -1; | 1704 } |
| 1185 } | 1705 |
| 1186 | 1706 avctx->sample_rate = ctx->sync_info.sampling_rate; |
| 1187 return 0; | 1707 if (avctx->channels == 0) { |
| 1188 } | 1708 avctx->channels = ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0); |
| 1709 ctx->output = AC3_OUTPUT_UNMODIFIED; | |
| 1710 } | |
| 1711 else if ((ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0)) < avctx->channels) { | |
| 1712 av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n", | |
| 1713 avctx->channels, (ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0))); | |
| 1714 avctx->channels = ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0); | |
| 1715 ctx->output = AC3_OUTPUT_UNMODIFIED; | |
| 1716 } | |
| 1717 else if (avctx->channels == 1) { | |
| 1718 ctx->output = AC3_OUTPUT_MONO; | |
| 1719 } else if (avctx->channels == 2) { | |
| 1720 if (ctx->bsi.dsurmod == 0x02) | |
| 1721 ctx->output = AC3_OUTPUT_DOLBY; | |
| 1722 else | |
| 1723 ctx->output = AC3_OUTPUT_STEREO; | |
| 1724 } | |
| 1725 | |
| 1726 | |
| 1727 avctx->bit_rate = ctx->sync_info.bit_rate; | |
| 1728 av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->sample_rate, avctx->bit_rate); | |
| 1729 | |
| 1730 //Parse the Audio Blocks. | |
| 1731 for (i = 0; i < 6; i++) { | |
| 1732 if (ac3_parse_audio_block(ctx, i)) { | |
| 1733 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); | |
| 1734 *data_size = 0; | |
| 1735 return -1; | |
| 1736 } | |
| 1737 samples = ctx->samples; | |
| 1738 if (ctx->bsi.flags & AC3_BSI_LFEON) { | |
| 1739 ff_imdct_calc(&ctx->imdct_ctx_512, ctx->samples + 1536, samples, tmp); | |
| 1740 for (l = 0; l < 256; l++) | |
| 1741 samples[l] = (ctx->samples + 1536)[l]; | |
| 1742 float_to_int(samples, out_samples, 256); | |
| 1743 samples += 256; | |
| 1744 out_samples += 256; | |
| 1745 } | |
| 1746 for (j = 0; j < ctx->bsi.nfchans; j++) { | |
| 1747 if (ctx->audio_block.blksw & (1 << j)) { | |
| 1748 for (k = 0; k < 128; k++) { | |
| 1749 tmp0[k] = samples[2 * k]; | |
| 1750 tmp1[k] = samples[2 * k + 1]; | |
| 1751 } | |
| 1752 ff_imdct_calc(&ctx->imdct_ctx_256, ctx->samples + 1536, tmp0, tmp); | |
| 1753 for (l = 0; l < 256; l++) | |
| 1754 samples[l] = (ctx->samples + 1536)[l] * window[l] + (ctx->samples + 2048)[l] * window[255 - l]; | |
| 1755 ff_imdct_calc(&ctx->imdct_ctx_256, ctx->samples + 2048, tmp1, tmp); | |
| 1756 float_to_int(samples, out_samples, 256); | |
| 1757 samples += 256; | |
| 1758 out_samples += 256; | |
| 1759 } | |
| 1760 else { | |
| 1761 ff_imdct_calc(&ctx->imdct_ctx_512, ctx->samples + 1536, samples, tmp); | |
| 1762 for (l = 0; l < 256; l++) | |
| 1763 samples[l] = (ctx->samples + 1536)[l] * window[l] + (ctx->samples + 2048)[l] * window[255 - l]; | |
| 1764 float_to_int(samples, out_samples, 256); | |
| 1765 memcpy(ctx->samples + 2048, ctx->samples + 1792, 256 * sizeof (float)); | |
| 1766 samples += 256; | |
| 1767 out_samples += 256; | |
| 1768 } | |
| 1769 } | |
| 1770 } | |
| 1771 *data_size = 6 * ctx->bsi.nfchans * 256 * sizeof (int16_t); | |
| 1772 | |
| 1773 return (buf_size - frame_start); | |
| 1774 } | |
| 1775 | |
| 1776 static int ac3_decode_end(AVCodecContext *ctx) | |
| 1777 { | |
| 1778 return 0; | |
| 1779 } | |
| 1780 | |
| 1781 AVCodec lgpl_ac3_decoder = { | |
| 1782 "ac3", | |
| 1783 CODEC_TYPE_AUDIO, | |
| 1784 CODEC_ID_AC3, | |
| 1785 sizeof (AC3DecodeContext), | |
| 1786 ac3_decode_init, | |
| 1787 NULL, | |
| 1788 ac3_decode_end, | |
| 1789 ac3_decode_frame, | |
| 1790 }; | |
| 1791 |
