Libav 0.7.1
|
00001 /* 00002 * gsm 06.10 decoder 00003 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00027 #include "get_bits.h" 00028 #include "gsmdec_data.h" 00029 00030 static void apcm_dequant_add(GetBitContext *gb, int16_t *dst) 00031 { 00032 int i; 00033 int maxidx = get_bits(gb, 6); 00034 const int16_t *tab = ff_gsm_dequant_tab[maxidx]; 00035 for (i = 0; i < 13; i++) 00036 dst[3*i] += tab[get_bits(gb, 3)]; 00037 } 00038 00039 static inline int gsm_mult(int a, int b) 00040 { 00041 return (a * b + (1 << 14)) >> 15; 00042 } 00043 00044 static void long_term_synth(int16_t *dst, int lag, int gain_idx) 00045 { 00046 int i; 00047 const int16_t *src = dst - lag; 00048 uint16_t gain = ff_gsm_long_term_gain_tab[gain_idx]; 00049 for (i = 0; i < 40; i++) 00050 dst[i] = gsm_mult(gain, src[i]); 00051 } 00052 00053 static inline int decode_log_area(int coded, int factor, int offset) 00054 { 00055 coded <<= 10; 00056 coded -= offset; 00057 return gsm_mult(coded, factor) << 1; 00058 } 00059 00060 static av_noinline int get_rrp(int filtered) 00061 { 00062 int abs = FFABS(filtered); 00063 if (abs < 11059) abs <<= 1; 00064 else if (abs < 20070) abs += 11059; 00065 else abs = (abs >> 2) + 26112; 00066 return filtered < 0 ? -abs : abs; 00067 } 00068 00069 static int filter_value(int in, int rrp[8], int v[9]) 00070 { 00071 int i; 00072 for (i = 7; i >= 0; i--) { 00073 in -= gsm_mult(rrp[i], v[i]); 00074 v[i + 1] = v[i] + gsm_mult(rrp[i], in); 00075 } 00076 v[0] = in; 00077 return in; 00078 } 00079 00080 static void short_term_synth(GSMContext *ctx, int16_t *dst, const int16_t *src) 00081 { 00082 int i; 00083 int rrp[8]; 00084 int *lar = ctx->lar[ctx->lar_idx]; 00085 int *lar_prev = ctx->lar[ctx->lar_idx ^ 1]; 00086 for (i = 0; i < 8; i++) 00087 rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar_prev[i] >> 1) + (lar[i] >> 2)); 00088 for (i = 0; i < 13; i++) 00089 dst[i] = filter_value(src[i], rrp, ctx->v); 00090 00091 for (i = 0; i < 8; i++) 00092 rrp[i] = get_rrp((lar_prev[i] >> 1) + (lar [i] >> 1)); 00093 for (i = 13; i < 27; i++) 00094 dst[i] = filter_value(src[i], rrp, ctx->v); 00095 00096 for (i = 0; i < 8; i++) 00097 rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar [i] >> 1) + (lar[i] >> 2)); 00098 for (i = 27; i < 40; i++) 00099 dst[i] = filter_value(src[i], rrp, ctx->v); 00100 00101 for (i = 0; i < 8; i++) 00102 rrp[i] = get_rrp(lar[i]); 00103 for (i = 40; i < 160; i++) 00104 dst[i] = filter_value(src[i], rrp, ctx->v); 00105 00106 ctx->lar_idx ^= 1; 00107 } 00108 00109 static int postprocess(int16_t *data, int msr) 00110 { 00111 int i; 00112 for (i = 0; i < 160; i++) { 00113 msr = av_clip_int16(data[i] + gsm_mult(msr, 28180)); 00114 data[i] = av_clip_int16(msr << 1) & ~7; 00115 } 00116 return msr; 00117 } 00118 00119 static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples, 00120 GetBitContext *gb) 00121 { 00122 GSMContext *ctx = avctx->priv_data; 00123 int i; 00124 int16_t *ref_dst = ctx->ref_buf + 120; 00125 int *lar = ctx->lar[ctx->lar_idx]; 00126 lar[0] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); 00127 lar[1] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15); 00128 lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2); 00129 lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2); 00130 lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) + 94*2); 00131 lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2); 00132 lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) - 341*2); 00133 lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2); 00134 00135 for (i = 0; i < 4; i++) { 00136 int lag = get_bits(gb, 7); 00137 int gain_idx = get_bits(gb, 2); 00138 int offset = get_bits(gb, 2); 00139 lag = av_clip(lag, 40, 120); 00140 long_term_synth(ref_dst, lag, gain_idx); 00141 apcm_dequant_add(gb, ref_dst + offset); 00142 ref_dst += 40; 00143 } 00144 memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf)); 00145 short_term_synth(ctx, samples, ctx->ref_buf + 120); 00146 // for optimal speed this could be merged with short_term_synth, 00147 // not done yet because it is a bit ugly 00148 ctx->msr = postprocess(samples, ctx->msr); 00149 return 0; 00150 }