|
|
|
@ -25,23 +25,27 @@ |
|
|
|
|
* G.723.1 compatible decoder |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include "avcodec.h" |
|
|
|
|
#define BITSTREAM_READER_LE |
|
|
|
|
#include "libavutil/audioconvert.h" |
|
|
|
|
#include "libavutil/lzo.h" |
|
|
|
|
#include "libavutil/opt.h" |
|
|
|
|
#include "avcodec.h" |
|
|
|
|
#include "internal.h" |
|
|
|
|
#include "get_bits.h" |
|
|
|
|
#include "acelp_vectors.h" |
|
|
|
|
#include "celp_filters.h" |
|
|
|
|
#include "celp_math.h" |
|
|
|
|
#include "lsp.h" |
|
|
|
|
#include "libavutil/lzo.h" |
|
|
|
|
#include "g723_1_data.h" |
|
|
|
|
|
|
|
|
|
typedef struct g723_1_context { |
|
|
|
|
AVClass *class; |
|
|
|
|
AVFrame frame; |
|
|
|
|
|
|
|
|
|
G723_1_Subframe subframe[4]; |
|
|
|
|
FrameType cur_frame_type; |
|
|
|
|
FrameType past_frame_type; |
|
|
|
|
Rate cur_rate; |
|
|
|
|
enum FrameType cur_frame_type; |
|
|
|
|
enum FrameType past_frame_type; |
|
|
|
|
enum Rate cur_rate; |
|
|
|
|
uint8_t lsp_index[LSP_BANDS]; |
|
|
|
|
int pitch_lag[2]; |
|
|
|
|
int erased_frames; |
|
|
|
@ -62,6 +66,7 @@ typedef struct g723_1_context { |
|
|
|
|
int pf_gain; ///< formant postfilter
|
|
|
|
|
///< gain scaling unit memory
|
|
|
|
|
|
|
|
|
|
int postfilter; |
|
|
|
|
int16_t prev_data[HALF_FRAME_LEN]; |
|
|
|
|
int16_t prev_weight_sig[PITCH_MAX]; |
|
|
|
|
|
|
|
|
@ -80,11 +85,12 @@ static av_cold int g723_1_decode_init(AVCodecContext *avctx) |
|
|
|
|
|
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
|
|
|
|
p->pf_gain = 1 << 12; |
|
|
|
|
memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t)); |
|
|
|
|
|
|
|
|
|
avcodec_get_frame_defaults(&p->frame); |
|
|
|
|
avctx->coded_frame = &p->frame; |
|
|
|
|
|
|
|
|
|
memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp)); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -108,7 +114,7 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, |
|
|
|
|
info_bits = get_bits(&gb, 2); |
|
|
|
|
|
|
|
|
|
if (info_bits == 3) { |
|
|
|
|
p->cur_frame_type = UntransmittedFrame; |
|
|
|
|
p->cur_frame_type = UNTRANSMITTED_FRAME; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -118,14 +124,14 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, |
|
|
|
|
p->lsp_index[0] = get_bits(&gb, 8); |
|
|
|
|
|
|
|
|
|
if (info_bits == 2) { |
|
|
|
|
p->cur_frame_type = SIDFrame; |
|
|
|
|
p->cur_frame_type = SID_FRAME; |
|
|
|
|
p->subframe[0].amp_index = get_bits(&gb, 6); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Extract the info common to both rates */ |
|
|
|
|
p->cur_rate = info_bits ? Rate5k3 : Rate6k3; |
|
|
|
|
p->cur_frame_type = ActiveFrame; |
|
|
|
|
p->cur_rate = info_bits ? RATE_5300 : RATE_6300; |
|
|
|
|
p->cur_frame_type = ACTIVE_FRAME; |
|
|
|
|
|
|
|
|
|
p->pitch_lag[0] = get_bits(&gb, 7); |
|
|
|
|
if (p->pitch_lag[0] > 123) /* test if forbidden code */ |
|
|
|
@ -146,9 +152,9 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, |
|
|
|
|
temp = get_bits(&gb, 12); |
|
|
|
|
ad_cb_len = 170; |
|
|
|
|
p->subframe[i].dirac_train = 0; |
|
|
|
|
if (p->cur_rate == Rate6k3 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) { |
|
|
|
|
if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) { |
|
|
|
|
p->subframe[i].dirac_train = temp >> 11; |
|
|
|
|
temp &= 0x7ff; |
|
|
|
|
temp &= 0x7FF; |
|
|
|
|
ad_cb_len = 85; |
|
|
|
|
} |
|
|
|
|
p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS); |
|
|
|
@ -165,7 +171,7 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, |
|
|
|
|
p->subframe[2].grid_index = get_bits1(&gb); |
|
|
|
|
p->subframe[3].grid_index = get_bits1(&gb); |
|
|
|
|
|
|
|
|
|
if (p->cur_rate == Rate6k3) { |
|
|
|
|
if (p->cur_rate == RATE_6300) { |
|
|
|
|
skip_bits1(&gb); /* skip reserved bit */ |
|
|
|
|
|
|
|
|
|
/* Compute pulse_pos index using the 13-bit combined position index */ |
|
|
|
@ -192,7 +198,7 @@ static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf, |
|
|
|
|
p->subframe[1].pulse_sign = get_bits(&gb, 5); |
|
|
|
|
p->subframe[2].pulse_sign = get_bits(&gb, 6); |
|
|
|
|
p->subframe[3].pulse_sign = get_bits(&gb, 5); |
|
|
|
|
} else { /* Rate5k3 */ |
|
|
|
|
} else { /* 5300 bps */ |
|
|
|
|
p->subframe[0].pulse_pos = get_bits(&gb, 12); |
|
|
|
|
p->subframe[1].pulse_pos = get_bits(&gb, 12); |
|
|
|
|
p->subframe[2].pulse_pos = get_bits(&gb, 12); |
|
|
|
@ -333,7 +339,7 @@ static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp, |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (!stable) |
|
|
|
|
memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(int16_t)); |
|
|
|
|
memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -432,7 +438,7 @@ static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp) |
|
|
|
|
8192, 8192, 1 << 13, 14, LPC_ORDER); |
|
|
|
|
ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp, |
|
|
|
|
12288, 4096, 1 << 13, 14, LPC_ORDER); |
|
|
|
|
memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(int16_t)); |
|
|
|
|
memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc)); |
|
|
|
|
|
|
|
|
|
for (i = 0; i < SUBFRAMES; i++) { |
|
|
|
|
lsp2lpc(lpc_ptr); |
|
|
|
@ -448,7 +454,7 @@ static void gen_dirac_train(int16_t *buf, int pitch_lag) |
|
|
|
|
int16_t vector[SUBFRAME_LEN]; |
|
|
|
|
int i, j; |
|
|
|
|
|
|
|
|
|
memcpy(vector, buf, SUBFRAME_LEN * sizeof(int16_t)); |
|
|
|
|
memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector)); |
|
|
|
|
for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) { |
|
|
|
|
for (j = 0; j < SUBFRAME_LEN - i; j++) |
|
|
|
|
buf[i + j] += vector[j]; |
|
|
|
@ -465,13 +471,13 @@ static void gen_dirac_train(int16_t *buf, int pitch_lag) |
|
|
|
|
* @param index current subframe index |
|
|
|
|
*/ |
|
|
|
|
static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe subfrm, |
|
|
|
|
Rate cur_rate, int pitch_lag, int index) |
|
|
|
|
enum Rate cur_rate, int pitch_lag, int index) |
|
|
|
|
{ |
|
|
|
|
int temp, i, j; |
|
|
|
|
|
|
|
|
|
memset(vector, 0, SUBFRAME_LEN * sizeof(int16_t)); |
|
|
|
|
memset(vector, 0, SUBFRAME_LEN * sizeof(*vector)); |
|
|
|
|
|
|
|
|
|
if (cur_rate == Rate6k3) { |
|
|
|
|
if (cur_rate == RATE_6300) { |
|
|
|
|
if (subfrm.pulse_pos >= max_pos[index]) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
@ -495,7 +501,7 @@ static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe subfrm, |
|
|
|
|
} |
|
|
|
|
if (subfrm.dirac_train == 1) |
|
|
|
|
gen_dirac_train(vector, pitch_lag); |
|
|
|
|
} else { /* Rate5k3 */ |
|
|
|
|
} else { /* 5300 bps */ |
|
|
|
|
int cb_gain = fixed_cb_gain[subfrm.amp_index]; |
|
|
|
|
int cb_shift = subfrm.grid_index; |
|
|
|
|
int cb_sign = subfrm.pulse_sign; |
|
|
|
@ -554,7 +560,7 @@ static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation, |
|
|
|
|
get_residual(residual, prev_excitation, lag); |
|
|
|
|
|
|
|
|
|
/* Select quantization table */ |
|
|
|
|
if (cur_rate == Rate6k3 && pitch_lag < SUBFRAME_LEN - 2) { |
|
|
|
|
if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) { |
|
|
|
|
cb_ptr = adaptive_cb_gain85; |
|
|
|
|
} else |
|
|
|
|
cb_ptr = adaptive_cb_gain170; |
|
|
|
@ -608,7 +614,7 @@ static int autocorr_max(G723_1_Context *p, int offset, int *ccr_max, |
|
|
|
|
* @param ccr cross-correlation |
|
|
|
|
* @param res_eng residual energy |
|
|
|
|
*/ |
|
|
|
|
static void comp_ppf_gains(int lag, PPFParam *ppf, Rate cur_rate, |
|
|
|
|
static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate, |
|
|
|
|
int tgt_eng, int ccr, int res_eng) |
|
|
|
|
{ |
|
|
|
|
int pf_residual; /* square of postfiltered residual */ |
|
|
|
@ -657,7 +663,7 @@ static void comp_ppf_gains(int lag, PPFParam *ppf, Rate cur_rate, |
|
|
|
|
* @param cur_rate current bitrate |
|
|
|
|
*/ |
|
|
|
|
static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag, |
|
|
|
|
PPFParam *ppf, Rate cur_rate) |
|
|
|
|
PPFParam *ppf, enum Rate cur_rate) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
int16_t scale; |
|
|
|
@ -795,15 +801,15 @@ static void residual_interp(int16_t *buf, int16_t *out, int lag, |
|
|
|
|
/* Attenuate */ |
|
|
|
|
for (i = 0; i < lag; i++) |
|
|
|
|
vector_ptr[i - lag] = vector_ptr[i - lag] * 3 >> 2; |
|
|
|
|
av_memcpy_backptr((uint8_t*)vector_ptr, lag * sizeof(int16_t), |
|
|
|
|
FRAME_LEN * sizeof(int16_t)); |
|
|
|
|
memcpy(out, vector_ptr, FRAME_LEN * sizeof(int16_t)); |
|
|
|
|
av_memcpy_backptr((uint8_t*)vector_ptr, lag * sizeof(*vector_ptr), |
|
|
|
|
FRAME_LEN * sizeof(*vector_ptr)); |
|
|
|
|
memcpy(out, vector_ptr, FRAME_LEN * sizeof(*vector_ptr)); |
|
|
|
|
} else { /* Unvoiced */ |
|
|
|
|
for (i = 0; i < FRAME_LEN; i++) { |
|
|
|
|
*rseed = *rseed * 521 + 259; |
|
|
|
|
out[i] = gain * *rseed >> 15; |
|
|
|
|
} |
|
|
|
|
memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(int16_t)); |
|
|
|
|
memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -889,8 +895,8 @@ static void formant_postfilter(G723_1_Context *p, int16_t *lpc, int16_t *buf) |
|
|
|
|
int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr; |
|
|
|
|
int i, j, k; |
|
|
|
|
|
|
|
|
|
memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(int16_t)); |
|
|
|
|
memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(int)); |
|
|
|
|
memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf)); |
|
|
|
|
memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal)); |
|
|
|
|
|
|
|
|
|
for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) { |
|
|
|
|
for (k = 0; k < LPC_ORDER; k++) { |
|
|
|
@ -977,8 +983,8 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data, |
|
|
|
|
|
|
|
|
|
if (unpack_bitstream(p, buf, buf_size) < 0) { |
|
|
|
|
bad_frame = 1; |
|
|
|
|
p->cur_frame_type = p->past_frame_type == ActiveFrame ? |
|
|
|
|
ActiveFrame : UntransmittedFrame; |
|
|
|
|
p->cur_frame_type = p->past_frame_type == ACTIVE_FRAME ? |
|
|
|
|
ACTIVE_FRAME : UNTRANSMITTED_FRAME; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
p->frame.nb_samples = FRAME_LEN + LPC_ORDER; |
|
|
|
@ -989,20 +995,21 @@ static int g723_1_decode_frame(AVCodecContext *avctx, void *data, |
|
|
|
|
out= (int16_t*)p->frame.data[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(p->cur_frame_type == ActiveFrame) { |
|
|
|
|
if (!bad_frame) { |
|
|
|
|
if(p->cur_frame_type == ACTIVE_FRAME) { |
|
|
|
|
if (!bad_frame) |
|
|
|
|
p->erased_frames = 0; |
|
|
|
|
} else if(p->erased_frames != 3) |
|
|
|
|
else if(p->erased_frames != 3) |
|
|
|
|
p->erased_frames++; |
|
|
|
|
|
|
|
|
|
inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame); |
|
|
|
|
lsp_interpolate(lpc, cur_lsp, p->prev_lsp); |
|
|
|
|
|
|
|
|
|
/* Save the lsp_vector for the next frame */ |
|
|
|
|
memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(int16_t)); |
|
|
|
|
memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp)); |
|
|
|
|
|
|
|
|
|
/* Generate the excitation for the frame */ |
|
|
|
|
memcpy(p->excitation, p->prev_excitation, PITCH_MAX * sizeof(int16_t)); |
|
|
|
|
memcpy(p->excitation, p->prev_excitation, |
|
|
|
|
PITCH_MAX * sizeof(*p->excitation)); |
|
|
|
|
vector_ptr = p->excitation + PITCH_MAX; |
|
|
|
|
if (!p->erased_frames) { |
|
|
|
|
/* Update interpolation gain memory */ |
|
|
|
@ -1118,7 +1125,7 @@ static av_cold int g723_1_encode_init(AVCodecContext *avctx) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (avctx->bit_rate == 6300) { |
|
|
|
|
p->cur_rate = Rate6k3; |
|
|
|
|
p->cur_rate = RATE_6300; |
|
|
|
|
} else if (avctx->bit_rate == 5300) { |
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6.3k\n"); |
|
|
|
|
return AVERROR_PATCHWELCOME; |
|
|
|
@ -2041,7 +2048,7 @@ static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size) |
|
|
|
|
|
|
|
|
|
init_put_bits(&pb, frame, size); |
|
|
|
|
|
|
|
|
|
if (p->cur_rate == Rate6k3) { |
|
|
|
|
if (p->cur_rate == RATE_6300) { |
|
|
|
|
info_bits = 0; |
|
|
|
|
put_bits(&pb, 2, info_bits); |
|
|
|
|
} |
|
|
|
@ -2059,7 +2066,7 @@ static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size) |
|
|
|
|
for (i = 0; i < SUBFRAMES; i++) { |
|
|
|
|
temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS + |
|
|
|
|
p->subframe[i].amp_index; |
|
|
|
|
if (p->cur_rate == Rate6k3) |
|
|
|
|
if (p->cur_rate == RATE_6300) |
|
|
|
|
temp += p->subframe[i].dirac_train << 11; |
|
|
|
|
put_bits(&pb, 12, temp); |
|
|
|
|
} |
|
|
|
@ -2069,7 +2076,7 @@ static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size) |
|
|
|
|
put_bits(&pb, 1, p->subframe[2].grid_index); |
|
|
|
|
put_bits(&pb, 1, p->subframe[3].grid_index); |
|
|
|
|
|
|
|
|
|
if (p->cur_rate == Rate6k3) { |
|
|
|
|
if (p->cur_rate == RATE_6300) { |
|
|
|
|
skip_put_bits(&pb, 1); /* reserved bit */ |
|
|
|
|
|
|
|
|
|
/* Write 13 bit combined position index */ |
|
|
|
@ -2192,7 +2199,7 @@ static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, |
|
|
|
|
|
|
|
|
|
/* Reconstruct the excitation */ |
|
|
|
|
gen_acb_excitation(impulse_resp, p->prev_excitation, p->pitch_lag[i >> 1], |
|
|
|
|
p->subframe[i], Rate6k3); |
|
|
|
|
p->subframe[i], RATE_6300); |
|
|
|
|
|
|
|
|
|
memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN, |
|
|
|
|
sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN)); |
|
|
|
|