X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fg729dec.c;h=9826e63e4937298027f274323a8ac24b09979d58;hb=70efd101f102168d928b394f1e46f555c82afad9;hp=8a35bdc83c4c122d6addb8e0220efe510c8f2918;hpb=2525821f1aadf34e3c874c669feb6be27249e881;p=ffmpeg diff --git a/libavcodec/g729dec.c b/libavcodec/g729dec.c index 8a35bdc83c4..9826e63e493 100644 --- a/libavcodec/g729dec.c +++ b/libavcodec/g729dec.c @@ -71,6 +71,12 @@ */ #define SHARP_MAX 13017 +typedef enum { + FORMAT_G729_8K = 0, + FORMAT_G729D_6K4, + FORMAT_COUNT, +} G729Formats; + typedef struct { uint8_t ac_index_bits[2]; ///< adaptive codebook index for second subframe (size in bits) uint8_t parity_bit; ///< parity bit for pitch delay @@ -90,6 +96,8 @@ typedef struct { int16_t lsfq[10]; ///< (2.13) quantized LSF coefficients from previous frame int16_t lsp_buf[2][10]; ///< (0.15) LSP coefficients (previous and current frames) (3.2.5) int16_t *lsp[2]; ///< pointers to lsp_buf + + int ma_predictor_prev; ///< switched MA predictor of LSP quantizer from last good frame } G729Context; static const G729FormatDescription format_g729_8k = { @@ -111,7 +119,7 @@ static const G729FormatDescription format_g729d_6k4 = { }; /** - * \brief pseudo random number generator + * @brief pseudo random number generator */ static inline uint16_t g729_prng(uint16_t value) { @@ -126,6 +134,15 @@ static inline int get_parity(uint8_t value) return (0x6996966996696996ULL >> (value >> 2)) & 1; } +/* + * Decodes LSF (Line Spectral Frequencies) from L0-L3 (3.2.4). + * @param lsfq [out] (2.13) quantized LSF coefficients + * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames + * @param ma_predictor switched MA predictor of LSP quantizer + * @param vq_1st first stage vector of quantizer + * @param vq_2nd_low second stage lower vector of LSP quantizer + * @param vq_2nd_high second stage higher vector of LSP quantizer + */ static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1], int16_t ma_predictor, int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high) @@ -157,13 +174,33 @@ static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1] lsfq[i] = sum >> 15; } - /* Rotate past_quantizer_outputs. */ - memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*)); - past_quantizer_outputs[0] = quantizer_output; - ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10); } +/** + * Restores past LSP quantizer output using LSF from previous frame + * @param lsfq [in/out] (2.13) quantized LSF coefficients + * @param past_quantizer_outputs [in/out] (2.13) quantizer outputs from previous frames + * @param ma_predictor_prev MA predictor from previous frame + * @param lsfq_prev (2.13) quantized LSF coefficients from previous frame + */ +static void lsf_restore_from_previous(int16_t* lsfq, + int16_t* past_quantizer_outputs[MA_NP + 1], + int ma_predictor_prev) +{ + int16_t* quantizer_output = past_quantizer_outputs[MA_NP]; + int i,k; + + for (i = 0; i < 10; i++) { + int tmp = lsfq[i] << 15; + + for (k = 0; k < MA_NP; k++) + tmp -= past_quantizer_outputs[k][i] * cb_ma_predictor[ma_predictor_prev][k][i]; + + quantizer_output[i] = ((tmp >> 15) * cb_ma_predictor_sum_inv[ma_predictor_prev][i]) >> 12; + } +} + static av_cold int decoder_init(AVCodecContext * avctx) { G729Context* ctx = avctx->priv_data; @@ -201,6 +238,8 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, int frame_erasure = 0; ///< frame erasure detected during decoding int bad_pitch = 0; ///< parity check failed int i; + int16_t *tmp; + G729Formats packet_type; G729Context *ctx = avctx->priv_data; int16_t lp[2][11]; // (3.12) uint8_t ma_predictor; ///< switched MA predictor of LSP quantizer @@ -217,9 +256,11 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, } if (buf_size == 10) { + packet_type = FORMAT_G729_8K; format = format_g729_8k; av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s"); } else if (buf_size == 8) { + packet_type = FORMAT_G729D_6K4; format = format_g729d_6k4; av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s"); } else { @@ -238,9 +279,20 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS); quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS); - lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs, - ma_predictor, - quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi); + if(frame_erasure) + lsf_restore_from_previous(ctx->lsfq, ctx->past_quantizer_outputs, + ctx->ma_predictor_prev); + else { + lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs, + ma_predictor, + quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi); + ctx->ma_predictor_prev = ma_predictor; + } + + tmp = ctx->past_quantizer_outputs[MA_NP]; + memmove(ctx->past_quantizer_outputs + 1, ctx->past_quantizer_outputs, + MA_NP * sizeof(int16_t*)); + ctx->past_quantizer_outputs[0] = tmp; ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10); @@ -263,7 +315,9 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, gc_1st_index = get_bits(&gb, format.gc_1st_index_bits); gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits); - if(!i) { + if (frame_erasure) + pitch_delay_3x = 3 * ctx->pitch_delay_int_prev; + else if(!i) { if (bad_pitch) pitch_delay_3x = 3 * ctx->pitch_delay_int_prev; else @@ -305,6 +359,9 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, ( voicing && frame_erasure) ? 0 : ctx->gain_code, 1 << 13, 14, SUBFRAME_SIZE); + if (frame_erasure) + ctx->pitch_delay_int_prev = FFMIN(ctx->pitch_delay_int_prev + 1, PITCH_DELAY_MAX); + else ctx->pitch_delay_int_prev = pitch_delay_int; }