00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022 #include <inttypes.h>
00023 #include <limits.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <math.h>
00027 #include <assert.h>
00028
00029 #include "avcodec.h"
00030 #include "libavutil/avutil.h"
00031 #include "get_bits.h"
00032
00033 #include "lsp.h"
00034 #include "celp_math.h"
00035 #include "acelp_filters.h"
00036 #include "acelp_pitch_delay.h"
00037 #include "acelp_vectors.h"
00038 #include "g729data.h"
00039
00044 #define LSFQ_MIN 40
00045
00050 #define LSFQ_MAX 25681
00051
00056 #define LSFQ_DIFF_MIN 321
00057
00062 #define SHARP_MIN 3277
00063
00071 #define SHARP_MAX 13017
00072
00076 #define SUBFRAME_SIZE 40
00077
00078
00079 typedef struct {
00080 uint8_t ac_index_bits[2];
00081 uint8_t parity_bit;
00082 uint8_t gc_1st_index_bits;
00083 uint8_t gc_2nd_index_bits;
00084 uint8_t fc_signs_bits;
00085 uint8_t fc_indexes_bits;
00086 } G729FormatDescription;
00087
00088 typedef struct {
00089 int pitch_delay_int_prev;
00090
00092 int16_t past_quantizer_output_buf[MA_NP + 1][10];
00093 int16_t* past_quantizer_outputs[MA_NP + 1];
00094
00095 int16_t lsfq[10];
00096 int16_t lsp_buf[2][10];
00097 int16_t *lsp[2];
00098 } G729Context;
00099
00100 static const G729FormatDescription format_g729_8k = {
00101 .ac_index_bits = {8,5},
00102 .parity_bit = 1,
00103 .gc_1st_index_bits = GC_1ST_IDX_BITS_8K,
00104 .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K,
00105 .fc_signs_bits = 4,
00106 .fc_indexes_bits = 13,
00107 };
00108
00109 static const G729FormatDescription format_g729d_6k4 = {
00110 .ac_index_bits = {8,4},
00111 .parity_bit = 0,
00112 .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4,
00113 .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4,
00114 .fc_signs_bits = 2,
00115 .fc_indexes_bits = 9,
00116 };
00117
00121 static inline uint16_t g729_prng(uint16_t value)
00122 {
00123 return 31821 * value + 13849;
00124 }
00125
00129 static inline int get_parity(uint8_t value)
00130 {
00131 return (0x6996966996696996ULL >> (value >> 2)) & 1;
00132 }
00133
00134 static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1],
00135 int16_t ma_predictor,
00136 int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high)
00137 {
00138 int i,j;
00139 static const uint8_t min_distance[2]={10, 5};
00140 int16_t* quantizer_output = past_quantizer_outputs[MA_NP];
00141
00142 for (i = 0; i < 5; i++) {
00143 quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ];
00144 quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5];
00145 }
00146
00147 for (j = 0; j < 2; j++) {
00148 for (i = 1; i < 10; i++) {
00149 int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1;
00150 if (diff > 0) {
00151 quantizer_output[i - 1] -= diff;
00152 quantizer_output[i ] += diff;
00153 }
00154 }
00155 }
00156
00157 for (i = 0; i < 10; i++) {
00158 int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i];
00159 for (j = 0; j < MA_NP; j++)
00160 sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i];
00161
00162 lsfq[i] = sum >> 15;
00163 }
00164
00165
00166 memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*));
00167 past_quantizer_outputs[0] = quantizer_output;
00168
00169 ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10);
00170 }
00171
00172 static av_cold int decoder_init(AVCodecContext * avctx)
00173 {
00174 G729Context* ctx = avctx->priv_data;
00175 int i,k;
00176
00177 if (avctx->channels != 1) {
00178 av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels);
00179 return AVERROR(EINVAL);
00180 }
00181
00182
00183 avctx->frame_size = SUBFRAME_SIZE << 1;
00184
00185 for (k = 0; k < MA_NP + 1; k++) {
00186 ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k];
00187 for (i = 1; i < 11; i++)
00188 ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3;
00189 }
00190
00191 ctx->lsp[0] = ctx->lsp_buf[0];
00192 ctx->lsp[1] = ctx->lsp_buf[1];
00193 memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t));
00194
00195 return 0;
00196 }
00197
00198 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00199 AVPacket *avpkt)
00200 {
00201 const uint8_t *buf = avpkt->data;
00202 int buf_size = avpkt->size;
00203 int16_t *out_frame = data;
00204 GetBitContext gb;
00205 G729FormatDescription format;
00206 int frame_erasure = 0;
00207 int bad_pitch = 0;
00208 int i;
00209 G729Context *ctx = avctx->priv_data;
00210 int16_t lp[2][11];
00211 uint8_t ma_predictor;
00212 uint8_t quantizer_1st;
00213 uint8_t quantizer_2nd_lo;
00214 uint8_t quantizer_2nd_hi;
00215
00216 int pitch_delay_int;
00217 int pitch_delay_3x;
00218
00219 if (*data_size < SUBFRAME_SIZE << 2) {
00220 av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n");
00221 return AVERROR(EIO);
00222 }
00223
00224 if (buf_size == 10) {
00225 format = format_g729_8k;
00226 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s");
00227 } else if (buf_size == 8) {
00228 format = format_g729d_6k4;
00229 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s");
00230 } else {
00231 av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size);
00232 return AVERROR_INVALIDDATA;
00233 }
00234
00235 for (i=0; i < buf_size; i++)
00236 frame_erasure |= buf[i];
00237 frame_erasure = !frame_erasure;
00238
00239 init_get_bits(&gb, buf, buf_size);
00240
00241 ma_predictor = get_bits(&gb, 1);
00242 quantizer_1st = get_bits(&gb, VQ_1ST_BITS);
00243 quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS);
00244 quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS);
00245
00246 lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs,
00247 ma_predictor,
00248 quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi);
00249
00250 ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10);
00251
00252 ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10);
00253
00254 FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]);
00255
00256 for (i = 0; i < 2; i++) {
00257 uint8_t ac_index;
00258 uint8_t pulses_signs;
00259 int fc_indexes;
00260 uint8_t gc_1st_index;
00261 uint8_t gc_2nd_index;
00262
00263 ac_index = get_bits(&gb, format.ac_index_bits[i]);
00264 if(!i && format.parity_bit)
00265 bad_pitch = get_parity(ac_index) == get_bits1(&gb);
00266 fc_indexes = get_bits(&gb, format.fc_indexes_bits);
00267 pulses_signs = get_bits(&gb, format.fc_signs_bits);
00268 gc_1st_index = get_bits(&gb, format.gc_1st_index_bits);
00269 gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits);
00270
00271 if(!i) {
00272 if (bad_pitch)
00273 pitch_delay_3x = 3 * ctx->pitch_delay_int_prev;
00274 else
00275 pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index);
00276 } else {
00277 int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5,
00278 PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9);
00279
00280 if(packet_type == FORMAT_G729D_6K4)
00281 pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min);
00282 else
00283 pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min);
00284 }
00285
00286
00287 pitch_delay_int = (pitch_delay_3x + 1) / 3;
00288
00289 ff_acelp_weighted_vector_sum(fc + pitch_delay_int,
00290 fc + pitch_delay_int,
00291 fc, 1 << 14,
00292 av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX),
00293 0, 14,
00294 SUBFRAME_SIZE - pitch_delay_int);
00295
00296 if (frame_erasure) {
00297 ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15;
00298 ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11;
00299
00300 gain_corr_factor = 0;
00301 } else {
00302 ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] +
00303 cb_gain_2nd_8k[gc_2nd_index][0];
00304 gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] +
00305 cb_gain_2nd_8k[gc_2nd_index][1];
00306
00307 ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE,
00308 ctx->exc + i * SUBFRAME_SIZE, fc,
00309 (!voicing && frame_erasure) ? 0 : ctx->gain_pitch,
00310 ( voicing && frame_erasure) ? 0 : ctx->gain_code,
00311 1 << 13, 14, SUBFRAME_SIZE);
00312
00313 ctx->pitch_delay_int_prev = pitch_delay_int;
00314 }
00315
00316 *data_size = SUBFRAME_SIZE << 2;
00317 return buf_size;
00318 }
00319
00320 AVCodec ff_g729_decoder =
00321 {
00322 "g729",
00323 AVMEDIA_TYPE_AUDIO,
00324 CODEC_ID_G729,
00325 sizeof(G729Context),
00326 decoder_init,
00327 NULL,
00328 NULL,
00329 decode_frame,
00330 .long_name = NULL_IF_CONFIG_SMALL("G.729"),
00331 };