| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Real Audio 1.0 (14.4K) encoder | ||
| 3 | * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it> | ||
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * Real Audio 1.0 (14.4K) encoder | ||
| 25 | * @author Francesco Lavra <francescolavra@interfree.it> | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <float.h> | ||
| 29 | |||
| 30 | #include "libavutil/channel_layout.h" | ||
| 31 | #include "avcodec.h" | ||
| 32 | #include "audio_frame_queue.h" | ||
| 33 | #include "celp_filters.h" | ||
| 34 | #include "codec_internal.h" | ||
| 35 | #include "encode.h" | ||
| 36 | #include "mathops.h" | ||
| 37 | #include "put_bits.h" | ||
| 38 | #include "ra144.h" | ||
| 39 | |||
| 40 | 1 | static av_cold int ra144_encode_close(AVCodecContext *avctx) | |
| 41 | { | ||
| 42 | 1 | RA144Context *ractx = avctx->priv_data; | |
| 43 | 1 | ff_lpc_end(&ractx->lpc_ctx); | |
| 44 | 1 | ff_af_queue_close(&ractx->afq); | |
| 45 | 1 | return 0; | |
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | 1 | static av_cold int ra144_encode_init(AVCodecContext * avctx) | |
| 50 | { | ||
| 51 | RA144Context *ractx; | ||
| 52 | int ret; | ||
| 53 | |||
| 54 | 1 | avctx->frame_size = NBLOCKS * BLOCKSIZE; | |
| 55 | 1 | avctx->initial_padding = avctx->frame_size; | |
| 56 | 1 | avctx->bit_rate = 8000; | |
| 57 | 1 | ractx = avctx->priv_data; | |
| 58 | 1 | ractx->lpc_coef[0] = ractx->lpc_tables[0]; | |
| 59 | 1 | ractx->lpc_coef[1] = ractx->lpc_tables[1]; | |
| 60 | 1 | ractx->avctx = avctx; | |
| 61 | 1 | ff_audiodsp_init(&ractx->adsp); | |
| 62 | 1 | ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER, | |
| 63 | FF_LPC_TYPE_LEVINSON); | ||
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 65 | ✗ | return ret; | |
| 66 | |||
| 67 | 1 | ff_af_queue_init(avctx, &ractx->afq); | |
| 68 | |||
| 69 | 1 | return 0; | |
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | /** | ||
| 74 | * Quantize a value by searching a sorted table for the element with the | ||
| 75 | * nearest value | ||
| 76 | * | ||
| 77 | * @param value value to quantize | ||
| 78 | * @param table array containing the quantization table | ||
| 79 | * @param size size of the quantization table | ||
| 80 | * @return index of the quantization table corresponding to the element with the | ||
| 81 | * nearest value | ||
| 82 | */ | ||
| 83 | 3612 | static int quantize(int value, const int16_t *table, unsigned int size) | |
| 84 | { | ||
| 85 | 3612 | unsigned int low = 0, high = size - 1; | |
| 86 | |||
| 87 | 14245 | while (1) { | |
| 88 | 17857 | int index = (low + high) >> 1; | |
| 89 | 17857 | int error = table[index] - value; | |
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 3612 times.
✓ Branch 1 taken 14245 times.
|
17857 | if (index == low) |
| 92 |
2/2✓ Branch 0 taken 1748 times.
✓ Branch 1 taken 1864 times.
|
3612 | return table[high] + error > value ? low : high; |
| 93 |
2/2✓ Branch 0 taken 5875 times.
✓ Branch 1 taken 8370 times.
|
14245 | if (error > 0) { |
| 94 | 5875 | high = index; | |
| 95 | } else { | ||
| 96 | 8370 | low = index; | |
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | /** | ||
| 103 | * Orthogonalize a vector to another vector | ||
| 104 | * | ||
| 105 | * @param v vector to orthogonalize | ||
| 106 | * @param u vector against which orthogonalization is performed | ||
| 107 | */ | ||
| 108 | 461357 | static void orthogonalize(float *v, const float *u) | |
| 109 | { | ||
| 110 | int i; | ||
| 111 | 461357 | float num = 0, den = 0; | |
| 112 | |||
| 113 |
2/2✓ Branch 0 taken 18454280 times.
✓ Branch 1 taken 461357 times.
|
18915637 | for (i = 0; i < BLOCKSIZE; i++) { |
| 114 | 18454280 | num += v[i] * u[i]; | |
| 115 | 18454280 | den += u[i] * u[i]; | |
| 116 | } | ||
| 117 | 461357 | num /= den; | |
| 118 |
2/2✓ Branch 0 taken 18454280 times.
✓ Branch 1 taken 461357 times.
|
18915637 | for (i = 0; i < BLOCKSIZE; i++) |
| 119 | 18454280 | v[i] -= num * u[i]; | |
| 120 | 461357 | } | |
| 121 | |||
| 122 | |||
| 123 | /** | ||
| 124 | * Calculate match score and gain of an LPC-filtered vector with respect to | ||
| 125 | * input data, possibly orthogonalizing it to up to two other vectors. | ||
| 126 | * | ||
| 127 | * @param work array used to calculate the filtered vector | ||
| 128 | * @param coefs coefficients of the LPC filter | ||
| 129 | * @param vect original vector | ||
| 130 | * @param ortho1 first vector against which orthogonalization is performed | ||
| 131 | * @param ortho2 second vector against which orthogonalization is performed | ||
| 132 | * @param data input data | ||
| 133 | * @param score pointer to variable where match score is returned | ||
| 134 | * @param gain pointer to variable where gain is returned | ||
| 135 | */ | ||
| 136 | 461132 | static void get_match_score(float *work, const float *coefs, float *vect, | |
| 137 | const float *ortho1, const float *ortho2, | ||
| 138 | const float *data, float *score, float *gain) | ||
| 139 | { | ||
| 140 | float c, g; | ||
| 141 | int i; | ||
| 142 | |||
| 143 | 461132 | ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER); | |
| 144 |
2/2✓ Branch 0 taken 306432 times.
✓ Branch 1 taken 154700 times.
|
461132 | if (ortho1) |
| 145 | 306432 | orthogonalize(work, ortho1); | |
| 146 |
2/2✓ Branch 0 taken 153728 times.
✓ Branch 1 taken 307404 times.
|
461132 | if (ortho2) |
| 147 | 153728 | orthogonalize(work, ortho2); | |
| 148 | 461132 | c = g = 0; | |
| 149 |
2/2✓ Branch 0 taken 18445280 times.
✓ Branch 1 taken 461132 times.
|
18906412 | for (i = 0; i < BLOCKSIZE; i++) { |
| 150 | 18445280 | g += work[i] * work[i]; | |
| 151 | 18445280 | c += data[i] * work[i]; | |
| 152 | } | ||
| 153 |
2/2✓ Branch 0 taken 232992 times.
✓ Branch 1 taken 228140 times.
|
461132 | if (c <= 0) { |
| 154 | 232992 | *score = 0; | |
| 155 | 232992 | return; | |
| 156 | } | ||
| 157 | 228140 | *gain = c / g; | |
| 158 | 228140 | *score = *gain * c; | |
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | /** | ||
| 163 | * Create a vector from the adaptive codebook at a given lag value | ||
| 164 | * | ||
| 165 | * @param vect array where vector is stored | ||
| 166 | * @param cb adaptive codebook | ||
| 167 | * @param lag lag value | ||
| 168 | */ | ||
| 169 | 154105 | static void create_adapt_vect(float *vect, const int16_t *cb, int lag) | |
| 170 | { | ||
| 171 | int i; | ||
| 172 | |||
| 173 | 154105 | cb += BUFFERSIZE - lag; | |
| 174 |
2/2✓ Branch 0 taken 5906921 times.
✓ Branch 1 taken 154105 times.
|
6061026 | for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++) |
| 175 | 5906921 | vect[i] = cb[i]; | |
| 176 |
2/2✓ Branch 0 taken 24517 times.
✓ Branch 1 taken 129588 times.
|
154105 | if (lag < BLOCKSIZE) |
| 177 |
2/2✓ Branch 0 taken 257279 times.
✓ Branch 1 taken 24517 times.
|
281796 | for (i = 0; i < BLOCKSIZE - lag; i++) |
| 178 | 257279 | vect[lag + i] = cb[i]; | |
| 179 | 154105 | } | |
| 180 | |||
| 181 | |||
| 182 | /** | ||
| 183 | * Search the adaptive codebook for the best entry and gain and remove its | ||
| 184 | * contribution from input data | ||
| 185 | * | ||
| 186 | * @param adapt_cb array from which the adaptive codebook is extracted | ||
| 187 | * @param work array used to calculate LPC-filtered vectors | ||
| 188 | * @param coefs coefficients of the LPC filter | ||
| 189 | * @param data input data | ||
| 190 | * @return index of the best entry of the adaptive codebook | ||
| 191 | */ | ||
| 192 | 1204 | static int adaptive_cb_search(const int16_t *adapt_cb, float *work, | |
| 193 | const float *coefs, float *data) | ||
| 194 | { | ||
| 195 | 1204 | int i, av_uninit(best_vect); | |
| 196 | 1204 | float score, gain, best_score, av_uninit(best_gain); | |
| 197 | float exc[BLOCKSIZE]; | ||
| 198 | |||
| 199 | 1204 | gain = best_score = 0; | |
| 200 |
2/2✓ Branch 0 taken 152908 times.
✓ Branch 1 taken 1204 times.
|
154112 | for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) { |
| 201 | 152908 | create_adapt_vect(exc, adapt_cb, i); | |
| 202 | 152908 | get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain); | |
| 203 |
2/2✓ Branch 0 taken 5528 times.
✓ Branch 1 taken 147380 times.
|
152908 | if (score > best_score) { |
| 204 | 5528 | best_score = score; | |
| 205 | 5528 | best_vect = i; | |
| 206 | 5528 | best_gain = gain; | |
| 207 | } | ||
| 208 | } | ||
| 209 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1197 times.
|
1204 | if (!best_score) |
| 210 | 7 | return 0; | |
| 211 | |||
| 212 | /** | ||
| 213 | * Re-calculate the filtered vector from the vector with maximum match score | ||
| 214 | * and remove its contribution from input data. | ||
| 215 | */ | ||
| 216 | 1197 | create_adapt_vect(exc, adapt_cb, best_vect); | |
| 217 | 1197 | ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER); | |
| 218 |
2/2✓ Branch 0 taken 47880 times.
✓ Branch 1 taken 1197 times.
|
49077 | for (i = 0; i < BLOCKSIZE; i++) |
| 219 | 47880 | data[i] -= best_gain * work[i]; | |
| 220 | 1197 | return best_vect - BLOCKSIZE / 2 + 1; | |
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | /** | ||
| 225 | * Find the best vector of a fixed codebook by applying an LPC filter to | ||
| 226 | * codebook entries, possibly orthogonalizing them to up to two other vectors | ||
| 227 | * and matching the results with input data. | ||
| 228 | * | ||
| 229 | * @param work array used to calculate the filtered vectors | ||
| 230 | * @param coefs coefficients of the LPC filter | ||
| 231 | * @param cb fixed codebook | ||
| 232 | * @param ortho1 first vector against which orthogonalization is performed | ||
| 233 | * @param ortho2 second vector against which orthogonalization is performed | ||
| 234 | * @param data input data | ||
| 235 | * @param idx pointer to variable where the index of the best codebook entry is | ||
| 236 | * returned | ||
| 237 | * @param gain pointer to variable where the gain of the best codebook entry is | ||
| 238 | * returned | ||
| 239 | */ | ||
| 240 | 2408 | static void find_best_vect(float *work, const float *coefs, | |
| 241 | const int8_t cb[][BLOCKSIZE], const float *ortho1, | ||
| 242 | const float *ortho2, float *data, int *idx, | ||
| 243 | float *gain) | ||
| 244 | { | ||
| 245 | int i, j; | ||
| 246 | float g, score, best_score; | ||
| 247 | float vect[BLOCKSIZE]; | ||
| 248 | |||
| 249 | 2408 | *idx = *gain = best_score = 0; | |
| 250 |
2/2✓ Branch 0 taken 308224 times.
✓ Branch 1 taken 2408 times.
|
310632 | for (i = 0; i < FIXED_CB_SIZE; i++) { |
| 251 |
2/2✓ Branch 0 taken 12328960 times.
✓ Branch 1 taken 308224 times.
|
12637184 | for (j = 0; j < BLOCKSIZE; j++) |
| 252 | 12328960 | vect[j] = cb[i][j]; | |
| 253 | 308224 | get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g); | |
| 254 |
2/2✓ Branch 0 taken 14127 times.
✓ Branch 1 taken 294097 times.
|
308224 | if (score > best_score) { |
| 255 | 14127 | best_score = score; | |
| 256 | 14127 | *idx = i; | |
| 257 | 14127 | *gain = g; | |
| 258 | } | ||
| 259 | } | ||
| 260 | 2408 | } | |
| 261 | |||
| 262 | |||
| 263 | /** | ||
| 264 | * Search the two fixed codebooks for the best entry and gain | ||
| 265 | * | ||
| 266 | * @param work array used to calculate LPC-filtered vectors | ||
| 267 | * @param coefs coefficients of the LPC filter | ||
| 268 | * @param data input data | ||
| 269 | * @param cba_idx index of the best entry of the adaptive codebook | ||
| 270 | * @param cb1_idx pointer to variable where the index of the best entry of the | ||
| 271 | * first fixed codebook is returned | ||
| 272 | * @param cb2_idx pointer to variable where the index of the best entry of the | ||
| 273 | * second fixed codebook is returned | ||
| 274 | */ | ||
| 275 | 1204 | static void fixed_cb_search(float *work, const float *coefs, float *data, | |
| 276 | int cba_idx, int *cb1_idx, int *cb2_idx) | ||
| 277 | { | ||
| 278 | int i, ortho_cb1; | ||
| 279 | float gain; | ||
| 280 | float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE]; | ||
| 281 | float vect[BLOCKSIZE]; | ||
| 282 | |||
| 283 | /** | ||
| 284 | * The filtered vector from the adaptive codebook can be retrieved from | ||
| 285 | * work, because this function is called just after adaptive_cb_search(). | ||
| 286 | */ | ||
| 287 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 7 times.
|
1204 | if (cba_idx) |
| 288 | 1197 | memcpy(cba_vect, work, sizeof(cba_vect)); | |
| 289 | |||
| 290 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 7 times.
|
1204 | find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL, |
| 291 | data, cb1_idx, &gain); | ||
| 292 | |||
| 293 | /** | ||
| 294 | * Re-calculate the filtered vector from the vector with maximum match score | ||
| 295 | * and remove its contribution from input data. | ||
| 296 | */ | ||
| 297 |
2/2✓ Branch 0 taken 1201 times.
✓ Branch 1 taken 3 times.
|
1204 | if (gain) { |
| 298 |
2/2✓ Branch 0 taken 48040 times.
✓ Branch 1 taken 1201 times.
|
49241 | for (i = 0; i < BLOCKSIZE; i++) |
| 299 | 48040 | vect[i] = ff_cb1_vects[*cb1_idx][i]; | |
| 300 | 1201 | ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER); | |
| 301 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 4 times.
|
1201 | if (cba_idx) |
| 302 | 1197 | orthogonalize(work, cba_vect); | |
| 303 |
2/2✓ Branch 0 taken 48040 times.
✓ Branch 1 taken 1201 times.
|
49241 | for (i = 0; i < BLOCKSIZE; i++) |
| 304 | 48040 | data[i] -= gain * work[i]; | |
| 305 | 1201 | memcpy(cb1_vect, work, sizeof(cb1_vect)); | |
| 306 | 1201 | ortho_cb1 = 1; | |
| 307 | } else | ||
| 308 | 3 | ortho_cb1 = 0; | |
| 309 | |||
| 310 |
4/4✓ Branch 0 taken 1201 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1197 times.
✓ Branch 3 taken 7 times.
|
1204 | find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL, |
| 311 | ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain); | ||
| 312 | 1204 | } | |
| 313 | |||
| 314 | |||
| 315 | /** | ||
| 316 | * Encode a subblock of the current frame | ||
| 317 | * | ||
| 318 | * @param ractx encoder context | ||
| 319 | * @param sblock_data input data of the subblock | ||
| 320 | * @param lpc_coefs coefficients of the LPC filter | ||
| 321 | * @param rms RMS of the reflection coefficients | ||
| 322 | * @param pb pointer to PutBitContext of the current frame | ||
| 323 | */ | ||
| 324 | 1204 | static void ra144_encode_subblock(RA144Context *ractx, | |
| 325 | const int16_t *sblock_data, | ||
| 326 | const int16_t *lpc_coefs, unsigned int rms, | ||
| 327 | PutBitContext *pb) | ||
| 328 | { | ||
| 329 | 1204 | float data[BLOCKSIZE] = { 0 }, work[LPC_ORDER + BLOCKSIZE]; | |
| 330 | float coefs[LPC_ORDER]; | ||
| 331 | float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE]; | ||
| 332 | int cba_idx, cb1_idx, cb2_idx, gain; | ||
| 333 | int i, n; | ||
| 334 | unsigned m[3]; | ||
| 335 | float g[3]; | ||
| 336 | float error, best_error; | ||
| 337 | |||
| 338 |
2/2✓ Branch 0 taken 12040 times.
✓ Branch 1 taken 1204 times.
|
13244 | for (i = 0; i < LPC_ORDER; i++) { |
| 339 | 12040 | work[i] = ractx->curr_sblock[BLOCKSIZE + i]; | |
| 340 | 12040 | coefs[i] = lpc_coefs[i] * (1/4096.0); | |
| 341 | } | ||
| 342 | |||
| 343 | /** | ||
| 344 | * Calculate the zero-input response of the LPC filter and subtract it from | ||
| 345 | * input data. | ||
| 346 | */ | ||
| 347 | 1204 | ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE, | |
| 348 | LPC_ORDER); | ||
| 349 |
2/2✓ Branch 0 taken 48160 times.
✓ Branch 1 taken 1204 times.
|
49364 | for (i = 0; i < BLOCKSIZE; i++) { |
| 350 | 48160 | zero[i] = work[LPC_ORDER + i]; | |
| 351 | 48160 | data[i] = sblock_data[i] - zero[i]; | |
| 352 | } | ||
| 353 | |||
| 354 | /** | ||
| 355 | * Codebook search is performed without taking into account the contribution | ||
| 356 | * of the previous subblock, since it has been just subtracted from input | ||
| 357 | * data. | ||
| 358 | */ | ||
| 359 | 1204 | memset(work, 0, LPC_ORDER * sizeof(*work)); | |
| 360 | |||
| 361 | 1204 | cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs, | |
| 362 | data); | ||
| 363 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 7 times.
|
1204 | if (cba_idx) { |
| 364 | /** | ||
| 365 | * The filtered vector from the adaptive codebook can be retrieved from | ||
| 366 | * work, see implementation of adaptive_cb_search(). | ||
| 367 | */ | ||
| 368 | 1197 | memcpy(cba, work + LPC_ORDER, sizeof(cba)); | |
| 369 | |||
| 370 | 1197 | ff_copy_and_dup(ractx->buffer_a, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1); | |
| 371 | 1197 | m[0] = (ff_irms(&ractx->adsp, ractx->buffer_a) * rms) >> 12; | |
| 372 | } | ||
| 373 | 1204 | fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx); | |
| 374 |
2/2✓ Branch 0 taken 48160 times.
✓ Branch 1 taken 1204 times.
|
49364 | for (i = 0; i < BLOCKSIZE; i++) { |
| 375 | 48160 | cb1[i] = ff_cb1_vects[cb1_idx][i]; | |
| 376 | 48160 | cb2[i] = ff_cb2_vects[cb2_idx][i]; | |
| 377 | } | ||
| 378 | 1204 | ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE, | |
| 379 | LPC_ORDER); | ||
| 380 | 1204 | memcpy(cb1, work + LPC_ORDER, sizeof(cb1)); | |
| 381 | 1204 | m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8; | |
| 382 | 1204 | ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE, | |
| 383 | LPC_ORDER); | ||
| 384 | 1204 | memcpy(cb2, work + LPC_ORDER, sizeof(cb2)); | |
| 385 | 1204 | m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8; | |
| 386 | 1204 | best_error = FLT_MAX; | |
| 387 | 1204 | gain = 0; | |
| 388 |
2/2✓ Branch 0 taken 308224 times.
✓ Branch 1 taken 1204 times.
|
309428 | for (n = 0; n < 256; n++) { |
| 389 | 308224 | g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) * | |
| 390 | (1/4096.0); | ||
| 391 | 308224 | g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) * | |
| 392 | (1/4096.0); | ||
| 393 | 308224 | error = 0; | |
| 394 |
2/2✓ Branch 0 taken 306432 times.
✓ Branch 1 taken 1792 times.
|
308224 | if (cba_idx) { |
| 395 | 306432 | g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) * | |
| 396 | (1/4096.0); | ||
| 397 |
2/2✓ Branch 0 taken 12257280 times.
✓ Branch 1 taken 306432 times.
|
12563712 | for (i = 0; i < BLOCKSIZE; i++) { |
| 398 | 12257280 | data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] + | |
| 399 | 12257280 | g[2] * cb2[i]; | |
| 400 | 12257280 | error += (data[i] - sblock_data[i]) * | |
| 401 | 12257280 | (data[i] - sblock_data[i]); | |
| 402 | } | ||
| 403 | } else { | ||
| 404 |
2/2✓ Branch 0 taken 71680 times.
✓ Branch 1 taken 1792 times.
|
73472 | for (i = 0; i < BLOCKSIZE; i++) { |
| 405 | 71680 | data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i]; | |
| 406 | 71680 | error += (data[i] - sblock_data[i]) * | |
| 407 | 71680 | (data[i] - sblock_data[i]); | |
| 408 | } | ||
| 409 | } | ||
| 410 |
2/2✓ Branch 0 taken 45662 times.
✓ Branch 1 taken 262562 times.
|
308224 | if (error < best_error) { |
| 411 | 45662 | best_error = error; | |
| 412 | 45662 | gain = n; | |
| 413 | } | ||
| 414 | } | ||
| 415 | 1204 | put_bits(pb, 7, cba_idx); | |
| 416 | 1204 | put_bits(pb, 8, gain); | |
| 417 | 1204 | put_bits(pb, 7, cb1_idx); | |
| 418 | 1204 | put_bits(pb, 7, cb2_idx); | |
| 419 | 1204 | ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms, | |
| 420 | gain); | ||
| 421 | 1204 | } | |
| 422 | |||
| 423 | |||
| 424 | 302 | static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, | |
| 425 | const AVFrame *frame, int *got_packet_ptr) | ||
| 426 | { | ||
| 427 | static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4}; | ||
| 428 | static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2}; | ||
| 429 | 302 | RA144Context *ractx = avctx->priv_data; | |
| 430 | PutBitContext pb; | ||
| 431 | int32_t lpc_data[NBLOCKS * BLOCKSIZE]; | ||
| 432 | int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER]; | ||
| 433 | int shift[LPC_ORDER]; | ||
| 434 | int16_t block_coefs[NBLOCKS][LPC_ORDER]; | ||
| 435 | int lpc_refl[LPC_ORDER]; /**< reflection coefficients of the frame */ | ||
| 436 | unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */ | ||
| 437 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 2 times.
|
302 | const int16_t *samples = frame ? (const int16_t *)frame->data[0] : NULL; |
| 438 | 302 | int energy = 0; | |
| 439 | int i, idx, ret; | ||
| 440 | |||
| 441 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 301 times.
|
302 | if (ractx->last_frame) |
| 442 | 1 | return 0; | |
| 443 | |||
| 444 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 301 times.
|
301 | if ((ret = ff_get_encode_buffer(avctx, avpkt, FRAME_SIZE, 0)) < 0) |
| 445 | ✗ | return ret; | |
| 446 | |||
| 447 | /** | ||
| 448 | * Since the LPC coefficients are calculated on a frame centered over the | ||
| 449 | * fourth subframe, to encode a given frame, data from the next frame is | ||
| 450 | * needed. In each call to this function, the previous frame (whose data are | ||
| 451 | * saved in the encoder context) is encoded, and data from the current frame | ||
| 452 | * are saved in the encoder context to be used in the next function call. | ||
| 453 | */ | ||
| 454 |
2/2✓ Branch 0 taken 30100 times.
✓ Branch 1 taken 301 times.
|
30401 | for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) { |
| 455 | 30100 | lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i]; | |
| 456 | 30100 | energy += (lpc_data[i] * lpc_data[i]) >> 4; | |
| 457 | } | ||
| 458 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1 times.
|
301 | if (frame) { |
| 459 | int j; | ||
| 460 |
3/4✓ Branch 0 taken 18300 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18000 times.
✓ Branch 3 taken 300 times.
|
18300 | for (j = 0; j < frame->nb_samples && i < NBLOCKS * BLOCKSIZE; i++, j++) { |
| 461 | 18000 | lpc_data[i] = samples[j] >> 2; | |
| 462 | 18000 | energy += (lpc_data[i] * lpc_data[i]) >> 4; | |
| 463 | } | ||
| 464 | } | ||
| 465 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 300 times.
|
301 | if (i < NBLOCKS * BLOCKSIZE) |
| 466 | 1 | memset(&lpc_data[i], 0, (NBLOCKS * BLOCKSIZE - i) * sizeof(*lpc_data)); | |
| 467 | 301 | energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab, | |
| 468 | 32)]; | ||
| 469 | |||
| 470 | 301 | ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER, | |
| 471 | LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON, | ||
| 472 | 0, ORDER_METHOD_EST, 0, 12, 0); | ||
| 473 |
2/2✓ Branch 0 taken 3010 times.
✓ Branch 1 taken 301 times.
|
3311 | for (i = 0; i < LPC_ORDER; i++) |
| 474 | 3010 | block_coefs[NBLOCKS - 1][i] = -lpc_coefs[LPC_ORDER - 1][i] | |
| 475 | 3010 | * (1 << (12 - shift[LPC_ORDER - 1])); | |
| 476 | |||
| 477 | /** | ||
| 478 | * TODO: apply perceptual weighting of the input speech through bandwidth | ||
| 479 | * expansion of the LPC filter. | ||
| 480 | */ | ||
| 481 | |||
| 482 |
2/2✓ Branch 1 taken 73 times.
✓ Branch 2 taken 228 times.
|
301 | if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) { |
| 483 | /** | ||
| 484 | * The filter is unstable: use the coefficients of the previous frame. | ||
| 485 | */ | ||
| 486 | 73 | ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]); | |
| 487 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
|
73 | if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) { |
| 488 | /* the filter is still unstable. set reflection coeffs to zero. */ | ||
| 489 | ✗ | memset(lpc_refl, 0, sizeof(lpc_refl)); | |
| 490 | } | ||
| 491 | } | ||
| 492 | 301 | init_put_bits(&pb, avpkt->data, avpkt->size); | |
| 493 |
2/2✓ Branch 0 taken 3010 times.
✓ Branch 1 taken 301 times.
|
3311 | for (i = 0; i < LPC_ORDER; i++) { |
| 494 | 3010 | idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]); | |
| 495 | 3010 | put_bits(&pb, bit_sizes[i], idx); | |
| 496 | 3010 | lpc_refl[i] = ff_lpc_refl_cb[i][idx]; | |
| 497 | } | ||
| 498 | 301 | ractx->lpc_refl_rms[0] = ff_rms(lpc_refl); | |
| 499 | 301 | ff_eval_coefs(ractx->lpc_coef[0], lpc_refl); | |
| 500 | 301 | refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy); | |
| 501 | 602 | refl_rms[1] = ff_interp(ractx, block_coefs[1], 2, | |
| 502 | 301 | energy <= ractx->old_energy, | |
| 503 | 301 | ff_t_sqrt(energy * ractx->old_energy) >> 12); | |
| 504 | 301 | refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy); | |
| 505 | 301 | refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy); | |
| 506 | 301 | ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]); | |
| 507 | 301 | put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32)); | |
| 508 |
2/2✓ Branch 0 taken 1204 times.
✓ Branch 1 taken 301 times.
|
1505 | for (i = 0; i < NBLOCKS; i++) |
| 509 | 1204 | ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE, | |
| 510 | 1204 | block_coefs[i], refl_rms[i], &pb); | |
| 511 | 301 | flush_put_bits(&pb); | |
| 512 | 301 | ractx->old_energy = energy; | |
| 513 | 301 | ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0]; | |
| 514 | 301 | FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]); | |
| 515 | |||
| 516 | /* copy input samples to current block for processing in next call */ | ||
| 517 | 301 | i = 0; | |
| 518 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 1 times.
|
301 | if (frame) { |
| 519 |
2/2✓ Branch 0 taken 48000 times.
✓ Branch 1 taken 300 times.
|
48300 | for (; i < frame->nb_samples; i++) |
| 520 | 48000 | ractx->curr_block[i] = samples[i] >> 2; | |
| 521 | |||
| 522 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 300 times.
|
300 | if ((ret = ff_af_queue_add(&ractx->afq, frame)) < 0) |
| 523 | ✗ | return ret; | |
| 524 | } else | ||
| 525 | 1 | ractx->last_frame = 1; | |
| 526 | 301 | memset(&ractx->curr_block[i], 0, | |
| 527 | 301 | (NBLOCKS * BLOCKSIZE - i) * sizeof(*ractx->curr_block)); | |
| 528 | |||
| 529 | /* Get the next frame pts/duration */ | ||
| 530 | 301 | ff_af_queue_remove(&ractx->afq, avctx->frame_size, &avpkt->pts, | |
| 531 | &avpkt->duration); | ||
| 532 | |||
| 533 | 301 | *got_packet_ptr = 1; | |
| 534 | 301 | return 0; | |
| 535 | } | ||
| 536 | |||
| 537 | |||
| 538 | const FFCodec ff_ra_144_encoder = { | ||
| 539 | .p.name = "real_144", | ||
| 540 | CODEC_LONG_NAME("RealAudio 1.0 (14.4K)"), | ||
| 541 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 542 | .p.id = AV_CODEC_ID_RA_144, | ||
| 543 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | | ||
| 544 | AV_CODEC_CAP_SMALL_LAST_FRAME, | ||
| 545 | .priv_data_size = sizeof(RA144Context), | ||
| 546 | .init = ra144_encode_init, | ||
| 547 | FF_CODEC_ENCODE_CB(ra144_encode_frame), | ||
| 548 | .close = ra144_encode_close, | ||
| 549 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16), | ||
| 550 | CODEC_SAMPLERATES(8000), | ||
| 551 | CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO), | ||
| 552 | }; | ||
| 553 |