Libav
|
00001 /* 00002 * MPEG-4 ALS decoder 00003 * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00029 //#define DEBUG 00030 00031 00032 #include "avcodec.h" 00033 #include "get_bits.h" 00034 #include "unary.h" 00035 #include "mpeg4audio.h" 00036 #include "bytestream.h" 00037 #include "bgmc.h" 00038 00039 #include <stdint.h> 00040 00045 static const int8_t parcor_rice_table[3][20][2] = { 00046 { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4}, 00047 { 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3}, 00048 { -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2}, 00049 { 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} }, 00050 { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4}, 00051 { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4}, 00052 {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4}, 00053 { 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} }, 00054 { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4}, 00055 { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3}, 00056 {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3}, 00057 { 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} } 00058 }; 00059 00060 00066 static const int16_t parcor_scaled_values[] = { 00067 -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32, 00068 -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32, 00069 -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32, 00070 -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32, 00071 -1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32, 00072 -994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32, 00073 -971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32, 00074 -944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32, 00075 -913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32, 00076 -878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32, 00077 -838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32, 00078 -795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32, 00079 -747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32, 00080 -695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32, 00081 -639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32, 00082 -580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32, 00083 -516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32, 00084 -447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32, 00085 -375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32, 00086 -299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32, 00087 -219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32, 00088 -134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32, 00089 -46048 / 32, -23264 / 32, -224 / 32, 23072 / 32, 00090 46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32, 00091 143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32, 00092 244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32, 00093 349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32, 00094 458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32, 00095 571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32, 00096 688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32, 00097 810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32, 00098 935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32 00099 }; 00100 00101 00105 static const uint8_t ltp_gain_values [4][4] = { 00106 { 0, 8, 16, 24}, 00107 {32, 40, 48, 56}, 00108 {64, 70, 76, 82}, 00109 {88, 92, 96, 100} 00110 }; 00111 00112 00116 static const int16_t mcc_weightings[] = { 00117 204, 192, 179, 166, 153, 140, 128, 115, 00118 102, 89, 76, 64, 51, 38, 25, 12, 00119 0, -12, -25, -38, -51, -64, -76, -89, 00120 -102, -115, -128, -140, -153, -166, -179, -192 00121 }; 00122 00123 00126 static const uint8_t tail_code[16][6] = { 00127 { 74, 44, 25, 13, 7, 3}, 00128 { 68, 42, 24, 13, 7, 3}, 00129 { 58, 39, 23, 13, 7, 3}, 00130 {126, 70, 37, 19, 10, 5}, 00131 {132, 70, 37, 20, 10, 5}, 00132 {124, 70, 38, 20, 10, 5}, 00133 {120, 69, 37, 20, 11, 5}, 00134 {116, 67, 37, 20, 11, 5}, 00135 {108, 66, 36, 20, 10, 5}, 00136 {102, 62, 36, 20, 10, 5}, 00137 { 88, 58, 34, 19, 10, 5}, 00138 {162, 89, 49, 25, 13, 7}, 00139 {156, 87, 49, 26, 14, 7}, 00140 {150, 86, 47, 26, 14, 7}, 00141 {142, 84, 47, 26, 14, 7}, 00142 {131, 79, 46, 26, 14, 7} 00143 }; 00144 00145 00146 enum RA_Flag { 00147 RA_FLAG_NONE, 00148 RA_FLAG_FRAMES, 00149 RA_FLAG_HEADER 00150 }; 00151 00152 00153 typedef struct { 00154 uint32_t samples; 00155 int resolution; 00156 int floating; 00157 int frame_length; 00158 int ra_distance; 00159 enum RA_Flag ra_flag; 00160 int adapt_order; 00161 int coef_table; 00162 int long_term_prediction; 00163 int max_order; 00164 int block_switching; 00165 int bgmc; 00166 int sb_part; 00167 int joint_stereo; 00168 int mc_coding; 00169 int chan_config; 00170 int chan_sort; 00171 int rlslms; 00172 int chan_config_info; 00173 int *chan_pos; 00174 } ALSSpecificConfig; 00175 00176 00177 typedef struct { 00178 int stop_flag; 00179 int master_channel; 00180 int time_diff_flag; 00181 int time_diff_sign; 00182 int time_diff_index; 00183 int weighting[6]; 00184 } ALSChannelData; 00185 00186 00187 typedef struct { 00188 AVCodecContext *avctx; 00189 ALSSpecificConfig sconf; 00190 GetBitContext gb; 00191 unsigned int cur_frame_length; 00192 unsigned int frame_id; 00193 unsigned int js_switch; 00194 unsigned int num_blocks; 00195 unsigned int s_max; 00196 uint8_t *bgmc_lut; 00197 unsigned int *bgmc_lut_status; 00198 int ltp_lag_length; 00199 int *use_ltp; 00200 int *ltp_lag; 00201 int **ltp_gain; 00202 int *ltp_gain_buffer; 00203 int32_t **quant_cof; 00204 int32_t *quant_cof_buffer; 00205 int32_t **lpc_cof; 00206 int32_t *lpc_cof_buffer; 00207 int32_t *lpc_cof_reversed_buffer; 00208 ALSChannelData **chan_data; 00209 ALSChannelData *chan_data_buffer; 00210 int *reverted_channels; 00211 int32_t *prev_raw_samples; 00212 int32_t **raw_samples; 00213 int32_t *raw_buffer; 00214 } ALSDecContext; 00215 00216 00217 typedef struct { 00218 unsigned int block_length; 00219 unsigned int ra_block; 00220 int const_block; 00221 int32_t const_val; 00222 int js_blocks; 00223 unsigned int shift_lsbs; 00224 unsigned int opt_order; 00225 int store_prev_samples; 00226 int *use_ltp; 00227 int *ltp_lag; 00228 int *ltp_gain; 00229 int32_t *quant_cof; 00230 int32_t *lpc_cof; 00231 int32_t *raw_samples; 00232 int32_t *prev_raw_samples; 00233 int32_t *raw_other; 00234 } ALSBlockData; 00235 00236 00237 static av_cold void dprint_specific_config(ALSDecContext *ctx) 00238 { 00239 #ifdef DEBUG 00240 AVCodecContext *avctx = ctx->avctx; 00241 ALSSpecificConfig *sconf = &ctx->sconf; 00242 00243 dprintf(avctx, "resolution = %i\n", sconf->resolution); 00244 dprintf(avctx, "floating = %i\n", sconf->floating); 00245 dprintf(avctx, "frame_length = %i\n", sconf->frame_length); 00246 dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance); 00247 dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag); 00248 dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order); 00249 dprintf(avctx, "coef_table = %i\n", sconf->coef_table); 00250 dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction); 00251 dprintf(avctx, "max_order = %i\n", sconf->max_order); 00252 dprintf(avctx, "block_switching = %i\n", sconf->block_switching); 00253 dprintf(avctx, "bgmc = %i\n", sconf->bgmc); 00254 dprintf(avctx, "sb_part = %i\n", sconf->sb_part); 00255 dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo); 00256 dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding); 00257 dprintf(avctx, "chan_config = %i\n", sconf->chan_config); 00258 dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort); 00259 dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms); 00260 dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info); 00261 #endif 00262 } 00263 00264 00267 static av_cold int read_specific_config(ALSDecContext *ctx) 00268 { 00269 GetBitContext gb; 00270 uint64_t ht_size; 00271 int i, config_offset, crc_enabled; 00272 MPEG4AudioConfig m4ac; 00273 ALSSpecificConfig *sconf = &ctx->sconf; 00274 AVCodecContext *avctx = ctx->avctx; 00275 uint32_t als_id, header_size, trailer_size; 00276 00277 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8); 00278 00279 config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata, 00280 avctx->extradata_size); 00281 00282 if (config_offset < 0) 00283 return -1; 00284 00285 skip_bits_long(&gb, config_offset); 00286 00287 if (get_bits_left(&gb) < (30 << 3)) 00288 return -1; 00289 00290 // read the fixed items 00291 als_id = get_bits_long(&gb, 32); 00292 avctx->sample_rate = m4ac.sample_rate; 00293 skip_bits_long(&gb, 32); // sample rate already known 00294 sconf->samples = get_bits_long(&gb, 32); 00295 avctx->channels = m4ac.channels; 00296 skip_bits(&gb, 16); // number of channels already knwon 00297 skip_bits(&gb, 3); // skip file_type 00298 sconf->resolution = get_bits(&gb, 3); 00299 sconf->floating = get_bits1(&gb); 00300 skip_bits1(&gb); // skip msb_first 00301 sconf->frame_length = get_bits(&gb, 16) + 1; 00302 sconf->ra_distance = get_bits(&gb, 8); 00303 sconf->ra_flag = get_bits(&gb, 2); 00304 sconf->adapt_order = get_bits1(&gb); 00305 sconf->coef_table = get_bits(&gb, 2); 00306 sconf->long_term_prediction = get_bits1(&gb); 00307 sconf->max_order = get_bits(&gb, 10); 00308 sconf->block_switching = get_bits(&gb, 2); 00309 sconf->bgmc = get_bits1(&gb); 00310 sconf->sb_part = get_bits1(&gb); 00311 sconf->joint_stereo = get_bits1(&gb); 00312 sconf->mc_coding = get_bits1(&gb); 00313 sconf->chan_config = get_bits1(&gb); 00314 sconf->chan_sort = get_bits1(&gb); 00315 crc_enabled = get_bits1(&gb); 00316 sconf->rlslms = get_bits1(&gb); 00317 skip_bits(&gb, 5); // skip 5 reserved bits 00318 skip_bits1(&gb); // skip aux_data_enabled 00319 00320 00321 // check for ALSSpecificConfig struct 00322 if (als_id != MKBETAG('A','L','S','\0')) 00323 return -1; 00324 00325 ctx->cur_frame_length = sconf->frame_length; 00326 00327 // read channel config 00328 if (sconf->chan_config) 00329 sconf->chan_config_info = get_bits(&gb, 16); 00330 // TODO: use this to set avctx->channel_layout 00331 00332 00333 // read channel sorting 00334 if (sconf->chan_sort && avctx->channels > 1) { 00335 int chan_pos_bits = av_ceil_log2(avctx->channels); 00336 int bits_needed = avctx->channels * chan_pos_bits + 7; 00337 if (get_bits_left(&gb) < bits_needed) 00338 return -1; 00339 00340 if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos)))) 00341 return AVERROR(ENOMEM); 00342 00343 for (i = 0; i < avctx->channels; i++) 00344 sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits); 00345 00346 align_get_bits(&gb); 00347 // TODO: use this to actually do channel sorting 00348 } else { 00349 sconf->chan_sort = 0; 00350 } 00351 00352 00353 // read fixed header and trailer sizes, 00354 // if size = 0xFFFFFFFF then there is no data field! 00355 if (get_bits_left(&gb) < 64) 00356 return -1; 00357 00358 header_size = get_bits_long(&gb, 32); 00359 trailer_size = get_bits_long(&gb, 32); 00360 if (header_size == 0xFFFFFFFF) 00361 header_size = 0; 00362 if (trailer_size == 0xFFFFFFFF) 00363 trailer_size = 0; 00364 00365 ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3; 00366 00367 00368 // skip the header and trailer data 00369 if (get_bits_left(&gb) < ht_size) 00370 return -1; 00371 00372 if (ht_size > INT32_MAX) 00373 return -1; 00374 00375 skip_bits_long(&gb, ht_size); 00376 00377 00378 // skip the crc data 00379 if (crc_enabled) { 00380 if (get_bits_left(&gb) < 32) 00381 return -1; 00382 00383 skip_bits_long(&gb, 32); 00384 } 00385 00386 00387 // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data) 00388 00389 dprint_specific_config(ctx); 00390 00391 return 0; 00392 } 00393 00394 00397 static int check_specific_config(ALSDecContext *ctx) 00398 { 00399 ALSSpecificConfig *sconf = &ctx->sconf; 00400 int error = 0; 00401 00402 // report unsupported feature and set error value 00403 #define MISSING_ERR(cond, str, errval) \ 00404 { \ 00405 if (cond) { \ 00406 av_log_missing_feature(ctx->avctx, str, 0); \ 00407 error = errval; \ 00408 } \ 00409 } 00410 00411 MISSING_ERR(sconf->floating, "Floating point decoding", -1); 00412 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1); 00413 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0); 00414 00415 return error; 00416 } 00417 00418 00422 static void parse_bs_info(const uint32_t bs_info, unsigned int n, 00423 unsigned int div, unsigned int **div_blocks, 00424 unsigned int *num_blocks) 00425 { 00426 if (n < 31 && ((bs_info << n) & 0x40000000)) { 00427 // if the level is valid and the investigated bit n is set 00428 // then recursively check both children at bits (2n+1) and (2n+2) 00429 n *= 2; 00430 div += 1; 00431 parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks); 00432 parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks); 00433 } else { 00434 // else the bit is not set or the last level has been reached 00435 // (bit implicitly not set) 00436 **div_blocks = div; 00437 (*div_blocks)++; 00438 (*num_blocks)++; 00439 } 00440 } 00441 00442 00445 static int32_t decode_rice(GetBitContext *gb, unsigned int k) 00446 { 00447 int max = get_bits_left(gb) - k; 00448 int q = get_unary(gb, 0, max); 00449 int r = k ? get_bits1(gb) : !(q & 1); 00450 00451 if (k > 1) { 00452 q <<= (k - 1); 00453 q += get_bits_long(gb, k - 1); 00454 } else if (!k) { 00455 q >>= 1; 00456 } 00457 return r ? q : ~q; 00458 } 00459 00460 00463 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof) 00464 { 00465 int i, j; 00466 00467 for (i = 0, j = k - 1; i < j; i++, j--) { 00468 int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); 00469 cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20); 00470 cof[i] += tmp1; 00471 } 00472 if (i == j) 00473 cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20); 00474 00475 cof[k] = par[k]; 00476 } 00477 00478 00483 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks, 00484 uint32_t *bs_info) 00485 { 00486 ALSSpecificConfig *sconf = &ctx->sconf; 00487 GetBitContext *gb = &ctx->gb; 00488 unsigned int *ptr_div_blocks = div_blocks; 00489 unsigned int b; 00490 00491 if (sconf->block_switching) { 00492 unsigned int bs_info_len = 1 << (sconf->block_switching + 2); 00493 *bs_info = get_bits_long(gb, bs_info_len); 00494 *bs_info <<= (32 - bs_info_len); 00495 } 00496 00497 ctx->num_blocks = 0; 00498 parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks); 00499 00500 // The last frame may have an overdetermined block structure given in 00501 // the bitstream. In that case the defined block structure would need 00502 // more samples than available to be consistent. 00503 // The block structure is actually used but the block sizes are adapted 00504 // to fit the actual number of available samples. 00505 // Example: 5 samples, 2nd level block sizes: 2 2 2 2. 00506 // This results in the actual block sizes: 2 2 1 0. 00507 // This is not specified in 14496-3 but actually done by the reference 00508 // codec RM22 revision 2. 00509 // This appears to happen in case of an odd number of samples in the last 00510 // frame which is actually not allowed by the block length switching part 00511 // of 14496-3. 00512 // The ALS conformance files feature an odd number of samples in the last 00513 // frame. 00514 00515 for (b = 0; b < ctx->num_blocks; b++) 00516 div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b]; 00517 00518 if (ctx->cur_frame_length != ctx->sconf.frame_length) { 00519 unsigned int remaining = ctx->cur_frame_length; 00520 00521 for (b = 0; b < ctx->num_blocks; b++) { 00522 if (remaining <= div_blocks[b]) { 00523 div_blocks[b] = remaining; 00524 ctx->num_blocks = b + 1; 00525 break; 00526 } 00527 00528 remaining -= div_blocks[b]; 00529 } 00530 } 00531 } 00532 00533 00536 static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd) 00537 { 00538 ALSSpecificConfig *sconf = &ctx->sconf; 00539 AVCodecContext *avctx = ctx->avctx; 00540 GetBitContext *gb = &ctx->gb; 00541 00542 bd->const_val = 0; 00543 bd->const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence) 00544 bd->js_blocks = get_bits1(gb); 00545 00546 // skip 5 reserved bits 00547 skip_bits(gb, 5); 00548 00549 if (bd->const_block) { 00550 unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample; 00551 bd->const_val = get_sbits_long(gb, const_val_bits); 00552 } 00553 00554 // ensure constant block decoding by reusing this field 00555 bd->const_block = 1; 00556 } 00557 00558 00561 static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd) 00562 { 00563 int smp = bd->block_length; 00564 int32_t val = bd->const_val; 00565 int32_t *dst = bd->raw_samples; 00566 00567 // write raw samples into buffer 00568 for (; smp; smp--) 00569 *dst++ = val; 00570 } 00571 00572 00575 static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd) 00576 { 00577 ALSSpecificConfig *sconf = &ctx->sconf; 00578 AVCodecContext *avctx = ctx->avctx; 00579 GetBitContext *gb = &ctx->gb; 00580 unsigned int k; 00581 unsigned int s[8]; 00582 unsigned int sx[8]; 00583 unsigned int sub_blocks, log2_sub_blocks, sb_length; 00584 unsigned int start = 0; 00585 unsigned int opt_order; 00586 int sb; 00587 int32_t *quant_cof = bd->quant_cof; 00588 int32_t *current_res; 00589 00590 00591 // ensure variable block decoding by reusing this field 00592 bd->const_block = 0; 00593 00594 bd->opt_order = 1; 00595 bd->js_blocks = get_bits1(gb); 00596 00597 opt_order = bd->opt_order; 00598 00599 // determine the number of subblocks for entropy decoding 00600 if (!sconf->bgmc && !sconf->sb_part) { 00601 log2_sub_blocks = 0; 00602 } else { 00603 if (sconf->bgmc && sconf->sb_part) 00604 log2_sub_blocks = get_bits(gb, 2); 00605 else 00606 log2_sub_blocks = 2 * get_bits1(gb); 00607 } 00608 00609 sub_blocks = 1 << log2_sub_blocks; 00610 00611 // do not continue in case of a damaged stream since 00612 // block_length must be evenly divisible by sub_blocks 00613 if (bd->block_length & (sub_blocks - 1)) { 00614 av_log(avctx, AV_LOG_WARNING, 00615 "Block length is not evenly divisible by the number of subblocks.\n"); 00616 return -1; 00617 } 00618 00619 sb_length = bd->block_length >> log2_sub_blocks; 00620 00621 if (sconf->bgmc) { 00622 s[0] = get_bits(gb, 8 + (sconf->resolution > 1)); 00623 for (k = 1; k < sub_blocks; k++) 00624 s[k] = s[k - 1] + decode_rice(gb, 2); 00625 00626 for (k = 0; k < sub_blocks; k++) { 00627 sx[k] = s[k] & 0x0F; 00628 s [k] >>= 4; 00629 } 00630 } else { 00631 s[0] = get_bits(gb, 4 + (sconf->resolution > 1)); 00632 for (k = 1; k < sub_blocks; k++) 00633 s[k] = s[k - 1] + decode_rice(gb, 0); 00634 } 00635 00636 if (get_bits1(gb)) 00637 bd->shift_lsbs = get_bits(gb, 4) + 1; 00638 00639 bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || bd->shift_lsbs; 00640 00641 00642 if (!sconf->rlslms) { 00643 if (sconf->adapt_order) { 00644 int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1, 00645 2, sconf->max_order + 1)); 00646 bd->opt_order = get_bits(gb, opt_order_length); 00647 } else { 00648 bd->opt_order = sconf->max_order; 00649 } 00650 00651 opt_order = bd->opt_order; 00652 00653 if (opt_order) { 00654 int add_base; 00655 00656 if (sconf->coef_table == 3) { 00657 add_base = 0x7F; 00658 00659 // read coefficient 0 00660 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)]; 00661 00662 // read coefficient 1 00663 if (opt_order > 1) 00664 quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)]; 00665 00666 // read coefficients 2 to opt_order 00667 for (k = 2; k < opt_order; k++) 00668 quant_cof[k] = get_bits(gb, 7); 00669 } else { 00670 int k_max; 00671 add_base = 1; 00672 00673 // read coefficient 0 to 19 00674 k_max = FFMIN(opt_order, 20); 00675 for (k = 0; k < k_max; k++) { 00676 int rice_param = parcor_rice_table[sconf->coef_table][k][1]; 00677 int offset = parcor_rice_table[sconf->coef_table][k][0]; 00678 quant_cof[k] = decode_rice(gb, rice_param) + offset; 00679 } 00680 00681 // read coefficients 20 to 126 00682 k_max = FFMIN(opt_order, 127); 00683 for (; k < k_max; k++) 00684 quant_cof[k] = decode_rice(gb, 2) + (k & 1); 00685 00686 // read coefficients 127 to opt_order 00687 for (; k < opt_order; k++) 00688 quant_cof[k] = decode_rice(gb, 1); 00689 00690 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64]; 00691 00692 if (opt_order > 1) 00693 quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64]; 00694 } 00695 00696 for (k = 2; k < opt_order; k++) 00697 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13); 00698 } 00699 } 00700 00701 // read LTP gain and lag values 00702 if (sconf->long_term_prediction) { 00703 *bd->use_ltp = get_bits1(gb); 00704 00705 if (*bd->use_ltp) { 00706 int r, c; 00707 00708 bd->ltp_gain[0] = decode_rice(gb, 1) << 3; 00709 bd->ltp_gain[1] = decode_rice(gb, 2) << 3; 00710 00711 r = get_unary(gb, 0, 4); 00712 c = get_bits(gb, 2); 00713 bd->ltp_gain[2] = ltp_gain_values[r][c]; 00714 00715 bd->ltp_gain[3] = decode_rice(gb, 2) << 3; 00716 bd->ltp_gain[4] = decode_rice(gb, 1) << 3; 00717 00718 *bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length); 00719 *bd->ltp_lag += FFMAX(4, opt_order + 1); 00720 } 00721 } 00722 00723 // read first value and residuals in case of a random access block 00724 if (bd->ra_block) { 00725 if (opt_order) 00726 bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4); 00727 if (opt_order > 1) 00728 bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max)); 00729 if (opt_order > 2) 00730 bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max)); 00731 00732 start = FFMIN(opt_order, 3); 00733 } 00734 00735 // read all residuals 00736 if (sconf->bgmc) { 00737 unsigned int delta[sub_blocks]; 00738 unsigned int k [sub_blocks]; 00739 unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5); 00740 unsigned int i = start; 00741 00742 // read most significant bits 00743 unsigned int high; 00744 unsigned int low; 00745 unsigned int value; 00746 00747 ff_bgmc_decode_init(gb, &high, &low, &value); 00748 00749 current_res = bd->raw_samples + start; 00750 00751 for (sb = 0; sb < sub_blocks; sb++, i = 0) { 00752 k [sb] = s[sb] > b ? s[sb] - b : 0; 00753 delta[sb] = 5 - s[sb] + k[sb]; 00754 00755 ff_bgmc_decode(gb, sb_length, current_res, 00756 delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status); 00757 00758 current_res += sb_length; 00759 } 00760 00761 ff_bgmc_decode_end(gb); 00762 00763 00764 // read least significant bits and tails 00765 i = start; 00766 current_res = bd->raw_samples + start; 00767 00768 for (sb = 0; sb < sub_blocks; sb++, i = 0) { 00769 unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]]; 00770 unsigned int cur_k = k[sb]; 00771 unsigned int cur_s = s[sb]; 00772 00773 for (; i < sb_length; i++) { 00774 int32_t res = *current_res; 00775 00776 if (res == cur_tail_code) { 00777 unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10)) 00778 << (5 - delta[sb]); 00779 00780 res = decode_rice(gb, cur_s); 00781 00782 if (res >= 0) { 00783 res += (max_msb ) << cur_k; 00784 } else { 00785 res -= (max_msb - 1) << cur_k; 00786 } 00787 } else { 00788 if (res > cur_tail_code) 00789 res--; 00790 00791 if (res & 1) 00792 res = -res; 00793 00794 res >>= 1; 00795 00796 if (cur_k) { 00797 res <<= cur_k; 00798 res |= get_bits_long(gb, cur_k); 00799 } 00800 } 00801 00802 *current_res++ = res; 00803 } 00804 } 00805 } else { 00806 current_res = bd->raw_samples + start; 00807 00808 for (sb = 0; sb < sub_blocks; sb++, start = 0) 00809 for (; start < sb_length; start++) 00810 *current_res++ = decode_rice(gb, s[sb]); 00811 } 00812 00813 if (!sconf->mc_coding || ctx->js_switch) 00814 align_get_bits(gb); 00815 00816 return 0; 00817 } 00818 00819 00822 static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd) 00823 { 00824 ALSSpecificConfig *sconf = &ctx->sconf; 00825 unsigned int block_length = bd->block_length; 00826 unsigned int smp = 0; 00827 unsigned int k; 00828 int opt_order = bd->opt_order; 00829 int sb; 00830 int64_t y; 00831 int32_t *quant_cof = bd->quant_cof; 00832 int32_t *lpc_cof = bd->lpc_cof; 00833 int32_t *raw_samples = bd->raw_samples; 00834 int32_t *raw_samples_end = bd->raw_samples + bd->block_length; 00835 int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer; 00836 00837 // reverse long-term prediction 00838 if (*bd->use_ltp) { 00839 int ltp_smp; 00840 00841 for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) { 00842 int center = ltp_smp - *bd->ltp_lag; 00843 int begin = FFMAX(0, center - 2); 00844 int end = center + 3; 00845 int tab = 5 - (end - begin); 00846 int base; 00847 00848 y = 1 << 6; 00849 00850 for (base = begin; base < end; base++, tab++) 00851 y += MUL64(bd->ltp_gain[tab], raw_samples[base]); 00852 00853 raw_samples[ltp_smp] += y >> 7; 00854 } 00855 } 00856 00857 // reconstruct all samples from residuals 00858 if (bd->ra_block) { 00859 for (smp = 0; smp < opt_order; smp++) { 00860 y = 1 << 19; 00861 00862 for (sb = 0; sb < smp; sb++) 00863 y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]); 00864 00865 *raw_samples++ -= y >> 20; 00866 parcor_to_lpc(smp, quant_cof, lpc_cof); 00867 } 00868 } else { 00869 for (k = 0; k < opt_order; k++) 00870 parcor_to_lpc(k, quant_cof, lpc_cof); 00871 00872 // store previous samples in case that they have to be altered 00873 if (bd->store_prev_samples) 00874 memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order, 00875 sizeof(*bd->prev_raw_samples) * sconf->max_order); 00876 00877 // reconstruct difference signal for prediction (joint-stereo) 00878 if (bd->js_blocks && bd->raw_other) { 00879 int32_t *left, *right; 00880 00881 if (bd->raw_other > raw_samples) { // D = R - L 00882 left = raw_samples; 00883 right = bd->raw_other; 00884 } else { // D = R - L 00885 left = bd->raw_other; 00886 right = raw_samples; 00887 } 00888 00889 for (sb = -1; sb >= -sconf->max_order; sb--) 00890 raw_samples[sb] = right[sb] - left[sb]; 00891 } 00892 00893 // reconstruct shifted signal 00894 if (bd->shift_lsbs) 00895 for (sb = -1; sb >= -sconf->max_order; sb--) 00896 raw_samples[sb] >>= bd->shift_lsbs; 00897 } 00898 00899 // reverse linear prediction coefficients for efficiency 00900 lpc_cof = lpc_cof + opt_order; 00901 00902 for (sb = 0; sb < opt_order; sb++) 00903 lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)]; 00904 00905 // reconstruct raw samples 00906 raw_samples = bd->raw_samples + smp; 00907 lpc_cof = lpc_cof_reversed + opt_order; 00908 00909 for (; raw_samples < raw_samples_end; raw_samples++) { 00910 y = 1 << 19; 00911 00912 for (sb = -opt_order; sb < 0; sb++) 00913 y += MUL64(lpc_cof[sb], raw_samples[sb]); 00914 00915 *raw_samples -= y >> 20; 00916 } 00917 00918 raw_samples = bd->raw_samples; 00919 00920 // restore previous samples in case that they have been altered 00921 if (bd->store_prev_samples) 00922 memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples, 00923 sizeof(*raw_samples) * sconf->max_order); 00924 00925 return 0; 00926 } 00927 00928 00931 static int read_block(ALSDecContext *ctx, ALSBlockData *bd) 00932 { 00933 GetBitContext *gb = &ctx->gb; 00934 00935 // read block type flag and read the samples accordingly 00936 if (get_bits1(gb)) { 00937 if (read_var_block_data(ctx, bd)) 00938 return -1; 00939 } else { 00940 read_const_block_data(ctx, bd); 00941 } 00942 00943 return 0; 00944 } 00945 00946 00949 static int decode_block(ALSDecContext *ctx, ALSBlockData *bd) 00950 { 00951 unsigned int smp; 00952 00953 // read block type flag and read the samples accordingly 00954 if (bd->const_block) 00955 decode_const_block_data(ctx, bd); 00956 else if (decode_var_block_data(ctx, bd)) 00957 return -1; 00958 00959 // TODO: read RLSLMS extension data 00960 00961 if (bd->shift_lsbs) 00962 for (smp = 0; smp < bd->block_length; smp++) 00963 bd->raw_samples[smp] <<= bd->shift_lsbs; 00964 00965 return 0; 00966 } 00967 00968 00971 static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd) 00972 { 00973 int ret; 00974 00975 ret = read_block(ctx, bd); 00976 00977 if (ret) 00978 return ret; 00979 00980 ret = decode_block(ctx, bd); 00981 00982 return ret; 00983 } 00984 00985 00989 static void zero_remaining(unsigned int b, unsigned int b_max, 00990 const unsigned int *div_blocks, int32_t *buf) 00991 { 00992 unsigned int count = 0; 00993 00994 while (b < b_max) 00995 count += div_blocks[b]; 00996 00997 if (count) 00998 memset(buf, 0, sizeof(*buf) * count); 00999 } 01000 01001 01004 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame, 01005 unsigned int c, const unsigned int *div_blocks, 01006 unsigned int *js_blocks) 01007 { 01008 unsigned int b; 01009 ALSBlockData bd; 01010 01011 memset(&bd, 0, sizeof(ALSBlockData)); 01012 01013 bd.ra_block = ra_frame; 01014 bd.use_ltp = ctx->use_ltp; 01015 bd.ltp_lag = ctx->ltp_lag; 01016 bd.ltp_gain = ctx->ltp_gain[0]; 01017 bd.quant_cof = ctx->quant_cof[0]; 01018 bd.lpc_cof = ctx->lpc_cof[0]; 01019 bd.prev_raw_samples = ctx->prev_raw_samples; 01020 bd.raw_samples = ctx->raw_samples[c]; 01021 01022 01023 for (b = 0; b < ctx->num_blocks; b++) { 01024 bd.shift_lsbs = 0; 01025 bd.block_length = div_blocks[b]; 01026 01027 if (read_decode_block(ctx, &bd)) { 01028 // damaged block, write zero for the rest of the frame 01029 zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples); 01030 return -1; 01031 } 01032 bd.raw_samples += div_blocks[b]; 01033 bd.ra_block = 0; 01034 } 01035 01036 return 0; 01037 } 01038 01039 01042 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame, 01043 unsigned int c, const unsigned int *div_blocks, 01044 unsigned int *js_blocks) 01045 { 01046 ALSSpecificConfig *sconf = &ctx->sconf; 01047 unsigned int offset = 0; 01048 unsigned int b; 01049 ALSBlockData bd[2]; 01050 01051 memset(bd, 0, 2 * sizeof(ALSBlockData)); 01052 01053 bd[0].ra_block = ra_frame; 01054 bd[0].use_ltp = ctx->use_ltp; 01055 bd[0].ltp_lag = ctx->ltp_lag; 01056 bd[0].ltp_gain = ctx->ltp_gain[0]; 01057 bd[0].quant_cof = ctx->quant_cof[0]; 01058 bd[0].lpc_cof = ctx->lpc_cof[0]; 01059 bd[0].prev_raw_samples = ctx->prev_raw_samples; 01060 bd[0].js_blocks = *js_blocks; 01061 01062 bd[1].ra_block = ra_frame; 01063 bd[1].use_ltp = ctx->use_ltp; 01064 bd[1].ltp_lag = ctx->ltp_lag; 01065 bd[1].ltp_gain = ctx->ltp_gain[0]; 01066 bd[1].quant_cof = ctx->quant_cof[0]; 01067 bd[1].lpc_cof = ctx->lpc_cof[0]; 01068 bd[1].prev_raw_samples = ctx->prev_raw_samples; 01069 bd[1].js_blocks = *(js_blocks + 1); 01070 01071 // decode all blocks 01072 for (b = 0; b < ctx->num_blocks; b++) { 01073 unsigned int s; 01074 01075 bd[0].shift_lsbs = 0; 01076 bd[1].shift_lsbs = 0; 01077 01078 bd[0].block_length = div_blocks[b]; 01079 bd[1].block_length = div_blocks[b]; 01080 01081 bd[0].raw_samples = ctx->raw_samples[c ] + offset; 01082 bd[1].raw_samples = ctx->raw_samples[c + 1] + offset; 01083 01084 bd[0].raw_other = bd[1].raw_samples; 01085 bd[1].raw_other = bd[0].raw_samples; 01086 01087 if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) { 01088 // damaged block, write zero for the rest of the frame 01089 zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples); 01090 zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples); 01091 return -1; 01092 } 01093 01094 // reconstruct joint-stereo blocks 01095 if (bd[0].js_blocks) { 01096 if (bd[1].js_blocks) 01097 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n"); 01098 01099 for (s = 0; s < div_blocks[b]; s++) 01100 bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s]; 01101 } else if (bd[1].js_blocks) { 01102 for (s = 0; s < div_blocks[b]; s++) 01103 bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s]; 01104 } 01105 01106 offset += div_blocks[b]; 01107 bd[0].ra_block = 0; 01108 bd[1].ra_block = 0; 01109 } 01110 01111 // store carryover raw samples, 01112 // the others channel raw samples are stored by the calling function. 01113 memmove(ctx->raw_samples[c] - sconf->max_order, 01114 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, 01115 sizeof(*ctx->raw_samples[c]) * sconf->max_order); 01116 01117 return 0; 01118 } 01119 01120 01123 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c) 01124 { 01125 GetBitContext *gb = &ctx->gb; 01126 ALSChannelData *current = cd; 01127 unsigned int channels = ctx->avctx->channels; 01128 int entries = 0; 01129 01130 while (entries < channels && !(current->stop_flag = get_bits1(gb))) { 01131 current->master_channel = get_bits_long(gb, av_ceil_log2(channels)); 01132 01133 if (current->master_channel >= channels) { 01134 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n"); 01135 return -1; 01136 } 01137 01138 if (current->master_channel != c) { 01139 current->time_diff_flag = get_bits1(gb); 01140 current->weighting[0] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)]; 01141 current->weighting[1] = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)]; 01142 current->weighting[2] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)]; 01143 01144 if (current->time_diff_flag) { 01145 current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)]; 01146 current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)]; 01147 current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)]; 01148 01149 current->time_diff_sign = get_bits1(gb); 01150 current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3; 01151 } 01152 } 01153 01154 current++; 01155 entries++; 01156 } 01157 01158 if (entries == channels) { 01159 av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n"); 01160 return -1; 01161 } 01162 01163 align_get_bits(gb); 01164 return 0; 01165 } 01166 01167 01170 static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd, 01171 ALSChannelData **cd, int *reverted, 01172 unsigned int offset, int c) 01173 { 01174 ALSChannelData *ch = cd[c]; 01175 unsigned int dep = 0; 01176 unsigned int channels = ctx->avctx->channels; 01177 01178 if (reverted[c]) 01179 return 0; 01180 01181 reverted[c] = 1; 01182 01183 while (dep < channels && !ch[dep].stop_flag) { 01184 revert_channel_correlation(ctx, bd, cd, reverted, offset, 01185 ch[dep].master_channel); 01186 01187 dep++; 01188 } 01189 01190 if (dep == channels) { 01191 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n"); 01192 return -1; 01193 } 01194 01195 bd->use_ltp = ctx->use_ltp + c; 01196 bd->ltp_lag = ctx->ltp_lag + c; 01197 bd->ltp_gain = ctx->ltp_gain[c]; 01198 bd->lpc_cof = ctx->lpc_cof[c]; 01199 bd->quant_cof = ctx->quant_cof[c]; 01200 bd->raw_samples = ctx->raw_samples[c] + offset; 01201 01202 dep = 0; 01203 while (!ch[dep].stop_flag) { 01204 unsigned int smp; 01205 unsigned int begin = 1; 01206 unsigned int end = bd->block_length - 1; 01207 int64_t y; 01208 int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset; 01209 01210 if (ch[dep].time_diff_flag) { 01211 int t = ch[dep].time_diff_index; 01212 01213 if (ch[dep].time_diff_sign) { 01214 t = -t; 01215 begin -= t; 01216 } else { 01217 end -= t; 01218 } 01219 01220 for (smp = begin; smp < end; smp++) { 01221 y = (1 << 6) + 01222 MUL64(ch[dep].weighting[0], master[smp - 1 ]) + 01223 MUL64(ch[dep].weighting[1], master[smp ]) + 01224 MUL64(ch[dep].weighting[2], master[smp + 1 ]) + 01225 MUL64(ch[dep].weighting[3], master[smp - 1 + t]) + 01226 MUL64(ch[dep].weighting[4], master[smp + t]) + 01227 MUL64(ch[dep].weighting[5], master[smp + 1 + t]); 01228 01229 bd->raw_samples[smp] += y >> 7; 01230 } 01231 } else { 01232 for (smp = begin; smp < end; smp++) { 01233 y = (1 << 6) + 01234 MUL64(ch[dep].weighting[0], master[smp - 1]) + 01235 MUL64(ch[dep].weighting[1], master[smp ]) + 01236 MUL64(ch[dep].weighting[2], master[smp + 1]); 01237 01238 bd->raw_samples[smp] += y >> 7; 01239 } 01240 } 01241 01242 dep++; 01243 } 01244 01245 return 0; 01246 } 01247 01248 01251 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame) 01252 { 01253 ALSSpecificConfig *sconf = &ctx->sconf; 01254 AVCodecContext *avctx = ctx->avctx; 01255 GetBitContext *gb = &ctx->gb; 01256 unsigned int div_blocks[32]; 01257 unsigned int c; 01258 unsigned int js_blocks[2]; 01259 01260 uint32_t bs_info = 0; 01261 01262 // skip the size of the ra unit if present in the frame 01263 if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame) 01264 skip_bits_long(gb, 32); 01265 01266 if (sconf->mc_coding && sconf->joint_stereo) { 01267 ctx->js_switch = get_bits1(gb); 01268 align_get_bits(gb); 01269 } 01270 01271 if (!sconf->mc_coding || ctx->js_switch) { 01272 int independent_bs = !sconf->joint_stereo; 01273 01274 for (c = 0; c < avctx->channels; c++) { 01275 js_blocks[0] = 0; 01276 js_blocks[1] = 0; 01277 01278 get_block_sizes(ctx, div_blocks, &bs_info); 01279 01280 // if joint_stereo and block_switching is set, independent decoding 01281 // is signaled via the first bit of bs_info 01282 if (sconf->joint_stereo && sconf->block_switching) 01283 if (bs_info >> 31) 01284 independent_bs = 2; 01285 01286 // if this is the last channel, it has to be decoded independently 01287 if (c == avctx->channels - 1) 01288 independent_bs = 1; 01289 01290 if (independent_bs) { 01291 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks)) 01292 return -1; 01293 01294 independent_bs--; 01295 } else { 01296 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks)) 01297 return -1; 01298 01299 c++; 01300 } 01301 01302 // store carryover raw samples 01303 memmove(ctx->raw_samples[c] - sconf->max_order, 01304 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, 01305 sizeof(*ctx->raw_samples[c]) * sconf->max_order); 01306 } 01307 } else { // multi-channel coding 01308 ALSBlockData bd; 01309 int b; 01310 int *reverted_channels = ctx->reverted_channels; 01311 unsigned int offset = 0; 01312 01313 for (c = 0; c < avctx->channels; c++) 01314 if (ctx->chan_data[c] < ctx->chan_data_buffer) { 01315 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n"); 01316 return -1; 01317 } 01318 01319 memset(&bd, 0, sizeof(ALSBlockData)); 01320 memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels); 01321 01322 bd.ra_block = ra_frame; 01323 bd.prev_raw_samples = ctx->prev_raw_samples; 01324 01325 get_block_sizes(ctx, div_blocks, &bs_info); 01326 01327 for (b = 0; b < ctx->num_blocks; b++) { 01328 bd.shift_lsbs = 0; 01329 bd.block_length = div_blocks[b]; 01330 01331 for (c = 0; c < avctx->channels; c++) { 01332 bd.use_ltp = ctx->use_ltp + c; 01333 bd.ltp_lag = ctx->ltp_lag + c; 01334 bd.ltp_gain = ctx->ltp_gain[c]; 01335 bd.lpc_cof = ctx->lpc_cof[c]; 01336 bd.quant_cof = ctx->quant_cof[c]; 01337 bd.raw_samples = ctx->raw_samples[c] + offset; 01338 bd.raw_other = NULL; 01339 01340 read_block(ctx, &bd); 01341 if (read_channel_data(ctx, ctx->chan_data[c], c)) 01342 return -1; 01343 } 01344 01345 for (c = 0; c < avctx->channels; c++) 01346 if (revert_channel_correlation(ctx, &bd, ctx->chan_data, 01347 reverted_channels, offset, c)) 01348 return -1; 01349 01350 for (c = 0; c < avctx->channels; c++) { 01351 bd.use_ltp = ctx->use_ltp + c; 01352 bd.ltp_lag = ctx->ltp_lag + c; 01353 bd.ltp_gain = ctx->ltp_gain[c]; 01354 bd.lpc_cof = ctx->lpc_cof[c]; 01355 bd.quant_cof = ctx->quant_cof[c]; 01356 bd.raw_samples = ctx->raw_samples[c] + offset; 01357 decode_block(ctx, &bd); 01358 } 01359 01360 memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels)); 01361 offset += div_blocks[b]; 01362 bd.ra_block = 0; 01363 } 01364 01365 // store carryover raw samples 01366 for (c = 0; c < avctx->channels; c++) 01367 memmove(ctx->raw_samples[c] - sconf->max_order, 01368 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length, 01369 sizeof(*ctx->raw_samples[c]) * sconf->max_order); 01370 } 01371 01372 // TODO: read_diff_float_data 01373 01374 return 0; 01375 } 01376 01377 01380 static int decode_frame(AVCodecContext *avctx, 01381 void *data, int *data_size, 01382 AVPacket *avpkt) 01383 { 01384 ALSDecContext *ctx = avctx->priv_data; 01385 ALSSpecificConfig *sconf = &ctx->sconf; 01386 const uint8_t *buffer = avpkt->data; 01387 int buffer_size = avpkt->size; 01388 int invalid_frame, size; 01389 unsigned int c, sample, ra_frame, bytes_read, shift; 01390 01391 init_get_bits(&ctx->gb, buffer, buffer_size * 8); 01392 01393 // In the case that the distance between random access frames is set to zero 01394 // (sconf->ra_distance == 0) no frame is treated as a random access frame. 01395 // For the first frame, if prediction is used, all samples used from the 01396 // previous frame are assumed to be zero. 01397 ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance); 01398 01399 // the last frame to decode might have a different length 01400 if (sconf->samples != 0xFFFFFFFF) 01401 ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length, 01402 sconf->frame_length); 01403 else 01404 ctx->cur_frame_length = sconf->frame_length; 01405 01406 // decode the frame data 01407 if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0)) 01408 av_log(ctx->avctx, AV_LOG_WARNING, 01409 "Reading frame data failed. Skipping RA unit.\n"); 01410 01411 ctx->frame_id++; 01412 01413 // check for size of decoded data 01414 size = ctx->cur_frame_length * avctx->channels * 01415 (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3); 01416 01417 if (size > *data_size) { 01418 av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n"); 01419 return -1; 01420 } 01421 01422 *data_size = size; 01423 01424 // transform decoded frame into output format 01425 #define INTERLEAVE_OUTPUT(bps) \ 01426 { \ 01427 int##bps##_t *dest = (int##bps##_t*) data; \ 01428 shift = bps - ctx->avctx->bits_per_raw_sample; \ 01429 for (sample = 0; sample < ctx->cur_frame_length; sample++) \ 01430 for (c = 0; c < avctx->channels; c++) \ 01431 *dest++ = ctx->raw_samples[c][sample] << shift; \ 01432 } 01433 01434 if (ctx->avctx->bits_per_raw_sample <= 16) { 01435 INTERLEAVE_OUTPUT(16) 01436 } else { 01437 INTERLEAVE_OUTPUT(32) 01438 } 01439 01440 bytes_read = invalid_frame ? buffer_size : 01441 (get_bits_count(&ctx->gb) + 7) >> 3; 01442 01443 return bytes_read; 01444 } 01445 01446 01449 static av_cold int decode_end(AVCodecContext *avctx) 01450 { 01451 ALSDecContext *ctx = avctx->priv_data; 01452 01453 av_freep(&ctx->sconf.chan_pos); 01454 01455 ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status); 01456 01457 av_freep(&ctx->use_ltp); 01458 av_freep(&ctx->ltp_lag); 01459 av_freep(&ctx->ltp_gain); 01460 av_freep(&ctx->ltp_gain_buffer); 01461 av_freep(&ctx->quant_cof); 01462 av_freep(&ctx->lpc_cof); 01463 av_freep(&ctx->quant_cof_buffer); 01464 av_freep(&ctx->lpc_cof_buffer); 01465 av_freep(&ctx->lpc_cof_reversed_buffer); 01466 av_freep(&ctx->prev_raw_samples); 01467 av_freep(&ctx->raw_samples); 01468 av_freep(&ctx->raw_buffer); 01469 av_freep(&ctx->chan_data); 01470 av_freep(&ctx->chan_data_buffer); 01471 av_freep(&ctx->reverted_channels); 01472 01473 return 0; 01474 } 01475 01476 01479 static av_cold int decode_init(AVCodecContext *avctx) 01480 { 01481 unsigned int c; 01482 unsigned int channel_size; 01483 int num_buffers; 01484 ALSDecContext *ctx = avctx->priv_data; 01485 ALSSpecificConfig *sconf = &ctx->sconf; 01486 ctx->avctx = avctx; 01487 01488 if (!avctx->extradata) { 01489 av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n"); 01490 return -1; 01491 } 01492 01493 if (read_specific_config(ctx)) { 01494 av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n"); 01495 decode_end(avctx); 01496 return -1; 01497 } 01498 01499 if (check_specific_config(ctx)) { 01500 decode_end(avctx); 01501 return -1; 01502 } 01503 01504 if (sconf->bgmc) 01505 ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status); 01506 01507 if (sconf->floating) { 01508 avctx->sample_fmt = SAMPLE_FMT_FLT; 01509 avctx->bits_per_raw_sample = 32; 01510 } else { 01511 avctx->sample_fmt = sconf->resolution > 1 01512 ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16; 01513 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8; 01514 } 01515 01516 // set maximum Rice parameter for progressive decoding based on resolution 01517 // This is not specified in 14496-3 but actually done by the reference 01518 // codec RM22 revision 2. 01519 ctx->s_max = sconf->resolution > 1 ? 31 : 15; 01520 01521 // set lag value for long-term prediction 01522 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) + 01523 (avctx->sample_rate >= 192000); 01524 01525 // allocate quantized parcor coefficient buffer 01526 num_buffers = sconf->mc_coding ? avctx->channels : 1; 01527 01528 ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers); 01529 ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers); 01530 ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) * 01531 num_buffers * sconf->max_order); 01532 ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) * 01533 num_buffers * sconf->max_order); 01534 ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) * 01535 sconf->max_order); 01536 01537 if (!ctx->quant_cof || !ctx->lpc_cof || 01538 !ctx->quant_cof_buffer || !ctx->lpc_cof_buffer || 01539 !ctx->lpc_cof_reversed_buffer) { 01540 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); 01541 return AVERROR(ENOMEM); 01542 } 01543 01544 // assign quantized parcor coefficient buffers 01545 for (c = 0; c < num_buffers; c++) { 01546 ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order; 01547 ctx->lpc_cof[c] = ctx->lpc_cof_buffer + c * sconf->max_order; 01548 } 01549 01550 // allocate and assign lag and gain data buffer for ltp mode 01551 ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers); 01552 ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers); 01553 ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers); 01554 ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) * 01555 num_buffers * 5); 01556 01557 if (!ctx->use_ltp || !ctx->ltp_lag || 01558 !ctx->ltp_gain || !ctx->ltp_gain_buffer) { 01559 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); 01560 decode_end(avctx); 01561 return AVERROR(ENOMEM); 01562 } 01563 01564 for (c = 0; c < num_buffers; c++) 01565 ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5; 01566 01567 // allocate and assign channel data buffer for mcc mode 01568 if (sconf->mc_coding) { 01569 ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) * 01570 num_buffers * num_buffers); 01571 ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) * 01572 num_buffers); 01573 ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) * 01574 num_buffers); 01575 01576 if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) { 01577 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); 01578 decode_end(avctx); 01579 return AVERROR(ENOMEM); 01580 } 01581 01582 for (c = 0; c < num_buffers; c++) 01583 ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers; 01584 } else { 01585 ctx->chan_data = NULL; 01586 ctx->chan_data_buffer = NULL; 01587 ctx->reverted_channels = NULL; 01588 } 01589 01590 avctx->frame_size = sconf->frame_length; 01591 channel_size = sconf->frame_length + sconf->max_order; 01592 01593 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order); 01594 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size); 01595 ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels); 01596 01597 // allocate previous raw sample buffer 01598 if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) { 01599 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n"); 01600 decode_end(avctx); 01601 return AVERROR(ENOMEM); 01602 } 01603 01604 // assign raw samples buffers 01605 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order; 01606 for (c = 1; c < avctx->channels; c++) 01607 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size; 01608 01609 return 0; 01610 } 01611 01612 01615 static av_cold void flush(AVCodecContext *avctx) 01616 { 01617 ALSDecContext *ctx = avctx->priv_data; 01618 01619 ctx->frame_id = 0; 01620 } 01621 01622 01623 AVCodec als_decoder = { 01624 "als", 01625 AVMEDIA_TYPE_AUDIO, 01626 CODEC_ID_MP4ALS, 01627 sizeof(ALSDecContext), 01628 decode_init, 01629 NULL, 01630 decode_end, 01631 decode_frame, 01632 .flush = flush, 01633 .capabilities = CODEC_CAP_SUBFRAMES, 01634 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"), 01635 }; 01636