Libav
|
00001 /* 00002 * G.729 decoder 00003 * Copyright (c) 2008 Vladimir Voroshilov 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 "g729.h" 00034 #include "lsp.h" 00035 #include "celp_math.h" 00036 #include "acelp_filters.h" 00037 #include "acelp_pitch_delay.h" 00038 #include "acelp_vectors.h" 00039 #include "g729data.h" 00040 00045 #define LSFQ_MIN 40 00046 00051 #define LSFQ_MAX 25681 00052 00057 #define LSFQ_DIFF_MIN 321 00058 00063 #define SHARP_MIN 3277 00064 00072 #define SHARP_MAX 13017 00073 00074 typedef struct { 00075 uint8_t ac_index_bits[2]; 00076 uint8_t parity_bit; 00077 uint8_t gc_1st_index_bits; 00078 uint8_t gc_2nd_index_bits; 00079 uint8_t fc_signs_bits; 00080 uint8_t fc_indexes_bits; 00081 } G729FormatDescription; 00082 00083 typedef struct { 00084 int pitch_delay_int_prev; 00085 00087 int16_t past_quantizer_output_buf[MA_NP + 1][10]; 00088 int16_t* past_quantizer_outputs[MA_NP + 1]; 00089 00090 int16_t lsfq[10]; 00091 int16_t lsp_buf[2][10]; 00092 int16_t *lsp[2]; 00093 } G729Context; 00094 00095 static const G729FormatDescription format_g729_8k = { 00096 .ac_index_bits = {8,5}, 00097 .parity_bit = 1, 00098 .gc_1st_index_bits = GC_1ST_IDX_BITS_8K, 00099 .gc_2nd_index_bits = GC_2ND_IDX_BITS_8K, 00100 .fc_signs_bits = 4, 00101 .fc_indexes_bits = 13, 00102 }; 00103 00104 static const G729FormatDescription format_g729d_6k4 = { 00105 .ac_index_bits = {8,4}, 00106 .parity_bit = 0, 00107 .gc_1st_index_bits = GC_1ST_IDX_BITS_6K4, 00108 .gc_2nd_index_bits = GC_2ND_IDX_BITS_6K4, 00109 .fc_signs_bits = 2, 00110 .fc_indexes_bits = 9, 00111 }; 00112 00116 static inline uint16_t g729_prng(uint16_t value) 00117 { 00118 return 31821 * value + 13849; 00119 } 00120 00124 static inline int get_parity(uint8_t value) 00125 { 00126 return (0x6996966996696996ULL >> (value >> 2)) & 1; 00127 } 00128 00129 static void lsf_decode(int16_t* lsfq, int16_t* past_quantizer_outputs[MA_NP + 1], 00130 int16_t ma_predictor, 00131 int16_t vq_1st, int16_t vq_2nd_low, int16_t vq_2nd_high) 00132 { 00133 int i,j; 00134 static const uint8_t min_distance[2]={10, 5}; //(2.13) 00135 int16_t* quantizer_output = past_quantizer_outputs[MA_NP]; 00136 00137 for (i = 0; i < 5; i++) { 00138 quantizer_output[i] = cb_lsp_1st[vq_1st][i ] + cb_lsp_2nd[vq_2nd_low ][i ]; 00139 quantizer_output[i + 5] = cb_lsp_1st[vq_1st][i + 5] + cb_lsp_2nd[vq_2nd_high][i + 5]; 00140 } 00141 00142 for (j = 0; j < 2; j++) { 00143 for (i = 1; i < 10; i++) { 00144 int diff = (quantizer_output[i - 1] - quantizer_output[i] + min_distance[j]) >> 1; 00145 if (diff > 0) { 00146 quantizer_output[i - 1] -= diff; 00147 quantizer_output[i ] += diff; 00148 } 00149 } 00150 } 00151 00152 for (i = 0; i < 10; i++) { 00153 int sum = quantizer_output[i] * cb_ma_predictor_sum[ma_predictor][i]; 00154 for (j = 0; j < MA_NP; j++) 00155 sum += past_quantizer_outputs[j][i] * cb_ma_predictor[ma_predictor][j][i]; 00156 00157 lsfq[i] = sum >> 15; 00158 } 00159 00160 /* Rotate past_quantizer_outputs. */ 00161 memmove(past_quantizer_outputs + 1, past_quantizer_outputs, MA_NP * sizeof(int16_t*)); 00162 past_quantizer_outputs[0] = quantizer_output; 00163 00164 ff_acelp_reorder_lsf(lsfq, LSFQ_DIFF_MIN, LSFQ_MIN, LSFQ_MAX, 10); 00165 } 00166 00167 static av_cold int decoder_init(AVCodecContext * avctx) 00168 { 00169 G729Context* ctx = avctx->priv_data; 00170 int i,k; 00171 00172 if (avctx->channels != 1) { 00173 av_log(avctx, AV_LOG_ERROR, "Only mono sound is supported (requested channels: %d).\n", avctx->channels); 00174 return AVERROR(EINVAL); 00175 } 00176 00177 /* Both 8kbit/s and 6.4kbit/s modes uses two subframes per frame. */ 00178 avctx->frame_size = SUBFRAME_SIZE << 1; 00179 00180 for (k = 0; k < MA_NP + 1; k++) { 00181 ctx->past_quantizer_outputs[k] = ctx->past_quantizer_output_buf[k]; 00182 for (i = 1; i < 11; i++) 00183 ctx->past_quantizer_outputs[k][i - 1] = (18717 * i) >> 3; 00184 } 00185 00186 ctx->lsp[0] = ctx->lsp_buf[0]; 00187 ctx->lsp[1] = ctx->lsp_buf[1]; 00188 memcpy(ctx->lsp[0], lsp_init, 10 * sizeof(int16_t)); 00189 00190 return 0; 00191 } 00192 00193 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, 00194 AVPacket *avpkt) 00195 { 00196 const uint8_t *buf = avpkt->data; 00197 int buf_size = avpkt->size; 00198 int16_t *out_frame = data; 00199 GetBitContext gb; 00200 G729FormatDescription format; 00201 int frame_erasure = 0; 00202 int bad_pitch = 0; 00203 int i; 00204 G729Context *ctx = avctx->priv_data; 00205 int16_t lp[2][11]; // (3.12) 00206 uint8_t ma_predictor; 00207 uint8_t quantizer_1st; 00208 uint8_t quantizer_2nd_lo; 00209 uint8_t quantizer_2nd_hi; 00210 00211 int pitch_delay_int; // pitch delay, integer part 00212 int pitch_delay_3x; // pitch delay, multiplied by 3 00213 00214 if (*data_size < SUBFRAME_SIZE << 2) { 00215 av_log(avctx, AV_LOG_ERROR, "Error processing packet: output buffer too small\n"); 00216 return AVERROR(EIO); 00217 } 00218 00219 if (buf_size == 10) { 00220 format = format_g729_8k; 00221 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729 @ 8kbit/s"); 00222 } else if (buf_size == 8) { 00223 format = format_g729d_6k4; 00224 av_log(avctx, AV_LOG_DEBUG, "Packet type: %s\n", "G.729D @ 6.4kbit/s"); 00225 } else { 00226 av_log(avctx, AV_LOG_ERROR, "Packet size %d is unknown.\n", buf_size); 00227 return AVERROR_INVALIDDATA; 00228 } 00229 00230 for (i=0; i < buf_size; i++) 00231 frame_erasure |= buf[i]; 00232 frame_erasure = !frame_erasure; 00233 00234 init_get_bits(&gb, buf, buf_size); 00235 00236 ma_predictor = get_bits(&gb, 1); 00237 quantizer_1st = get_bits(&gb, VQ_1ST_BITS); 00238 quantizer_2nd_lo = get_bits(&gb, VQ_2ND_BITS); 00239 quantizer_2nd_hi = get_bits(&gb, VQ_2ND_BITS); 00240 00241 lsf_decode(ctx->lsfq, ctx->past_quantizer_outputs, 00242 ma_predictor, 00243 quantizer_1st, quantizer_2nd_lo, quantizer_2nd_hi); 00244 00245 ff_acelp_lsf2lsp(ctx->lsp[1], ctx->lsfq, 10); 00246 00247 ff_acelp_lp_decode(&lp[0][0], &lp[1][0], ctx->lsp[1], ctx->lsp[0], 10); 00248 00249 FFSWAP(int16_t*, ctx->lsp[1], ctx->lsp[0]); 00250 00251 for (i = 0; i < 2; i++) { 00252 uint8_t ac_index; 00253 uint8_t pulses_signs; 00254 int fc_indexes; 00255 uint8_t gc_1st_index; 00256 uint8_t gc_2nd_index; 00257 00258 ac_index = get_bits(&gb, format.ac_index_bits[i]); 00259 if(!i && format.parity_bit) 00260 bad_pitch = get_parity(ac_index) == get_bits1(&gb); 00261 fc_indexes = get_bits(&gb, format.fc_indexes_bits); 00262 pulses_signs = get_bits(&gb, format.fc_signs_bits); 00263 gc_1st_index = get_bits(&gb, format.gc_1st_index_bits); 00264 gc_2nd_index = get_bits(&gb, format.gc_2nd_index_bits); 00265 00266 if(!i) { 00267 if (bad_pitch) 00268 pitch_delay_3x = 3 * ctx->pitch_delay_int_prev; 00269 else 00270 pitch_delay_3x = ff_acelp_decode_8bit_to_1st_delay3(ac_index); 00271 } else { 00272 int pitch_delay_min = av_clip(ctx->pitch_delay_int_prev - 5, 00273 PITCH_DELAY_MIN, PITCH_DELAY_MAX - 9); 00274 00275 if(packet_type == FORMAT_G729D_6K4) 00276 pitch_delay_3x = ff_acelp_decode_4bit_to_2nd_delay3(ac_index, pitch_delay_min); 00277 else 00278 pitch_delay_3x = ff_acelp_decode_5_6_bit_to_2nd_delay3(ac_index, pitch_delay_min); 00279 } 00280 00281 /* Round pitch delay to nearest (used everywhere except ff_acelp_interpolate). */ 00282 pitch_delay_int = (pitch_delay_3x + 1) / 3; 00283 00284 ff_acelp_weighted_vector_sum(fc + pitch_delay_int, 00285 fc + pitch_delay_int, 00286 fc, 1 << 14, 00287 av_clip(ctx->gain_pitch, SHARP_MIN, SHARP_MAX), 00288 0, 14, 00289 SUBFRAME_SIZE - pitch_delay_int); 00290 00291 if (frame_erasure) { 00292 ctx->gain_pitch = (29491 * ctx->gain_pitch) >> 15; // 0.90 (0.15) 00293 ctx->gain_code = ( 2007 * ctx->gain_code ) >> 11; // 0.98 (0.11) 00294 00295 gain_corr_factor = 0; 00296 } else { 00297 ctx->gain_pitch = cb_gain_1st_8k[gc_1st_index][0] + 00298 cb_gain_2nd_8k[gc_2nd_index][0]; 00299 gain_corr_factor = cb_gain_1st_8k[gc_1st_index][1] + 00300 cb_gain_2nd_8k[gc_2nd_index][1]; 00301 00302 ff_acelp_weighted_vector_sum(ctx->exc + i * SUBFRAME_SIZE, 00303 ctx->exc + i * SUBFRAME_SIZE, fc, 00304 (!voicing && frame_erasure) ? 0 : ctx->gain_pitch, 00305 ( voicing && frame_erasure) ? 0 : ctx->gain_code, 00306 1 << 13, 14, SUBFRAME_SIZE); 00307 00308 ctx->pitch_delay_int_prev = pitch_delay_int; 00309 } 00310 00311 *data_size = SUBFRAME_SIZE << 2; 00312 return buf_size; 00313 } 00314 00315 AVCodec g729_decoder = 00316 { 00317 "g729", 00318 AVMEDIA_TYPE_AUDIO, 00319 CODEC_ID_G729, 00320 sizeof(G729Context), 00321 decoder_init, 00322 NULL, 00323 NULL, 00324 decode_frame, 00325 .long_name = NULL_IF_CONFIG_SMALL("G.729"), 00326 };