comparison alsdec.c @ 10530:d428e57f14c6 libavcodec

Add long-term prediction to the ALS decoder.
author thilo.borgmann
date Sat, 14 Nov 2009 06:29:19 +0000
parents e5d7b184a5b0
children 142645a57180
comparison
equal deleted inserted replaced
10529:f8fc0a56745e 10530:d428e57f14c6
76 GetBitContext gb; 76 GetBitContext gb;
77 unsigned int cur_frame_length; ///< length of the current frame to decode 77 unsigned int cur_frame_length; ///< length of the current frame to decode
78 unsigned int frame_id; ///< the frame ID / number of the current frame 78 unsigned int frame_id; ///< the frame ID / number of the current frame
79 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced 79 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
80 unsigned int num_blocks; ///< number of blocks used in the current frame 80 unsigned int num_blocks; ///< number of blocks used in the current frame
81 int ltp_lag_length; ///< number of bits used for ltp lag value
81 int32_t *quant_cof; ///< quantized parcor coefficients 82 int32_t *quant_cof; ///< quantized parcor coefficients
82 int32_t *lpc_cof; ///< coefficients of the direct form prediction filter 83 int32_t *lpc_cof; ///< coefficients of the direct form prediction filter
83 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block 84 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
84 int32_t **raw_samples; ///< decoded raw samples for each channel 85 int32_t **raw_samples; ///< decoded raw samples for each channel
85 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples 86 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
268 error = errval; \ 269 error = errval; \
269 } \ 270 } \
270 } 271 }
271 272
272 MISSING_ERR(sconf->floating, "Floating point decoding", -1); 273 MISSING_ERR(sconf->floating, "Floating point decoding", -1);
273 MISSING_ERR(sconf->long_term_prediction, "Long-term prediction", -1);
274 MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1); 274 MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1);
275 MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1); 275 MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1);
276 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1); 276 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
277 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0); 277 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
278 278
441 int32_t *lpc_cof = ctx->lpc_cof; 441 int32_t *lpc_cof = ctx->lpc_cof;
442 unsigned int start = 0; 442 unsigned int start = 0;
443 int smp = 0; 443 int smp = 0;
444 int sb, store_prev_samples; 444 int sb, store_prev_samples;
445 int64_t y; 445 int64_t y;
446 int use_ltp = 0;
447 int ltp_lag = 0;
448 int ltp_gain[5];
446 449
447 *js_blocks = get_bits1(gb); 450 *js_blocks = get_bits1(gb);
448 451
449 // determine the number of subblocks for entropy decoding 452 // determine the number of subblocks for entropy decoding
450 if (!sconf->bgmc && !sconf->sb_part) { 453 if (!sconf->bgmc && !sconf->sb_part) {
538 for (k = 2; k < opt_order; k++) 541 for (k = 2; k < opt_order; k++)
539 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13); 542 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
540 } 543 }
541 } 544 }
542 545
543 // TODO: LTP mode 546 // read LTP gain and lag values
547 if (sconf->long_term_prediction) {
548 use_ltp = get_bits1(gb);
549
550 if (use_ltp) {
551 ltp_gain[0] = decode_rice(gb, 1) << 3;
552 ltp_gain[1] = decode_rice(gb, 2) << 3;
553
554 ltp_gain[2] = ltp_gain_values[get_unary(gb, 0, 4)][get_bits(gb, 2)];
555
556 ltp_gain[3] = decode_rice(gb, 2) << 3;
557 ltp_gain[4] = decode_rice(gb, 1) << 3;
558
559 ltp_lag = get_bits(gb, ctx->ltp_lag_length);
560 ltp_lag += FFMAX(4, opt_order + 1);
561 }
562 }
544 563
545 // read first value and residuals in case of a random access block 564 // read first value and residuals in case of a random access block
546 if (ra_block) { 565 if (ra_block) {
547 if (opt_order) 566 if (opt_order)
548 raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4); 567 raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
562 581
563 for (sb = 0; sb < sub_blocks; sb++, start = 0) 582 for (sb = 0; sb < sub_blocks; sb++, start = 0)
564 for (; start < sb_length; start++) 583 for (; start < sb_length; start++)
565 *current_res++ = decode_rice(gb, s[sb]); 584 *current_res++ = decode_rice(gb, s[sb]);
566 } 585 }
586
587 // reverse long-term prediction
588 if (use_ltp) {
589 int ltp_smp;
590
591 for (ltp_smp = FFMAX(ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
592 int center = ltp_smp - ltp_lag;
593 int begin = FFMAX(0, center - 2);
594 int end = center + 3;
595 int tab = 5 - (end - begin);
596 int base;
597
598 y = 1 << 6;
599
600 for (base = begin; base < end; base++, tab++)
601 y += MUL64(ltp_gain[tab], raw_samples[base]);
602
603 raw_samples[ltp_smp] += y >> 7;
604 }
605 }
567 606
568 // reconstruct all samples from residuals 607 // reconstruct all samples from residuals
569 if (ra_block) { 608 if (ra_block) {
570 for (smp = 0; smp < opt_order; smp++) { 609 for (smp = 0; smp < opt_order; smp++) {
571 y = 1 << 19; 610 y = 1 << 19;
947 avctx->sample_fmt = sconf->resolution > 1 986 avctx->sample_fmt = sconf->resolution > 1
948 ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16; 987 ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
949 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8; 988 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
950 } 989 }
951 990
991 // set lag value for long-term prediction
992 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
993 (avctx->sample_rate >= 192000);
994
952 avctx->frame_size = sconf->frame_length; 995 avctx->frame_size = sconf->frame_length;
953 channel_size = sconf->frame_length + sconf->max_order; 996 channel_size = sconf->frame_length + sconf->max_order;
954 997
955 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order); 998 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
956 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size); 999 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);