annotate qcelpdec.c @ 8227:596677256482 libavcodec

Implement the fields rc_max_available_vbv_use and rc_min_vbv_overflow_use in AVCodecContext, and use their values in the ratecontrol code rather than hardcoded ones. See the thread: "[RFC] ratecontrol buffer size magic". Patch by Baptiste Coudurier.
author stefano
date Sat, 29 Nov 2008 14:08:48 +0000
parents c04182909bd8
children 72949bacc1b9
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
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
43 static void weighted_vector_sumf(float *out, const float *in_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
44 const float *in_b, float weight_coeff_a,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
45 float weight_coeff_b, int length)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
46 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
47 int i;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
48
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
49 for(i=0; i<length; i++)
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
50 out[i] = weight_coeff_a * in_a[i]
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
51 + weight_coeff_b * in_b[i];
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
52 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
53
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
54 /**
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
55 * Initialize the speech codec according to the specification.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
56 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
57 * TIA/EIA/IS-733 2.4.9
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
58 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
59 static av_cold int qcelp_decode_init(AVCodecContext *avctx)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
60 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
61 QCELPContext *q = avctx->priv_data;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
62 int i;
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 avctx->sample_fmt = SAMPLE_FMT_FLT;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
65
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
66 for (i = 0; i < 10; i++)
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
67 q->prev_lspf[i] = (i + 1) / 11.;
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 return 0;
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
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
72 /**
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
73 * Decodes the 10 quantized LSP frequencies from the LSPV/LSP
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
74 * transmission codes of any bitrate and checks for badly received packets.
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
75 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
76 * @param q the context
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
77 * @param lspf line spectral pair frequencies
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
78 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
79 * @return 0 on success, -1 if the packet is badly received
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
80 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
81 * 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
82 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
83 static int decode_lspf(QCELPContext *q, float *lspf)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
84 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
85 int i;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
86 float tmp_lspf;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
87
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
88 if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
89 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
90 float smooth;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
91 const float *predictors = (q->prev_bitrate != RATE_OCTAVE &&
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
92 q->prev_bitrate != I_F_Q ? q->prev_lspf
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
93 : q->predictor_lspf);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
94
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
95 if(q->bitrate == RATE_OCTAVE)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
96 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
97 q->octave_count++;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
98
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
99 for(i=0; i<10; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
100 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
101 q->predictor_lspf[i] =
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
102 lspf[i] = (q->lspv[i] ? QCELP_LSP_SPREAD_FACTOR
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
103 : -QCELP_LSP_SPREAD_FACTOR)
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
104 + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
105 + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
106 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
107 smooth = (q->octave_count < 10 ? .875 : 0.1);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
108 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
109 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
110 float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
111
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
112 assert(q->bitrate == I_F_Q);
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
113
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
114 if(q->erasure_count > 1)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
115 erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
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] =
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
120 lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
121 + erasure_coeff * predictors[i];
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
122 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
123 smooth = 0.125;
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
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
126 // Check the stability of the LSP frequencies.
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
127 lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
128 for(i=1; i<10; i++)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
129 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
130
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
131 lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
132 for(i=9; i>0; i--)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
133 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
134
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
135 // Low-pass filter the LSP frequencies.
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
136 weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
137 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
138 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
139 q->octave_count = 0;
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 tmp_lspf = 0.;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
142 for(i=0; i<5 ; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
143 {
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
144 lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][0] * 0.0001;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
145 lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][1] * 0.0001;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
146 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
147
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
148 // Check for badly received packets.
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
149 if(q->bitrate == RATE_QUARTER)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
150 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
151 if(lspf[9] <= .70 || lspf[9] >= .97)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
152 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
153 for(i=3; i<10; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
154 if(fabs(lspf[i] - lspf[i-2]) < .08)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
155 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
156 }else
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
157 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
158 if(lspf[9] <= .66 || lspf[9] >= .985)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
159 return -1;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
160 for(i=4; i<10; i++)
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
161 if (fabs(lspf[i] - lspf[i-4]) < .0931)
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
162 return -1;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
163 }
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 return 0;
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
166 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
167
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
168 /**
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
169 * If the received packet is Rate 1/4 a further sanity check is made of the
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
170 * codebook gain.
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
171 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
172 * @param cbgain the unpacked cbgain array
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
173 * @return -1 if the sanity check fails, 0 otherwise
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
174 *
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
175 * TIA/EIA/IS-733 2.4.8.7.3
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
176 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
177 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
178 {
8193
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
179 int i, prev_diff=0;
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
180
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
181 for(i=1; i<5; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
182 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
183 int diff = cbgain[i] - cbgain[i-1];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
184 if(FFABS(diff) > 10)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
185 return -1;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
186 else if(FFABS(diff - prev_diff) > 12)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
187 return -1;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
188 prev_diff = diff;
8193
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
189 }
c04182909bd8 Trivial, Cosmetics
reynaldo
parents: 8192
diff changeset
190 return 0;
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
191 }
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
192
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
193 /**
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
194 * Computes the scaled codebook vector Cdn From INDEX and GAIN
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
195 * for all rates.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
196 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
197 * The specification lacks some information here.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
198 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
199 * 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
200 * 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
201 * 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
202 * 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
203 * 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
204 * RI-compliant results.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
205 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
206 * 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
207 * have to do these calculations per codebook subframe and adjust given
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
208 * equation values accordingly.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
209 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
210 * @param q the context
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
211 * @param gain array holding the 4 pitch subframe gain values
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
212 * @param cdn_vector array for the generated scaled codebook vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
213 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
214 static void compute_svector(const QCELPContext *q, const float *gain,
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
215 float *cdn_vector)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
216 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
217 int i, j, k;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
218 uint16_t cbseed, cindex;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
219 float *rnd, tmp_gain, fir_filter_value;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
220
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
221 switch(q->bitrate)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
222 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
223 case RATE_FULL:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
224 for(i=0; i<16; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
225 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
226 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
227 cindex = -q->cindex[i];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
228 for(j=0; j<10; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
229 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
230 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
231 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
232 case RATE_HALF:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
233 for(i=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
234 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
235 tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
236 cindex = -q->cindex[i];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
237 for (j = 0; j < 40; j++)
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
238 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
239 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
240 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
241 case RATE_QUARTER:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
242 cbseed = (0x0003 & q->lspv[4])<<14 |
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
243 (0x003F & q->lspv[3])<< 8 |
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
244 (0x0060 & q->lspv[2])<< 1 |
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
245 (0x0007 & q->lspv[1])<< 3 |
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
246 (0x0038 & q->lspv[0])>> 3 ;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
247 rnd = q->rnd_fir_filter_mem + 20;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
248 for(i=0; i<8; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
249 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
250 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
251 for(k=0; k<20; k++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
252 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
253 cbseed = 521 * cbseed + 259;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
254 *rnd = (int16_t)cbseed;
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
255
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
256 // FIR filter
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
257 fir_filter_value = 0.0;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
258 for(j=0; j<10; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
259 fir_filter_value += qcelp_rnd_fir_coefs[j ]
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
260 * (rnd[-j ] + rnd[-20+j]);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
261
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
262 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
263 *cdn_vector++ = tmp_gain * fir_filter_value;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
264 rnd++;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
265 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
266 }
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
267 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
268 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
269 case RATE_OCTAVE:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
270 cbseed = q->first16bits;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
271 for(i=0; i<8; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
272 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
273 tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
274 for(j=0; j<20; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
275 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
276 cbseed = 521 * cbseed + 259;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
277 *cdn_vector++ = tmp_gain * (int16_t)cbseed;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
278 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
279 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
280 break;
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
281 case I_F_Q:
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
282 cbseed = -44; // random codebook index
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
283 for(i=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
284 {
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
285 tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
286 for(j=0; j<40; j++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
287 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
288 }
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
289 break;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
290 }
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
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 * Apply generic gain control.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
295 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
296 * @param v_out output vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
297 * @param v_in gain-controlled vector
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
298 * @param v_ref vector to control gain of
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
299 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
300 * 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
301 * and the behavior of the gain control is
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
302 * undefined in the specs.
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
303 *
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
304 * 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
305 */
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
306 static void apply_gain_ctrl(float *v_out, const float *v_ref,
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
307 const float *v_in)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
308 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
309 int i, j, len;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
310 float scalefactor;
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
311
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
312 for(i=0, j=0; i<4; i++)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
313 {
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
314 scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
315 if(scalefactor)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
316 scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40)
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
317 / scalefactor);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
318 else
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
319 av_log_missing_feature(NULL, "Zero energy for gain control", 1);
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
320 for(len=j+40; j<len; j++)
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
321 v_out[j] = scalefactor * v_in[j];
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
322 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
323 }
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
324
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
325 /**
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
326 * Apply filter in pitch-subframe steps.
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
327 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
328 * @param memory buffer for the previous state of the filter
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
329 * - must be able to contain 303 elements
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
330 * - the 143 first elements are from the previous state
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
331 * - the next 160 are for output
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
332 * @param v_in input filter vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
333 * @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
334 * @param lag per-subframe lag array, each element is
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
335 * - between 16 and 143 if its corresponding pfrac is 0,
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
336 * - between 16 and 139 otherwise
8192
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
337 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
d6b40db1a747 Trivial, Cosmetics
reynaldo
parents: 8191
diff changeset
338 * otherwise
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
339 *
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
340 * @return filter output vector
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
341 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
342 static const float *do_pitchfilter(float memory[303], const float v_in[160],
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
343 const float gain[4], const uint8_t *lag,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
344 const uint8_t pfrac[4])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
345 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
346 int i, j;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
347 float *v_lag, *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
348 const float *v_len;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
349
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
350 v_out = memory + 143; // Output vector starts at memory[143].
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
351
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
352 for(i=0; i<4; i++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
353 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
354 if(gain[i])
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
355 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
356 v_lag = memory + 143 + 40 * i - lag[i];
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
357 for(v_len=v_in+40; v_in<v_len; v_in++)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
358 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
359 if(pfrac[i]) // If it is a fractional lag...
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
360 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
361 for(j=0, *v_out=0.; j<4; j++)
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
362 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
363 }else
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
364 *v_out = *v_lag;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
365
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
366 *v_out = *v_in + gain[i] * *v_out;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
367
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
368 v_lag++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
369 v_out++;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
370 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
371 }else
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
372 {
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
373 memcpy(v_out, v_in, 40 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
374 v_in += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
375 v_out += 40;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
376 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
377 }
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
378
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
379 memmove(memory, memory + 160, 143 * sizeof(float));
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
380 return memory + 143;
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
381 }
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
382
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
383 /**
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
384 * Interpolates LSP frequencies and computes LPC coefficients
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
385 * for a given bitrate & pitch subframe.
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
386 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
387 * TIA/EIA/IS-733 2.4.3.3.4
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
388 *
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
389 * @param q the context
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
390 * @param curr_lspf LSP frequencies vector of the current frame
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
391 * @param lpc float vector for the resulting LPC
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
392 * @param subframe_num frame number in decoded stream
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
393 */
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
394 void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
395 const int subframe_num)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
396 {
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
397 float interpolated_lspf[10];
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
398 float weight;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
399
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
400 if(q->bitrate >= RATE_QUARTER)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
401 weight = 0.25 * (subframe_num + 1);
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
402 else if(q->bitrate == RATE_OCTAVE && !subframe_num)
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
403 weight = 0.625;
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
404 else
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
405 weight = 1.0;
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
406
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
407 if(weight != 1.0)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
408 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
409 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
410 weight, 1.0 - weight, 10);
8145
f27d01aff4af More OKed parts of the QCELP decoder
vitor
parents: 8127
diff changeset
411 qcelp_lspf2lpc(interpolated_lspf, lpc);
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
412 }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
413 qcelp_lspf2lpc(curr_lspf, lpc);
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
414 }
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
415
8191
cc1e8c59f1e8 More OKed parts of the QCELP decoder
vitor
parents: 8151
diff changeset
416 static int buf_size2bitrate(const int buf_size)
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
417 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
418 switch(buf_size)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
419 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
420 case 35:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
421 return RATE_FULL;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
422 case 17:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
423 return RATE_HALF;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
424 case 8:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
425 return RATE_QUARTER;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
426 case 4:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
427 return RATE_OCTAVE;
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
428 case 1:
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
429 return SILENCE;
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
430 }
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
431
8123
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
432 return -1;
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
433 }
cd756806be02 More OKed parts of the QCELP decoder
vitor
parents: 8096
diff changeset
434
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
435 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
8150
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
436 const char *message)
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
437 {
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
438 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
4da8fc62ae00 Cosmetics
reynaldo
parents: 8145
diff changeset
439 message);
8096
eb55481f35fa OKed parts of the QCELP decoder
vitor
parents:
diff changeset
440 }
8127
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
441
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
442 AVCodec qcelp_decoder =
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
443 {
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
444 .name = "qcelp",
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
445 .type = CODEC_TYPE_AUDIO,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
446 .id = CODEC_ID_QCELP,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
447 .init = qcelp_decode_init,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
448 .decode = qcelp_decode_frame,
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
449 .priv_data_size = sizeof(QCELPContext),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
450 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
3b5256153553 More OKed parts of the QCELP decoder
vitor
parents: 8123
diff changeset
451 };