annotate qcelpdec.c @ 8150:4da8fc62ae00 libavcodec

Cosmetics
author reynaldo
date Sun, 16 Nov 2008 00:57:06 +0000
parents f27d01aff4af
children e2c068cb210a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
1 /*
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
2 * QCELP decoder
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
3 * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
4 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
5 * This file is part of FFmpeg.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
6 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
11 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
15 * Lesser General Public License for more details.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
16 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
20 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
21
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
22 /**
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
23 * @file qcelpdec.c
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
24 * QCELP decoder
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
25 * @author Reynaldo H. Verdejo Pinochet
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
26 */
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
27
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
28 #include <stddef.h>
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
29
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
30 #include "avcodec.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
31 #include "bitstream.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
32
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
33 #include "qcelp.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
34 #include "qcelpdata.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
35
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
36 #include "celp_math.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
37 #include "celp_filters.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
38
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
39 #undef NDEBUG
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
40 #include <assert.h>
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
41
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
42 static void weighted_vector_sumf(float *out, const float *in_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
43 const float *in_b, float weight_coeff_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
44 float weight_coeff_b, int length)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
45 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
46 int i;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
47
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
48 for(i=0; i<length; i++)
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
49 out[i] = weight_coeff_a * in_a[i]
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
50 + weight_coeff_b * in_b[i];
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
51 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
52
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
53 /**
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
54 * Initialize the speech codec according to the specification.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
55 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
56 * TIA/EIA/IS-733 2.4.9
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
57 */
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
58 static av_cold int qcelp_decode_init(AVCodecContext *avctx) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
59 QCELPContext *q = avctx->priv_data;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
60 int i;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
61
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
62 avctx->sample_fmt = SAMPLE_FMT_FLT;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
63
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
64 for (i = 0; i < 10; i++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
65 q->prev_lspf[i] = (i + 1) / 11.;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
66
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
67 return 0;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
68 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
69
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
70 /**
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
71 * Computes the scaled codebook vector Cdn From INDEX and GAIN
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
72 * for all rates.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
73 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
74 * The specification lacks some information here.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
75 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
76 * TIA/EIA/IS-733 has an omission on the codebook index determination
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
77 * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
78 * you have to subtract the decoded index parameter from the given scaled
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
79 * codebook vector index 'n' to get the desired circular codebook index, but
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
80 * it does not mention that you have to clamp 'n' to [0-9] in order to get
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
81 * RI-compliant results.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
82 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
83 * The reason for this mistake seems to be the fact they forgot to mention you
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
84 * have to do these calculations per codebook subframe and adjust given
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
85 * equation values accordingly.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
86 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
87 * @param q the context
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
88 * @param gain array holding the 4 pitch subframe gain values
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
89 * @param cdn_vector array for the generated scaled codebook vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
90 */
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
91 static void compute_svector(const QCELPContext *q,
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
92 const float *gain,
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
93 float *cdn_vector) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
94 int i, j, k;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
95 uint16_t cbseed, cindex;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
96 float *rnd, tmp_gain, fir_filter_value;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
97
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
98 switch (q->framerate) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
99 case RATE_FULL:
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
100 for (i = 0; i < 16; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
101 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
102 cindex = -q->cindex[i];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
103 for (j = 0; j < 10; j++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
104 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
105 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
106 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
107 case RATE_HALF:
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
108 for (i = 0; i < 4; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
109 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
110 cindex = -q->cindex[i];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
111 for (j = 0; j < 40; j++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
112 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
113 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
114 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
115 case RATE_QUARTER:
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
116 cbseed = (0x0003 & q->lspv[4])<<14 |
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
117 (0x003F & q->lspv[3])<< 8 |
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
118 (0x0060 & q->lspv[2])<< 1 |
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
119 (0x0007 & q->lspv[1])<< 3 |
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
120 (0x0038 & q->lspv[0])>> 3 ;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
121 rnd = q->rnd_fir_filter_mem + 20;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
122 for (i = 0; i < 8; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
123 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
124 for (k = 0; k < 20; k++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
125 cbseed = 521 * cbseed + 259;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
126 *rnd = (int16_t)cbseed;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
127
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
128 // FIR filter
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
129 fir_filter_value = 0.0;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
130 for (j = 0; j < 10; j++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
131 fir_filter_value += qcelp_rnd_fir_coefs[j ] * (rnd[-j ] + rnd[-20+j]);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
132 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
133
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
134 *cdn_vector++ = tmp_gain * fir_filter_value;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
135 rnd++;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
136 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
137 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
138 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
139 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
140 case RATE_OCTAVE:
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
141 cbseed = q->first16bits;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
142 for (i = 0; i < 8; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
143 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
144 for (j = 0; j < 20; j++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
145 cbseed = 521 * cbseed + 259;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
146 *cdn_vector++ = tmp_gain * (int16_t)cbseed;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
147 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
148 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
149 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
150 case I_F_Q:
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
151 cbseed = -44; // random codebook index
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
152 for (i = 0; i < 4; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
153 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
154 for (j = 0; j < 40; j++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
155 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
156 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
157 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
158 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
159 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
160
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
161 /**
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
162 * Apply generic gain control.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
163 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
164 * @param v_out output vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
165 * @param v_in gain-controlled vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
166 * @param v_ref vector to control gain of
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
167 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
168 * FIXME: If v_ref is a zero vector, it energy is zero
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
169 * and the behavior of the gain control is
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
170 * undefined in the specs.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
171 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
172 * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
173 */
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
174 static void apply_gain_ctrl(float *v_out,
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
175 const float *v_ref,
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
176 const float *v_in) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
177 int i, j, len;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
178 float scalefactor;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
179
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
180 for (i = 0, j = 0; i < 4; i++) {
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
181 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
182 if (scalefactor)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
183 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) / scalefactor);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
184 else
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
185 av_log_missing_feature(NULL, "Zero energy for gain control", 1);
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
186 for (len = j + 40; j < len; j++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
187 v_out[j] = scalefactor * v_in[j];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
188 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
189 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
190
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
191 /**
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
192 * Apply filter in pitch-subframe steps.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
193 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
194 * @param memory buffer for the previous state of the filter
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
195 * - must be able to contain 303 elements
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
196 * - the 143 first elements are from the previous state
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
197 * - the next 160 are for output
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
198 * @param v_in input filter vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
199 * @param gain per-subframe gain array, each element is between 0.0 and 2.0
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
200 * @param lag per-subframe lag array, each element is
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
201 * - between 16 and 143 if its corresponding pfrac is 0,
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
202 * - between 16 and 139 otherwise
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
203 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 otherwise
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
204 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
205 * @return filter output vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
206 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
207 static const float *do_pitchfilter(float memory[303], const float v_in[160],
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
208 const float gain[4], const uint8_t *lag,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
209 const uint8_t pfrac[4])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
210 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
211 int i, j;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
212 float *v_lag, *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
213 const float *v_len;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
214
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
215 v_out = memory + 143; // Output vector starts at memory[143].
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
216
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
217 for(i=0; i<4; i++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
218 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
219 if(gain[i])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
220 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
221 v_lag = memory + 143 + 40 * i - lag[i];
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
222 for(v_len=v_in+40; v_in<v_len; v_in++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
223 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
224 if(pfrac[i]) // If it is a fractional lag...
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
225 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
226 for(j=0, *v_out=0.; j<4; j++)
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
227 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
228 }else
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
229 *v_out = *v_lag;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
230
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
231 *v_out = *v_in + gain[i] * *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
232
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
233 v_lag++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
234 v_out++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
235 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
236 }else
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
237 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
238 memcpy(v_out, v_in, 40 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
239 v_in += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
240 v_out += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
241 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
242 }
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
243
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
244 memmove(memory, memory + 160, 143 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
245 return memory + 143;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
246 }
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
247
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
248 /**
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
249 * Interpolates LSP frequencies and computes LPC coefficients
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
250 * for a given framerate & pitch subframe.
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
251 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
252 * TIA/EIA/IS-733 2.4.3.3.4
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
253 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
254 * @param q the context
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
255 * @param curr_lspf LSP frequencies vector of the current frame
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
256 * @param lpc float vector for the resulting LPC
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
257 * @param subframe_num frame number in decoded stream
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
258 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
259 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
260 const int subframe_num)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
261 {
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
262 float interpolated_lspf[10];
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
263 float weight;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
264
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
265 if(q->framerate >= RATE_QUARTER)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
266 weight = 0.25 * (subframe_num + 1);
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
267 else if(q->framerate == RATE_OCTAVE && !subframe_num)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
268 weight = 0.625;
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
269 else
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
270 weight = 1.0;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
271
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
272 if(weight != 1.0)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
273 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
274 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
275 weight, 1.0 - weight, 10);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
276 qcelp_lspf2lpc(interpolated_lspf, lpc);
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
277 }else if(q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num))
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
278 qcelp_lspf2lpc(curr_lspf, lpc);
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
279 }
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
280
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
281 static int buf_size2framerate(const int buf_size)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
282 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
283 switch(buf_size)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
284 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
285 case 35:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
286 return RATE_FULL;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
287 case 17:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
288 return RATE_HALF;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
289 case 8:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
290 return RATE_QUARTER;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
291 case 4:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
292 return RATE_OCTAVE;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
293 case 1:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
294 return SILENCE;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
295 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
296
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
297 return -1;
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
298 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
299
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
300 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
301 const char *message)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
302 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
303 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
304 message);
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
305 }
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
306
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
307 AVCodec qcelp_decoder =
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
308 {
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
309 .name = "qcelp",
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
310 .type = CODEC_TYPE_AUDIO,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
311 .id = CODEC_ID_QCELP,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
312 .init = qcelp_decode_init,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
313 .decode = qcelp_decode_frame,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
314 .priv_data_size = sizeof(QCELPContext),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
315 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
316 };