No point in having the same code twice to do exactly the same thing. Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>tags/n4.0
| @@ -29,7 +29,8 @@ | |||
| #include "libavutil/error.h" | |||
| #include "libavutil/ffmath.h" | |||
| #include "opus.h" | |||
| #include "opus_celt.h" | |||
| #include "opustab.h" | |||
| #include "vorbis.h" | |||
| static const uint16_t opus_frame_duration[32] = { | |||
| @@ -438,3 +439,111 @@ av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, | |||
| return 0; | |||
| } | |||
| void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc) | |||
| { | |||
| float lowband_scratch[8 * 22]; | |||
| float norm1[2 * 8 * 100]; | |||
| float *norm2 = norm1 + 8 * 100; | |||
| int totalbits = (f->framebits << 3) - f->anticollapse_needed; | |||
| int update_lowband = 1; | |||
| int lowband_offset = 0; | |||
| int i, j; | |||
| for (i = f->start_band; i < f->end_band; i++) { | |||
| uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 }; | |||
| int band_offset = ff_celt_freq_bands[i] << f->size; | |||
| int band_size = ff_celt_freq_range[i] << f->size; | |||
| float *X = f->block[0].coeffs + band_offset; | |||
| float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL; | |||
| float *norm_loc1, *norm_loc2; | |||
| int consumed = opus_rc_tell_frac(rc); | |||
| int effective_lowband = -1; | |||
| int b = 0; | |||
| /* Compute how many bits we want to allocate to this band */ | |||
| if (i != f->start_band) | |||
| f->remaining -= consumed; | |||
| f->remaining2 = totalbits - consumed - 1; | |||
| if (i <= f->coded_bands - 1) { | |||
| int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i); | |||
| b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14); | |||
| } | |||
| if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] || | |||
| i == f->start_band + 1) && (update_lowband || lowband_offset == 0)) | |||
| lowband_offset = i; | |||
| if (i == f->start_band + 1) { | |||
| /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into | |||
| the second to ensure the second band never has to use the LCG. */ | |||
| int offset = 8 * ff_celt_freq_bands[i]; | |||
| int count = 8 * (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]); | |||
| memcpy(&norm1[offset], &norm1[offset - count], count * sizeof(float)); | |||
| if (f->channels == 2) | |||
| memcpy(&norm2[offset], &norm2[offset - count], count * sizeof(float)); | |||
| } | |||
| /* Get a conservative estimate of the collapse_mask's for the bands we're | |||
| going to be folding from. */ | |||
| if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE || | |||
| f->blocks > 1 || f->tf_change[i] < 0)) { | |||
| int foldstart, foldend; | |||
| /* This ensures we never repeat spectral content within one band */ | |||
| effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band], | |||
| ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]); | |||
| foldstart = lowband_offset; | |||
| while (ff_celt_freq_bands[--foldstart] > effective_lowband); | |||
| foldend = lowband_offset - 1; | |||
| while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]); | |||
| cm[0] = cm[1] = 0; | |||
| for (j = foldstart; j < foldend; j++) { | |||
| cm[0] |= f->block[0].collapse_masks[j]; | |||
| cm[1] |= f->block[f->channels - 1].collapse_masks[j]; | |||
| } | |||
| } | |||
| if (f->dual_stereo && i == f->intensity_stereo) { | |||
| /* Switch off dual stereo to do intensity */ | |||
| f->dual_stereo = 0; | |||
| for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++) | |||
| norm1[j] = (norm1[j] + norm2[j]) / 2; | |||
| } | |||
| norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->size) : NULL; | |||
| norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL; | |||
| if (f->dual_stereo) { | |||
| cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, NULL, band_size, b >> 1, | |||
| f->blocks, norm_loc1, f->size, | |||
| norm1 + band_offset, 0, 1.0f, | |||
| lowband_scratch, cm[0]); | |||
| cm[1] = f->pvq->quant_band(f->pvq, f, rc, i, Y, NULL, band_size, b >> 1, | |||
| f->blocks, norm_loc2, f->size, | |||
| norm2 + band_offset, 0, 1.0f, | |||
| lowband_scratch, cm[1]); | |||
| } else { | |||
| cm[0] = f->pvq->quant_band(f->pvq, f, rc, i, X, Y, band_size, b >> 0, | |||
| f->blocks, norm_loc1, f->size, | |||
| norm1 + band_offset, 0, 1.0f, | |||
| lowband_scratch, cm[0] | cm[1]); | |||
| cm[1] = cm[0]; | |||
| } | |||
| f->block[0].collapse_masks[i] = (uint8_t)cm[0]; | |||
| f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1]; | |||
| f->remaining += f->pulses[i] + consumed; | |||
| /* Update the folding position only as long as we have 1 bit/sample depth */ | |||
| update_lowband = (b > band_size << 3); | |||
| } | |||
| } | |||
| @@ -191,4 +191,7 @@ int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, | |||
| enum OpusBandwidth bandwidth, int coded_channels, | |||
| int duration_ms); | |||
| /* Encode or decode CELT bands */ | |||
| void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc); | |||
| #endif /* AVCODEC_OPUS_H */ | |||
| @@ -676,110 +676,6 @@ static void process_anticollapse(CeltFrame *f, CeltBlock *block, float *X) | |||
| } | |||
| } | |||
| static void celt_decode_bands(CeltFrame *f, OpusRangeCoder *rc) | |||
| { | |||
| float lowband_scratch[8 * 22]; | |||
| float norm[2 * 8 * 100]; | |||
| int totalbits = (f->framebits << 3) - f->anticollapse_needed; | |||
| int update_lowband = 1; | |||
| int lowband_offset = 0; | |||
| int i, j; | |||
| memset(f->block[0].coeffs, 0, sizeof(f->block[0].coeffs)); | |||
| memset(f->block[1].coeffs, 0, sizeof(f->block[0].coeffs)); | |||
| for (i = f->start_band; i < f->end_band; i++) { | |||
| uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 }; | |||
| int band_offset = ff_celt_freq_bands[i] << f->size; | |||
| int band_size = ff_celt_freq_range[i] << f->size; | |||
| float *X = f->block[0].coeffs + band_offset; | |||
| float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL; | |||
| int consumed = opus_rc_tell_frac(rc); | |||
| float *norm2 = norm + 8 * 100; | |||
| int effective_lowband = -1; | |||
| int b = 0; | |||
| /* Compute how many bits we want to allocate to this band */ | |||
| if (i != f->start_band) | |||
| f->remaining -= consumed; | |||
| f->remaining2 = totalbits - consumed - 1; | |||
| if (i <= f->coded_bands - 1) { | |||
| int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i); | |||
| b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14); | |||
| } | |||
| if ((ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] || | |||
| i == f->start_band + 1) && (update_lowband || lowband_offset == 0)) | |||
| lowband_offset = i; | |||
| if (i == f->start_band + 1) { | |||
| /* Special Hybrid Folding (RFC 8251 section 9). Copy the first band into | |||
| the second to ensure the second band never has to use the LCG. */ | |||
| int offset = 8 * ff_celt_freq_bands[i]; | |||
| int count = 8 * (ff_celt_freq_range[i] - ff_celt_freq_range[i-1]); | |||
| memcpy(&norm[offset], &norm[offset - count], count * sizeof(float)); | |||
| if (f->channels == 2) | |||
| memcpy(&norm2[offset], &norm2[offset - count], count * sizeof(float)); | |||
| } | |||
| /* Get a conservative estimate of the collapse_mask's for the bands we're | |||
| going to be folding from. */ | |||
| if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE || | |||
| f->blocks > 1 || f->tf_change[i] < 0)) { | |||
| int foldstart, foldend; | |||
| /* This ensures we never repeat spectral content within one band */ | |||
| effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band], | |||
| ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]); | |||
| foldstart = lowband_offset; | |||
| while (ff_celt_freq_bands[--foldstart] > effective_lowband); | |||
| foldend = lowband_offset - 1; | |||
| while (++foldend < i && ff_celt_freq_bands[foldend] < effective_lowband + ff_celt_freq_range[i]); | |||
| cm[0] = cm[1] = 0; | |||
| for (j = foldstart; j < foldend; j++) { | |||
| cm[0] |= f->block[0].collapse_masks[j]; | |||
| cm[1] |= f->block[f->channels - 1].collapse_masks[j]; | |||
| } | |||
| } | |||
| if (f->dual_stereo && i == f->intensity_stereo) { | |||
| /* Switch off dual stereo to do intensity */ | |||
| f->dual_stereo = 0; | |||
| for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++) | |||
| norm[j] = (norm[j] + norm2[j]) / 2; | |||
| } | |||
| if (f->dual_stereo) { | |||
| cm[0] = f->pvq->decode_band(f->pvq, f, rc, i, X, NULL, band_size, b / 2, f->blocks, | |||
| effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size, | |||
| norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]); | |||
| cm[1] = f->pvq->decode_band(f->pvq, f, rc, i, Y, NULL, band_size, b/2, f->blocks, | |||
| effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL, f->size, | |||
| norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]); | |||
| } else { | |||
| cm[0] = f->pvq->decode_band(f->pvq, f, rc, i, X, Y, band_size, b, f->blocks, | |||
| effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size, | |||
| norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]|cm[1]); | |||
| cm[1] = cm[0]; | |||
| } | |||
| f->block[0].collapse_masks[i] = (uint8_t)cm[0]; | |||
| f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1]; | |||
| f->remaining += f->pulses[i] + consumed; | |||
| /* Update the folding position only as long as we have 1 bit/sample depth */ | |||
| update_lowband = (b > band_size << 3); | |||
| } | |||
| } | |||
| int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, | |||
| float **output, int channels, int frame_size, | |||
| int start_band, int end_band) | |||
| @@ -819,8 +715,10 @@ int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, | |||
| if (!f->output_channels) | |||
| f->output_channels = channels; | |||
| memset(f->block[0].collapse_masks, 0, sizeof(f->block[0].collapse_masks)); | |||
| memset(f->block[1].collapse_masks, 0, sizeof(f->block[1].collapse_masks)); | |||
| for (i = 0; i < f->channels; i++) { | |||
| memset(f->block[i].coeffs, 0, sizeof(f->block[i].coeffs)); | |||
| memset(f->block[i].collapse_masks, 0, sizeof(f->block[i].collapse_masks)); | |||
| } | |||
| consumed = opus_rc_tell(rc); | |||
| @@ -857,7 +755,7 @@ int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, | |||
| celt_decode_tf_changes (f, rc); | |||
| celt_decode_allocation (f, rc); | |||
| celt_decode_fine_energy (f, rc); | |||
| celt_decode_bands (f, rc); | |||
| ff_celt_quant_bands (f, rc); | |||
| if (f->anticollapse_needed) | |||
| f->anticollapse = ff_opus_rc_get_raw(rc, 1); | |||
| @@ -1021,7 +919,7 @@ int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, | |||
| if ((ret = ff_mdct15_init(&frm->imdct[i], 1, i + 3, -1.0f/32768)) < 0) | |||
| goto fail; | |||
| if ((ret = ff_celt_pvq_init(&frm->pvq)) < 0) | |||
| if ((ret = ff_celt_pvq_init(&frm->pvq, 0)) < 0) | |||
| goto fail; | |||
| frm->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |||
| @@ -486,8 +486,7 @@ static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |||
| int duration, float *lowband_out, | |||
| int level, float gain, | |||
| float *lowband_scratch, | |||
| int fill, int quant, | |||
| QUANT_FN(*rec)) | |||
| int fill, int quant) | |||
| { | |||
| int i; | |||
| const uint8_t *cache; | |||
| @@ -700,8 +699,8 @@ static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |||
| sign = 1 - 2 * sign; | |||
| /* We use orig_fill here because we want to fold the side, but if | |||
| itheta==16384, we'll have cleared the low bits of fill. */ | |||
| cm = rec(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration, | |||
| lowband_out, level, gain, lowband_scratch, orig_fill); | |||
| cm = pvq->quant_band(pvq, f, rc, band, x2, NULL, N, mbits, blocks, lowband, duration, | |||
| lowband_out, level, gain, lowband_scratch, orig_fill); | |||
| /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | |||
| and there's no need to worry about mixing with the other channel. */ | |||
| y2[0] = -sign * x2[1]; | |||
| @@ -753,24 +752,25 @@ static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |||
| if (mbits >= sbits) { | |||
| /* In stereo mode, we do not apply a scaling to the mid | |||
| * because we need the normalized mid for folding later */ | |||
| cm = rec(pvq, f, rc, band, X, NULL, N, mbits, blocks, lowband, | |||
| duration, next_lowband_out1, next_level, | |||
| stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | |||
| cm = pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, | |||
| lowband, duration, next_lowband_out1, next_level, | |||
| stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | |||
| rebalance = mbits - (rebalance - f->remaining2); | |||
| if (rebalance > 3 << 3 && itheta != 0) | |||
| sbits += rebalance - (3 << 3); | |||
| /* For a stereo split, the high bits of fill are always zero, | |||
| * so no folding will be done to the side. */ | |||
| cmt = rec(pvq, f, rc, band, Y, NULL, N, sbits, blocks, next_lowband2, | |||
| duration, NULL, next_level, gain * side, NULL, | |||
| fill >> blocks); | |||
| cmt = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |||
| next_lowband2, duration, NULL, next_level, | |||
| gain * side, NULL, fill >> blocks); | |||
| cm |= cmt << ((B0 >> 1) & (stereo - 1)); | |||
| } else { | |||
| /* For a stereo split, the high bits of fill are always zero, | |||
| * so no folding will be done to the side. */ | |||
| cm = rec(pvq, f, rc, band, Y, NULL, N, sbits, blocks, next_lowband2, | |||
| duration, NULL, next_level, gain * side, NULL, fill >> blocks); | |||
| cm = pvq->quant_band(pvq, f, rc, band, Y, NULL, N, sbits, blocks, | |||
| next_lowband2, duration, NULL, next_level, | |||
| gain * side, NULL, fill >> blocks); | |||
| cm <<= ((B0 >> 1) & (stereo - 1)); | |||
| rebalance = sbits - (rebalance - f->remaining2); | |||
| if (rebalance > 3 << 3 && itheta != 16384) | |||
| @@ -778,9 +778,9 @@ static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |||
| /* In stereo mode, we do not apply a scaling to the mid because | |||
| * we need the normalized mid for folding later */ | |||
| cm |= rec(pvq, f, rc, band, X, NULL, N, mbits, blocks, lowband, duration, | |||
| next_lowband_out1, next_level, stereo ? 1.0f : (gain * mid), | |||
| lowband_scratch, fill); | |||
| cm |= pvq->quant_band(pvq, f, rc, band, X, NULL, N, mbits, blocks, | |||
| lowband, duration, next_lowband_out1, next_level, | |||
| stereo ? 1.0f : (gain * mid), lowband_scratch, fill); | |||
| } | |||
| } | |||
| } else { | |||
| @@ -874,19 +874,16 @@ static av_always_inline uint32_t quant_band_template(CeltPVQ *pvq, CeltFrame *f, | |||
| return cm; | |||
| } | |||
| static QUANT_FN(pvq_decode_band) | |||
| { | |||
| return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |||
| lowband_out, level, gain, lowband_scratch, fill, 0, | |||
| pvq->decode_band); | |||
| lowband_out, level, gain, lowband_scratch, fill, 0); | |||
| } | |||
| static QUANT_FN(pvq_encode_band) | |||
| { | |||
| return quant_band_template(pvq, f, rc, band, X, Y, N, b, blocks, lowband, duration, | |||
| lowband_out, level, gain, lowband_scratch, fill, 1, | |||
| pvq->encode_band); | |||
| lowband_out, level, gain, lowband_scratch, fill, 1); | |||
| } | |||
| static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band, | |||
| @@ -914,14 +911,14 @@ static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int b | |||
| } | |||
| if (f->dual_stereo) { | |||
| pvq->encode_band(pvq, f, rc, band, X, NULL, band_size, b / 2, f->blocks, NULL, | |||
| f->size, norm1, 0, 1.0f, lowband_scratch, cm[0]); | |||
| pvq->quant_band(pvq, f, rc, band, X, NULL, band_size, b / 2, f->blocks, NULL, | |||
| f->size, norm1, 0, 1.0f, lowband_scratch, cm[0]); | |||
| pvq->encode_band(pvq, f, rc, band, Y, NULL, band_size, b / 2, f->blocks, NULL, | |||
| f->size, norm2, 0, 1.0f, lowband_scratch, cm[1]); | |||
| pvq->quant_band(pvq, f, rc, band, Y, NULL, band_size, b / 2, f->blocks, NULL, | |||
| f->size, norm2, 0, 1.0f, lowband_scratch, cm[1]); | |||
| } else { | |||
| pvq->encode_band(pvq, f, rc, band, X, Y, band_size, b, f->blocks, NULL, f->size, | |||
| norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]); | |||
| pvq->quant_band(pvq, f, rc, band, X, Y, band_size, b, f->blocks, NULL, f->size, | |||
| norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]); | |||
| } | |||
| for (i = 0; i < band_size; i++) { | |||
| @@ -939,16 +936,15 @@ static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int b | |||
| return lambda*dist*cost; | |||
| } | |||
| int av_cold ff_celt_pvq_init(CeltPVQ **pvq) | |||
| int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode) | |||
| { | |||
| CeltPVQ *s = av_malloc(sizeof(CeltPVQ)); | |||
| if (!s) | |||
| return AVERROR(ENOMEM); | |||
| s->pvq_search = ppp_pvq_search_c; | |||
| s->decode_band = pvq_decode_band; | |||
| s->encode_band = pvq_encode_band; | |||
| s->band_cost = pvq_band_cost; | |||
| s->pvq_search = ppp_pvq_search_c; | |||
| s->quant_band = encode ? pvq_encode_band : pvq_decode_band; | |||
| s->band_cost = pvq_band_cost; | |||
| if (ARCH_X86) | |||
| ff_opus_dsp_init_x86(s); | |||
| @@ -38,14 +38,14 @@ struct CeltPVQ { | |||
| float (*pvq_search)(float *X, int *y, int K, int N); | |||
| QUANT_FN(*decode_band); | |||
| QUANT_FN(*encode_band); | |||
| QUANT_FN(*quant_band); | |||
| float (*band_cost)(struct CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, | |||
| int band, float *bits, float lambda); | |||
| }; | |||
| int ff_celt_pvq_init (struct CeltPVQ **pvq); | |||
| void ff_opus_dsp_init_x86(struct CeltPVQ *s); | |||
| int ff_celt_pvq_init(struct CeltPVQ **pvq, int encode); | |||
| void ff_celt_pvq_uninit(struct CeltPVQ **pvq); | |||
| #endif /* AVCODEC_OPUS_PVQ_H */ | |||
| @@ -255,7 +255,7 @@ static void celt_frame_mdct(OpusEncContext *s, CeltFrame *f) | |||
| } | |||
| } | |||
| static void celt_enc_tf(OpusRangeCoder *rc, CeltFrame *f) | |||
| static void celt_enc_tf(CeltFrame *f, OpusRangeCoder *rc) | |||
| { | |||
| int i, tf_select = 0, diff = 0, tf_changed = 0, tf_select_needed; | |||
| int bits = f->transient ? 2 : 4; | |||
| @@ -282,7 +282,7 @@ static void celt_enc_tf(OpusRangeCoder *rc, CeltFrame *f) | |||
| f->tf_change[i] = ff_celt_tf_select[f->size][f->transient][tf_select][f->tf_change[i]]; | |||
| } | |||
| void ff_celt_enc_bitalloc(OpusRangeCoder *rc, CeltFrame *f) | |||
| void ff_celt_enc_bitalloc(CeltFrame *f, OpusRangeCoder *rc) | |||
| { | |||
| int i, j, low, high, total, done, bandbits, remaining, tbits_8ths; | |||
| int skip_startband = f->start_band; | |||
| @@ -690,7 +690,7 @@ static void exp_quant_coarse(OpusRangeCoder *rc, CeltFrame *f, | |||
| } | |||
| } | |||
| static void celt_quant_coarse(OpusRangeCoder *rc, CeltFrame *f, | |||
| static void celt_quant_coarse(CeltFrame *f, OpusRangeCoder *rc, | |||
| float last_energy[][CELT_MAX_BANDS]) | |||
| { | |||
| uint32_t inter, intra; | |||
| @@ -710,7 +710,7 @@ static void celt_quant_coarse(OpusRangeCoder *rc, CeltFrame *f, | |||
| } | |||
| } | |||
| static void celt_quant_fine(OpusRangeCoder *rc, CeltFrame *f) | |||
| static void celt_quant_fine(CeltFrame *f, OpusRangeCoder *rc) | |||
| { | |||
| int i, ch; | |||
| for (i = f->start_band; i < f->end_band; i++) { | |||
| @@ -747,95 +747,6 @@ static void celt_quant_final(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f | |||
| } | |||
| } | |||
| static void celt_quant_bands(OpusRangeCoder *rc, CeltFrame *f) | |||
| { | |||
| float lowband_scratch[8 * 22]; | |||
| float norm[2 * 8 * 100]; | |||
| int totalbits = (f->framebits << 3) - f->anticollapse_needed; | |||
| int update_lowband = 1; | |||
| int lowband_offset = 0; | |||
| int i, j; | |||
| for (i = f->start_band; i < f->end_band; i++) { | |||
| uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 }; | |||
| int band_offset = ff_celt_freq_bands[i] << f->size; | |||
| int band_size = ff_celt_freq_range[i] << f->size; | |||
| float *X = f->block[0].coeffs + band_offset; | |||
| float *Y = (f->channels == 2) ? f->block[1].coeffs + band_offset : NULL; | |||
| int consumed = opus_rc_tell_frac(rc); | |||
| float *norm2 = norm + 8 * 100; | |||
| int effective_lowband = -1; | |||
| int b = 0; | |||
| /* Compute how many bits we want to allocate to this band */ | |||
| if (i != f->start_band) | |||
| f->remaining -= consumed; | |||
| f->remaining2 = totalbits - consumed - 1; | |||
| if (i <= f->coded_bands - 1) { | |||
| int curr_balance = f->remaining / FFMIN(3, f->coded_bands-i); | |||
| b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[i] + curr_balance), 14); | |||
| } | |||
| if (ff_celt_freq_bands[i] - ff_celt_freq_range[i] >= ff_celt_freq_bands[f->start_band] && | |||
| (update_lowband || lowband_offset == 0)) | |||
| lowband_offset = i; | |||
| /* Get a conservative estimate of the collapse_mask's for the bands we're | |||
| going to be folding from. */ | |||
| if (lowband_offset != 0 && (f->spread != CELT_SPREAD_AGGRESSIVE || | |||
| f->blocks > 1 || f->tf_change[i] < 0)) { | |||
| int foldstart, foldend; | |||
| /* This ensures we never repeat spectral content within one band */ | |||
| effective_lowband = FFMAX(ff_celt_freq_bands[f->start_band], | |||
| ff_celt_freq_bands[lowband_offset] - ff_celt_freq_range[i]); | |||
| foldstart = lowband_offset; | |||
| while (ff_celt_freq_bands[--foldstart] > effective_lowband); | |||
| foldend = lowband_offset - 1; | |||
| while (ff_celt_freq_bands[++foldend] < effective_lowband + ff_celt_freq_range[i]); | |||
| cm[0] = cm[1] = 0; | |||
| for (j = foldstart; j < foldend; j++) { | |||
| cm[0] |= f->block[0].collapse_masks[j]; | |||
| cm[1] |= f->block[f->channels - 1].collapse_masks[j]; | |||
| } | |||
| } | |||
| if (f->dual_stereo && i == f->intensity_stereo) { | |||
| /* Switch off dual stereo to do intensity */ | |||
| f->dual_stereo = 0; | |||
| for (j = ff_celt_freq_bands[f->start_band] << f->size; j < band_offset; j++) | |||
| norm[j] = (norm[j] + norm2[j]) / 2; | |||
| } | |||
| if (f->dual_stereo) { | |||
| cm[0] = f->pvq->encode_band(f->pvq, f, rc, i, X, NULL, band_size, b / 2, f->blocks, | |||
| effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size, | |||
| norm + band_offset, 0, 1.0f, lowband_scratch, cm[0]); | |||
| cm[1] = f->pvq->encode_band(f->pvq, f, rc, i, Y, NULL, band_size, b / 2, f->blocks, | |||
| effective_lowband != -1 ? norm2 + (effective_lowband << f->size) : NULL, f->size, | |||
| norm2 + band_offset, 0, 1.0f, lowband_scratch, cm[1]); | |||
| } else { | |||
| cm[0] = f->pvq->encode_band(f->pvq, f, rc, i, X, Y, band_size, b, f->blocks, | |||
| effective_lowband != -1 ? norm + (effective_lowband << f->size) : NULL, f->size, | |||
| norm + band_offset, 0, 1.0f, lowband_scratch, cm[0] | cm[1]); | |||
| cm[1] = cm[0]; | |||
| } | |||
| f->block[0].collapse_masks[i] = (uint8_t)cm[0]; | |||
| f->block[f->channels - 1].collapse_masks[i] = (uint8_t)cm[1]; | |||
| f->remaining += f->pulses[i] + consumed; | |||
| /* Update the folding position only as long as we have 1 bit/sample depth */ | |||
| update_lowband = (b > band_size << 3); | |||
| } | |||
| } | |||
| static void celt_encode_frame(OpusEncContext *s, OpusRangeCoder *rc, | |||
| CeltFrame *f, int index) | |||
| { | |||
| @@ -883,11 +794,11 @@ static void celt_encode_frame(OpusEncContext *s, OpusRangeCoder *rc, | |||
| ff_opus_rc_enc_log(rc, f->transient, 3); | |||
| /* Main encoding */ | |||
| celt_quant_coarse(rc, f, s->last_quantized_energy); | |||
| celt_enc_tf (rc, f); | |||
| ff_celt_enc_bitalloc(rc, f); | |||
| celt_quant_fine (rc, f); | |||
| celt_quant_bands (rc, f); | |||
| celt_quant_coarse (f, rc, s->last_quantized_energy); | |||
| celt_enc_tf (f, rc); | |||
| ff_celt_enc_bitalloc(f, rc); | |||
| celt_quant_fine (f, rc); | |||
| ff_celt_quant_bands (f, rc); | |||
| /* Anticollapse bit */ | |||
| if (f->anticollapse_needed) | |||
| @@ -1080,7 +991,7 @@ static av_cold int opus_encode_init(AVCodecContext *avctx) | |||
| ff_af_queue_init(avctx, &s->afq); | |||
| if ((ret = ff_celt_pvq_init(&s->pvq)) < 0) | |||
| if ((ret = ff_celt_pvq_init(&s->pvq, 1)) < 0) | |||
| return ret; | |||
| if (!(s->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT))) | |||
| @@ -1117,6 +1028,7 @@ static av_cold int opus_encode_init(AVCodecContext *avctx) | |||
| s->frame[i].avctx = s->avctx; | |||
| s->frame[i].seed = 0; | |||
| s->frame[i].pvq = s->pvq; | |||
| s->frame[i].apply_phase_inv = 1; | |||
| s->frame[i].block[0].emph_coeff = s->frame[i].block[1].emph_coeff = 0.0f; | |||
| } | |||
| @@ -51,6 +51,6 @@ typedef struct OpusPacketInfo { | |||
| int frames; | |||
| } OpusPacketInfo; | |||
| void ff_celt_enc_bitalloc(OpusRangeCoder *rc, CeltFrame *f); | |||
| void ff_celt_enc_bitalloc(CeltFrame *f, OpusRangeCoder *rc); | |||
| #endif /* AVCODEC_OPUSENC_H */ | |||
| @@ -316,7 +316,7 @@ static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist) | |||
| OpusRangeCoder dump; | |||
| ff_opus_rc_enc_init(&dump); | |||
| ff_celt_enc_bitalloc(&dump, f); | |||
| ff_celt_enc_bitalloc(f, &dump); | |||
| for (i = 0; i < CELT_MAX_BANDS; i++) { | |||
| float bits = 0.0f; | |||