8096
|
1 /*
|
|
2 * QCELP decoder
|
|
3 * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
|
|
4 *
|
|
5 * This file is part of FFmpeg.
|
|
6 *
|
|
7 * FFmpeg is free software; you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Lesser General Public
|
|
9 * License as published by the Free Software Foundation; either
|
|
10 * version 2.1 of the License, or (at your option) any later version.
|
|
11 *
|
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 * Lesser General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Lesser General Public
|
|
18 * License along with FFmpeg; if not, write to the Free Software
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20 */
|
|
21 /**
|
|
22 * @file qcelpdec.c
|
|
23 * QCELP decoder
|
|
24 * @author Reynaldo H. Verdejo Pinochet
|
|
25 */
|
|
26
|
|
27 #include <stddef.h>
|
|
28
|
|
29 #include "avcodec.h"
|
|
30 #include "bitstream.h"
|
|
31
|
|
32 #include "qcelp.h"
|
|
33 #include "qcelpdata.h"
|
|
34
|
|
35 #include "celp_math.h"
|
|
36 #include "celp_filters.h"
|
|
37
|
|
38 #undef NDEBUG
|
|
39 #include <assert.h>
|
|
40
|
8123
|
41 static void weighted_vector_sumf(float *out,
|
|
42 const float *in_a,
|
|
43 const float *in_b,
|
|
44 float weight_coeff_a,
|
|
45 float weight_coeff_b,
|
|
46 int length) {
|
|
47 int i;
|
|
48
|
|
49 for (i = 0; i < length; i++)
|
|
50 out[i] = weight_coeff_a * in_a[i]
|
|
51 + weight_coeff_b * in_b[i];
|
|
52 }
|
|
53
|
8096
|
54 /**
|
|
55 * Apply filter in pitch-subframe steps.
|
|
56 *
|
|
57 * @param memory buffer for the previous state of the filter
|
|
58 * - must be able to contain 303 elements
|
|
59 * - the 143 first elements are from the previous state
|
|
60 * - the next 160 are for output
|
|
61 * @param v_in input filter vector
|
|
62 * @param gain per-subframe gain array, each element is between 0.0 and 2.0
|
|
63 * @param lag per-subframe lag array, each element is
|
|
64 * - between 16 and 143 if its corresponding pfrac is 0,
|
|
65 * - between 16 and 139 otherwise
|
|
66 * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 otherwise
|
|
67 *
|
|
68 * @return filter output vector
|
|
69 */
|
|
70 static const float *do_pitchfilter(float memory[303],
|
|
71 const float v_in[160],
|
|
72 const float gain[4],
|
|
73 const uint8_t *lag,
|
|
74 const uint8_t pfrac[4]) {
|
|
75 int i, j;
|
|
76 float *v_lag, *v_out;
|
|
77 const float *v_len;
|
|
78
|
|
79 v_out = memory + 143; // Output vector starts at memory[143].
|
|
80
|
|
81 for (i = 0; i < 4; i++)
|
|
82 if (gain[i]) {
|
|
83 v_lag = memory + 143 + 40 * i - lag[i];
|
|
84 for (v_len = v_in + 40; v_in < v_len; v_in++) {
|
|
85 if (pfrac[i]) { // If it is a fractional lag...
|
|
86 for (j = 0, *v_out = 0.; j < 4; j++)
|
|
87 *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
|
|
88 } else
|
|
89 *v_out = *v_lag;
|
|
90
|
|
91 *v_out = *v_in + gain[i] * *v_out;
|
|
92
|
|
93 v_lag++;
|
|
94 v_out++;
|
|
95 }
|
|
96 } else {
|
|
97 memcpy(v_out, v_in, 40 * sizeof(float));
|
|
98 v_in += 40;
|
|
99 v_out += 40;
|
|
100 }
|
|
101
|
|
102 memmove(memory, memory + 160, 143 * sizeof(float));
|
|
103 return memory + 143;
|
|
104 }
|
|
105
|
8127
|
106 /**
|
|
107 * Interpolates LSP frequencies and computes LPC coefficients
|
|
108 * for a given framerate & pitch subframe.
|
|
109 *
|
|
110 * TIA/EIA/IS-733 2.4.3.3.4
|
|
111 *
|
|
112 * @param q the context
|
|
113 * @param curr_lspf LSP frequencies vector of the current frame
|
|
114 * @param lpc float vector for the resulting LPC
|
|
115 * @param subframe_num frame number in decoded stream
|
|
116 */
|
|
117 void interpolate_lpc(QCELPContext *q,
|
|
118 const float *curr_lspf,
|
|
119 float *lpc,
|
|
120 const int subframe_num) {
|
|
121 float interpolated_lspf[10];
|
|
122 float weight;
|
|
123
|
|
124 if (q->framerate >= RATE_QUARTER) {
|
|
125 weight = 0.25 * (subframe_num + 1);
|
|
126 } else if (q->framerate == RATE_OCTAVE && !subframe_num) {
|
|
127 weight = 0.625;
|
|
128 } else {
|
|
129 weight = 1.0;
|
|
130 }
|
|
131
|
|
132 if (weight != 1.0) {
|
|
133 weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, weight, 1.0 - weight, 10);
|
|
134 lspf2lpc(q, interpolated_lspf, lpc);
|
|
135 } else if (q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num))
|
|
136 lspf2lpc(q, curr_lspf, lpc);
|
|
137 }
|
|
138
|
8123
|
139 static int buf_size2framerate(const int buf_size) {
|
|
140 switch (buf_size) {
|
|
141 case 35:
|
|
142 return RATE_FULL;
|
|
143 case 17:
|
|
144 return RATE_HALF;
|
|
145 case 8:
|
|
146 return RATE_QUARTER;
|
|
147 case 4:
|
|
148 return RATE_OCTAVE;
|
|
149 case 1:
|
|
150 return SILENCE;
|
|
151 }
|
|
152 return -1;
|
|
153 }
|
|
154
|
8096
|
155 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
|
|
156 const char *message) {
|
|
157 av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, message);
|
|
158 }
|
8127
|
159
|
|
160 AVCodec qcelp_decoder =
|
|
161 {
|
|
162 .name = "qcelp",
|
|
163 .type = CODEC_TYPE_AUDIO,
|
|
164 .id = CODEC_ID_QCELP,
|
|
165 .init = qcelp_decode_init,
|
|
166 .decode = qcelp_decode_frame,
|
|
167 .priv_data_size = sizeof(QCELPContext),
|
|
168 .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
|
|
169 };
|