annotate qcelpdec.c @ 8233:ababfa151ced libavcodec

enable RV40 decoder
author kostya
date Mon, 01 Dec 2008 06:40:36 +0000
parents 72949bacc1b9
children 93ee77578391
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
8151
e2c068cb210a Credit Kenan Gillet for his contributions towards merging the SoC QCELP decoder.
reynaldo
parents: 8150
diff changeset
26 * @remark FFmpeg merging spearheaded by Kenan Gillet
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
27 */
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
28
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
29 #include <stddef.h>
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
30
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
31 #include "avcodec.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
32 #include "bitstream.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
33
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
34 #include "qcelp.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
35 #include "qcelpdata.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
36
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
37 #include "celp_math.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
38 #include "celp_filters.h"
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
39
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
40 #undef NDEBUG
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
41 #include <assert.h>
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
42
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
43 typedef struct {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
44 GetBitContext gb;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
45 qcelp_packet_rate bitrate;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
46 QCELPFrame frame; /*!< unpacked data frame */
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
47 uint8_t erasure_count;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
48 uint8_t octave_count; /*!< count the consecutive RATE_OCTAVE frames */
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
49 float prev_lspf[10];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
50 float predictor_lspf[10]; /*!< LSP predictor,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
51 only use for RATE_OCTAVE and I_F_Q */
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
52 float formant_mem[170];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
53 float last_codebook_gain;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
54 int prev_g1[2];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
55 int prev_bitrate;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
56 float prev_pitch_gain[4];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
57 uint8_t prev_pitch_lag[4];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
58 uint16_t first16bits;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
59 } QCELPContext;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
60
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
61 static void weighted_vector_sumf(float *out, const float *in_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
62 const float *in_b, float weight_coeff_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
63 float weight_coeff_b, int length)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
64 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
65 int i;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
66
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
67 for(i=0; i<length; i++)
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
68 out[i] = weight_coeff_a * in_a[i]
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
69 + weight_coeff_b * in_b[i];
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
70 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
71
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
72 /**
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
73 * Initialize the speech codec according to the specification.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
74 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
75 * TIA/EIA/IS-733 2.4.9
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
76 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
77 static av_cold int qcelp_decode_init(AVCodecContext *avctx)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
78 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
79 QCELPContext *q = avctx->priv_data;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
80 int i;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
81
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
82 avctx->sample_fmt = SAMPLE_FMT_FLT;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
83
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
84 for (i = 0; i < 10; i++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
85 q->prev_lspf[i] = (i + 1) / 11.;
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 return 0;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
88 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
89
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
90 /**
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
91 * Decodes the 10 quantized LSP frequencies from the LSPV/LSP
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
92 * transmission codes of any bitrate and checks for badly received packets.
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
93 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
94 * @param q the context
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
95 * @param lspf line spectral pair frequencies
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
96 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
97 * @return 0 on success, -1 if the packet is badly received
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
98 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
99 * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
100 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
101 static int decode_lspf(QCELPContext *q, float *lspf)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
102 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
103 int i;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
104 float tmp_lspf;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
105
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
106 if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
107 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
108 float smooth;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
109 const float *predictors = (q->prev_bitrate != RATE_OCTAVE &&
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
110 q->prev_bitrate != I_F_Q ? q->prev_lspf
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
111 : q->predictor_lspf);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
112
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
113 if(q->bitrate == RATE_OCTAVE)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
114 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
115 q->octave_count++;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
116
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
117 for(i=0; i<10; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
118 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
119 q->predictor_lspf[i] =
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
120 lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
121 : -QCELP_LSP_SPREAD_FACTOR)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
122 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
123 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
124 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
125 smooth = (q->octave_count < 10 ? .875 : 0.1);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
126 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
127 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
128 float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
129
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
130 assert(q->bitrate == I_F_Q);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
131
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
132 if(q->erasure_count > 1)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
133 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
134
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
135 for(i=0; i<10; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
136 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
137 q->predictor_lspf[i] =
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
138 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
139 + erasure_coeff * predictors[i];
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
140 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
141 smooth = 0.125;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
142 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
143
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
144 // Check the stability of the LSP frequencies.
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
145 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
146 for(i=1; i<10; i++)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
147 lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR));
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
148
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
149 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
150 for(i=9; i>0; i--)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
151 lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR));
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
152
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
153 // Low-pass filter the LSP frequencies.
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
154 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
155 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
156 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
157 q->octave_count = 0;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
158
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
159 tmp_lspf = 0.;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
160 for(i=0; i<5 ; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
161 {
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
162 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
163 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
164 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
165
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
166 // Check for badly received packets.
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
167 if(q->bitrate == RATE_QUARTER)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
168 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
169 if(lspf[9] <= .70 || lspf[9] >= .97)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
170 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
171 for(i=3; i<10; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
172 if(fabs(lspf[i] - lspf[i-2]) < .08)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
173 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
174 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
175 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
176 if(lspf[9] <= .66 || lspf[9] >= .985)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
177 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
178 for(i=4; i<10; i++)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
179 if (fabs(lspf[i] - lspf[i-4]) < .0931)
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
180 return -1;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
181 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
182 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
183 return 0;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
184 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
185
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
186 /**
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
187 * Converts codebook transmission codes to GAIN and INDEX.
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
188 *
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
189 * @param q the context
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
190 * @param gain array holding the decoded gain
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
191 *
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
192 * TIA/EIA/IS-733 2.4.6.2
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
193 */
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
194 static void decode_gain_and_index(QCELPContext *q,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
195 float *gain) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
196 int i, subframes_count, g1[16];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
197 float slope;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
198
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
199 if (q->bitrate >= RATE_QUARTER) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
200 switch (q->bitrate) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
201 case RATE_FULL: subframes_count = 16; break;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
202 case RATE_HALF: subframes_count = 4; break;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
203 default: subframes_count = 5;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
204 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
205 for (i = 0; i < subframes_count; i++) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
206 g1[i] = 4 * q->frame.cbgain[i];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
207 if (q->bitrate == RATE_FULL && !((i+1) & 3)) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
208 g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
209 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
210
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
211 gain[i] = qcelp_g12ga[g1[i]];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
212
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
213 if (q->frame.cbsign[i]) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
214 gain[i] = -gain[i];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
215 q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
216 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
217 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
218
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
219 q->prev_g1[0] = g1[i-2];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
220 q->prev_g1[1] = g1[i-1];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
221 q->last_codebook_gain = qcelp_g12ga[g1[i-1]];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
222
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
223 if (q->bitrate == RATE_QUARTER) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
224 // Provide smoothing of the unvoiced excitation energy.
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
225 gain[7] = gain[4];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
226 gain[6] = 0.4*gain[3] + 0.6*gain[4];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
227 gain[5] = gain[3];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
228 gain[4] = 0.8*gain[2] + 0.2*gain[3];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
229 gain[3] = 0.2*gain[1] + 0.8*gain[2];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
230 gain[2] = gain[1];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
231 gain[1] = 0.6*gain[0] + 0.4*gain[1];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
232 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
233 } else {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
234 if (q->bitrate == RATE_OCTAVE) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
235 g1[0] = 2 * q->frame.cbgain[0]
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
236 + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
237 subframes_count = 8;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
238 } else {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
239 assert(q->bitrate == I_F_Q);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
240
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
241 g1[0] = q->prev_g1[1];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
242 switch (q->erasure_count) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
243 case 1 : break;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
244 case 2 : g1[0] -= 1; break;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
245 case 3 : g1[0] -= 2; break;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
246 default: g1[0] -= 6;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
247 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
248 if (g1[0] < 0)
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
249 g1[0] = 0;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
250 subframes_count = 4;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
251 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
252 // This interpolation is done to produce smoother background noise.
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
253 slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
254 for (i = 1; i <= subframes_count; i++)
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
255 gain[i-1] = q->last_codebook_gain + slope * i;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
256 q->last_codebook_gain = gain[i-2];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
257
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
258 q->prev_g1[0] = q->prev_g1[1];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
259 q->prev_g1[1] = g1[0];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
260 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
261 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
262
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
263 /**
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
264 * If the received packet is Rate 1/4 a further sanity check is made of the
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
265 * codebook gain.
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
266 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
267 * @param cbgain the unpacked cbgain array
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
268 * @return -1 if the sanity check fails, 0 otherwise
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
269 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
270 * TIA/EIA/IS-733 2.4.8.7.3
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
271 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
272 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
273 {
8193
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
274 int i, prev_diff=0;
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
275
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
276 for(i=1; i<5; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
277 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
278 int diff = cbgain[i] - cbgain[i-1];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
279 if(FFABS(diff) > 10)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
280 return -1;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
281 else if(FFABS(diff - prev_diff) > 12)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
282 return -1;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
283 prev_diff = diff;
8193
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
284 }
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
285 return 0;
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
286 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
287
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
288 /**
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
289 * Computes the scaled codebook vector Cdn From INDEX and GAIN
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
290 * for all rates.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
291 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
292 * The specification lacks some information here.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
293 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
294 * 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
295 * 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
296 * 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
297 * 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
298 * 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
299 * RI-compliant results.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
300 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
301 * 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
302 * have to do these calculations per codebook subframe and adjust given
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
303 * equation values accordingly.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
304 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
305 * @param q the context
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
306 * @param gain array holding the 4 pitch subframe gain values
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
307 * @param cdn_vector array for the generated scaled codebook vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
308 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
309 static void compute_svector(const QCELPContext *q, const float *gain,
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
310 float *cdn_vector)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
311 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
312 int i, j, k;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
313 uint16_t cbseed, cindex;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
314 float *rnd, tmp_gain, fir_filter_value;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
315
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
316 switch(q->bitrate)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
317 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
318 case RATE_FULL:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
319 for(i=0; i<16; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
320 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
321 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
322 cindex = -q->frame.cindex[i];
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
323 for(j=0; j<10; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
324 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
325 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
326 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
327 case RATE_HALF:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
328 for(i=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
329 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
330 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
331 cindex = -q->frame.cindex[i];
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
332 for (j = 0; j < 40; j++)
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
333 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
334 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
335 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
336 case RATE_QUARTER:
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
337 cbseed = (0x0003 & q->frame.lspv[4])<<14 |
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
338 (0x003F & q->frame.lspv[3])<< 8 |
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
339 (0x0060 & q->frame.lspv[2])<< 1 |
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
340 (0x0007 & q->frame.lspv[1])<< 3 |
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
341 (0x0038 & q->frame.lspv[0])>> 3 ;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
342 rnd = q->rnd_fir_filter_mem + 20;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
343 for(i=0; i<8; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
344 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
345 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
346 for(k=0; k<20; k++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
347 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
348 cbseed = 521 * cbseed + 259;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
349 *rnd = (int16_t)cbseed;
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
350
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
351 // FIR filter
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
352 fir_filter_value = 0.0;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
353 for(j=0; j<10; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
354 fir_filter_value += qcelp_rnd_fir_coefs[j ]
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
355 * (rnd[-j ] + rnd[-20+j]);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
356
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
357 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
358 *cdn_vector++ = tmp_gain * fir_filter_value;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
359 rnd++;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
360 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
361 }
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
362 memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
363 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
364 case RATE_OCTAVE:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
365 cbseed = q->first16bits;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
366 for(i=0; i<8; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
367 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
368 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
369 for(j=0; j<20; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
370 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
371 cbseed = 521 * cbseed + 259;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
372 *cdn_vector++ = tmp_gain * (int16_t)cbseed;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
373 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
374 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
375 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
376 case I_F_Q:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
377 cbseed = -44; // random codebook index
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
378 for(i=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
379 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
380 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
381 for(j=0; j<40; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
382 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
383 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
384 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
385 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
386 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
387
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
388 /**
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
389 * Apply generic gain control.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
390 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
391 * @param v_out output vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
392 * @param v_in gain-controlled vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
393 * @param v_ref vector to control gain of
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
394 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
395 * 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
396 * and the behavior of the gain control is
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
397 * undefined in the specs.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
398 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
399 * 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
400 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
401 static void apply_gain_ctrl(float *v_out, const float *v_ref,
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
402 const float *v_in)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
403 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
404 int i, j, len;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
405 float scalefactor;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
406
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
407 for(i=0, j=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
408 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
409 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
410 if(scalefactor)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
411 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
412 / scalefactor);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
413 else
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
414 av_log_missing_feature(NULL, "Zero energy for gain control", 1);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
415 for(len=j+40; j<len; j++)
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
416 v_out[j] = scalefactor * v_in[j];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
417 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
418 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
419
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
420 /**
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
421 * Apply filter in pitch-subframe steps.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
422 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
423 * @param memory buffer for the previous state of the filter
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
424 * - must be able to contain 303 elements
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
425 * - the 143 first elements are from the previous state
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
426 * - the next 160 are for output
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
427 * @param v_in input filter vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
428 * @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
429 * @param lag per-subframe lag array, each element is
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
430 * - between 16 and 143 if its corresponding pfrac is 0,
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
431 * - between 16 and 139 otherwise
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
432 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
433 * otherwise
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
434 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
435 * @return filter output vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
436 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
437 static const float *do_pitchfilter(float memory[303], const float v_in[160],
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
438 const float gain[4], const uint8_t *lag,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
439 const uint8_t pfrac[4])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
440 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
441 int i, j;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
442 float *v_lag, *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
443 const float *v_len;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
444
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
445 v_out = memory + 143; // Output vector starts at memory[143].
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
446
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
447 for(i=0; i<4; i++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
448 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
449 if(gain[i])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
450 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
451 v_lag = memory + 143 + 40 * i - lag[i];
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
452 for(v_len=v_in+40; v_in<v_len; v_in++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
453 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
454 if(pfrac[i]) // If it is a fractional lag...
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
455 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
456 for(j=0, *v_out=0.; j<4; j++)
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
457 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
458 }else
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
459 *v_out = *v_lag;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
460
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
461 *v_out = *v_in + gain[i] * *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
462
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
463 v_lag++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
464 v_out++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
465 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
466 }else
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
467 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
468 memcpy(v_out, v_in, 40 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
469 v_in += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
470 v_out += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
471 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
472 }
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
473
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
474 memmove(memory, memory + 160, 143 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
475 return memory + 143;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
476 }
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
477
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
478 /**
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
479 * Interpolates LSP frequencies and computes LPC coefficients
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
480 * for a given bitrate & pitch subframe.
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
481 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
482 * TIA/EIA/IS-733 2.4.3.3.4
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
483 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
484 * @param q the context
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
485 * @param curr_lspf LSP frequencies vector of the current frame
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
486 * @param lpc float vector for the resulting LPC
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
487 * @param subframe_num frame number in decoded stream
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
488 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
489 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
490 const int subframe_num)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
491 {
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
492 float interpolated_lspf[10];
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
493 float weight;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
494
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
495 if(q->bitrate >= RATE_QUARTER)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
496 weight = 0.25 * (subframe_num + 1);
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
497 else if(q->bitrate == RATE_OCTAVE && !subframe_num)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
498 weight = 0.625;
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
499 else
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
500 weight = 1.0;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
501
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
502 if(weight != 1.0)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
503 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
504 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
505 weight, 1.0 - weight, 10);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
506 qcelp_lspf2lpc(interpolated_lspf, lpc);
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
507 }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num))
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
508 qcelp_lspf2lpc(curr_lspf, lpc);
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
509 }
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
510
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
511 static int buf_size2bitrate(const int buf_size)
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
512 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
513 switch(buf_size)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
514 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
515 case 35:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
516 return RATE_FULL;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
517 case 17:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
518 return RATE_HALF;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
519 case 8:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
520 return RATE_QUARTER;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
521 case 4:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
522 return RATE_OCTAVE;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
523 case 1:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
524 return SILENCE;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
525 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
526
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
527 return -1;
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
528 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
529
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
530 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
531 const char *message)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
532 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
533 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
534 message);
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
535 }
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
536
8230
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
537 static int qcelp_decode_frame(AVCodecContext *avctx,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
538 void *data,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
539 int *data_size,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
540 uint8_t *buf,
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
541 const int buf_size) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
542 QCELPContext *q = avctx->priv_data;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
543 float *outbuffer = data;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
544 int i;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
545 float quantized_lspf[10], lpc[10];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
546 float gain[16];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
547 float *formant_mem;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
548
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
549 if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
550 warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
551 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
552 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
553
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
554 if (q->bitrate == RATE_OCTAVE &&
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
555 (q->first16bits = AV_RB16(buf)) == 0xFFFF) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
556 warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
557 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
558 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
559
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
560 if (q->bitrate > SILENCE) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
561 const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
562 const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
563 + qcelp_unpacking_bitmaps_lengths[q->bitrate];
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
564 uint8_t *unpacked_data = (uint8_t *)&q->frame;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
565
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
566 init_get_bits(&q->gb, buf, 8*buf_size);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
567
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
568 memset(&q->frame, 0, sizeof(QCELPFrame));
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
569
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
570 for (; bitmaps < bitmaps_end; bitmaps++)
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
571 unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
572
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
573 // Check for erasures/blanks on rates 1, 1/4 and 1/8.
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
574 if (q->frame.reserved) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
575 warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
576 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
577 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
578 if (q->bitrate == RATE_QUARTER && codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
579 warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
580 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
581 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
582
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
583 if (q->bitrate >= RATE_HALF) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
584 for (i = 0; i < 4; i++) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
585 if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
586 warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
587 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
588 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
589 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
590 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
591 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
592
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
593 decode_gain_and_index(q, gain);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
594 compute_svector(q, gain, outbuffer);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
595
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
596 if (decode_lspf(q, quantized_lspf) < 0) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
597 warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
598 goto erasure;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
599 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
600
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
601
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
602 apply_pitch_filters(q, outbuffer);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
603
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
604 if (q->bitrate == I_F_Q) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
605 erasure:
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
606 q->bitrate = I_F_Q;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
607 q->erasure_count++;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
608 decode_gain_and_index(q, gain);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
609 compute_svector(q, gain, outbuffer);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
610 decode_lspf(q, quantized_lspf);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
611 apply_pitch_filters(q, outbuffer);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
612 } else
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
613 q->erasure_count = 0;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
614
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
615 formant_mem = q->formant_mem + 10;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
616 for (i = 0; i < 4; i++) {
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
617 interpolate_lpc(q, quantized_lspf, lpc, i);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
618 ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 10);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
619 formant_mem += 40;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
620 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
621 memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
622
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
623 // FIXME: postfilter and final gain control should be here.
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
624 // TIA/EIA/IS-733 2.4.8.6
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
625
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
626 formant_mem = q->formant_mem + 10;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
627 for (i = 0; i < 160; i++)
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
628 *outbuffer++ = av_clipf(*formant_mem++, QCELP_CLIP_LOWER_BOUND, QCELP_CLIP_UPPER_BOUND);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
629
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
630 memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
631 q->prev_bitrate = q->bitrate;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
632
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
633 *data_size = 160 * sizeof(*outbuffer);
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
634
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
635 return *data_size;
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
636 }
72949bacc1b9 More OKed parts of the QCELP decoder
vitor
parents: 8193
diff changeset
637
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
638 AVCodec qcelp_decoder =
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
639 {
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
640 .name = "qcelp",
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
641 .type = CODEC_TYPE_AUDIO,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
642 .id = CODEC_ID_QCELP,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
643 .init = qcelp_decode_init,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
644 .decode = qcelp_decode_frame,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
645 .priv_data_size = sizeof(QCELPContext),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
646 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
647 };