Libav 0.7.1
|
00001 /* 00002 * Real Audio 1.0 (14.4K) encoder 00003 * Copyright (c) 2010 Francesco Lavra <francescolavra@interfree.it> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include <float.h> 00029 00030 #include "avcodec.h" 00031 #include "put_bits.h" 00032 #include "celp_filters.h" 00033 #include "ra144.h" 00034 00035 00036 static av_cold int ra144_encode_init(AVCodecContext * avctx) 00037 { 00038 RA144Context *ractx; 00039 int ret; 00040 00041 if (avctx->sample_fmt != AV_SAMPLE_FMT_S16) { 00042 av_log(avctx, AV_LOG_ERROR, "invalid sample format\n"); 00043 return -1; 00044 } 00045 if (avctx->channels != 1) { 00046 av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", 00047 avctx->channels); 00048 return -1; 00049 } 00050 avctx->frame_size = NBLOCKS * BLOCKSIZE; 00051 avctx->bit_rate = 8000; 00052 ractx = avctx->priv_data; 00053 ractx->lpc_coef[0] = ractx->lpc_tables[0]; 00054 ractx->lpc_coef[1] = ractx->lpc_tables[1]; 00055 ractx->avctx = avctx; 00056 ret = ff_lpc_init(&ractx->lpc_ctx, avctx->frame_size, LPC_ORDER, 00057 FF_LPC_TYPE_LEVINSON); 00058 return ret; 00059 } 00060 00061 00062 static av_cold int ra144_encode_close(AVCodecContext *avctx) 00063 { 00064 RA144Context *ractx = avctx->priv_data; 00065 ff_lpc_end(&ractx->lpc_ctx); 00066 return 0; 00067 } 00068 00069 00080 static int quantize(int value, const int16_t *table, unsigned int size) 00081 { 00082 unsigned int low = 0, high = size - 1; 00083 00084 while (1) { 00085 int index = (low + high) >> 1; 00086 int error = table[index] - value; 00087 00088 if (index == low) 00089 return table[high] + error > value ? low : high; 00090 if (error > 0) { 00091 high = index; 00092 } else { 00093 low = index; 00094 } 00095 } 00096 } 00097 00098 00105 static void orthogonalize(float *v, const float *u) 00106 { 00107 int i; 00108 float num = 0, den = 0; 00109 00110 for (i = 0; i < BLOCKSIZE; i++) { 00111 num += v[i] * u[i]; 00112 den += u[i] * u[i]; 00113 } 00114 num /= den; 00115 for (i = 0; i < BLOCKSIZE; i++) 00116 v[i] -= num * u[i]; 00117 } 00118 00119 00133 static void get_match_score(float *work, const float *coefs, float *vect, 00134 const float *ortho1, const float *ortho2, 00135 const float *data, float *score, float *gain) 00136 { 00137 float c, g; 00138 int i; 00139 00140 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER); 00141 if (ortho1) 00142 orthogonalize(work, ortho1); 00143 if (ortho2) 00144 orthogonalize(work, ortho2); 00145 c = g = 0; 00146 for (i = 0; i < BLOCKSIZE; i++) { 00147 g += work[i] * work[i]; 00148 c += data[i] * work[i]; 00149 } 00150 if (c <= 0) { 00151 *score = 0; 00152 return; 00153 } 00154 *gain = c / g; 00155 *score = *gain * c; 00156 } 00157 00158 00166 static void create_adapt_vect(float *vect, const int16_t *cb, int lag) 00167 { 00168 int i; 00169 00170 cb += BUFFERSIZE - lag; 00171 for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++) 00172 vect[i] = cb[i]; 00173 if (lag < BLOCKSIZE) 00174 for (i = 0; i < BLOCKSIZE - lag; i++) 00175 vect[lag + i] = cb[i]; 00176 } 00177 00178 00189 static int adaptive_cb_search(const int16_t *adapt_cb, float *work, 00190 const float *coefs, float *data) 00191 { 00192 int i, best_vect; 00193 float score, gain, best_score, best_gain; 00194 float exc[BLOCKSIZE]; 00195 00196 gain = best_score = 0; 00197 for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) { 00198 create_adapt_vect(exc, adapt_cb, i); 00199 get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain); 00200 if (score > best_score) { 00201 best_score = score; 00202 best_vect = i; 00203 best_gain = gain; 00204 } 00205 } 00206 if (!best_score) 00207 return 0; 00208 00213 create_adapt_vect(exc, adapt_cb, best_vect); 00214 ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER); 00215 for (i = 0; i < BLOCKSIZE; i++) 00216 data[i] -= best_gain * work[i]; 00217 return (best_vect - BLOCKSIZE / 2 + 1); 00218 } 00219 00220 00237 static void find_best_vect(float *work, const float *coefs, 00238 const int8_t cb[][BLOCKSIZE], const float *ortho1, 00239 const float *ortho2, float *data, int *idx, 00240 float *gain) 00241 { 00242 int i, j; 00243 float g, score, best_score; 00244 float vect[BLOCKSIZE]; 00245 00246 *idx = *gain = best_score = 0; 00247 for (i = 0; i < FIXED_CB_SIZE; i++) { 00248 for (j = 0; j < BLOCKSIZE; j++) 00249 vect[j] = cb[i][j]; 00250 get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g); 00251 if (score > best_score) { 00252 best_score = score; 00253 *idx = i; 00254 *gain = g; 00255 } 00256 } 00257 } 00258 00259 00272 static void fixed_cb_search(float *work, const float *coefs, float *data, 00273 int cba_idx, int *cb1_idx, int *cb2_idx) 00274 { 00275 int i, ortho_cb1; 00276 float gain; 00277 float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE]; 00278 float vect[BLOCKSIZE]; 00279 00284 if (cba_idx) 00285 memcpy(cba_vect, work, sizeof(cba_vect)); 00286 00287 find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL, 00288 data, cb1_idx, &gain); 00289 00294 if (gain) { 00295 for (i = 0; i < BLOCKSIZE; i++) 00296 vect[i] = ff_cb1_vects[*cb1_idx][i]; 00297 ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER); 00298 if (cba_idx) 00299 orthogonalize(work, cba_vect); 00300 for (i = 0; i < BLOCKSIZE; i++) 00301 data[i] -= gain * work[i]; 00302 memcpy(cb1_vect, work, sizeof(cb1_vect)); 00303 ortho_cb1 = 1; 00304 } else 00305 ortho_cb1 = 0; 00306 00307 find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL, 00308 ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain); 00309 } 00310 00311 00321 static void ra144_encode_subblock(RA144Context *ractx, 00322 const int16_t *sblock_data, 00323 const int16_t *lpc_coefs, unsigned int rms, 00324 PutBitContext *pb) 00325 { 00326 float data[BLOCKSIZE], work[LPC_ORDER + BLOCKSIZE]; 00327 float coefs[LPC_ORDER]; 00328 float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE]; 00329 int16_t cba_vect[BLOCKSIZE]; 00330 int cba_idx, cb1_idx, cb2_idx, gain; 00331 int i, n, m[3]; 00332 float g[3]; 00333 float error, best_error; 00334 00335 for (i = 0; i < LPC_ORDER; i++) { 00336 work[i] = ractx->curr_sblock[BLOCKSIZE + i]; 00337 coefs[i] = lpc_coefs[i] * (1/4096.0); 00338 } 00339 00344 memset(data, 0, sizeof(data)); 00345 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE, 00346 LPC_ORDER); 00347 for (i = 0; i < BLOCKSIZE; i++) { 00348 zero[i] = work[LPC_ORDER + i]; 00349 data[i] = sblock_data[i] - zero[i]; 00350 } 00351 00357 memset(work, 0, LPC_ORDER * sizeof(*work)); 00358 00359 cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs, 00360 data); 00361 if (cba_idx) { 00366 memcpy(cba, work + LPC_ORDER, sizeof(cba)); 00367 00368 ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1); 00369 m[0] = (ff_irms(cba_vect) * rms) >> 12; 00370 } 00371 fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx); 00372 for (i = 0; i < BLOCKSIZE; i++) { 00373 cb1[i] = ff_cb1_vects[cb1_idx][i]; 00374 cb2[i] = ff_cb2_vects[cb2_idx][i]; 00375 } 00376 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE, 00377 LPC_ORDER); 00378 memcpy(cb1, work + LPC_ORDER, sizeof(cb1)); 00379 m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8; 00380 ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE, 00381 LPC_ORDER); 00382 memcpy(cb2, work + LPC_ORDER, sizeof(cb2)); 00383 m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8; 00384 best_error = FLT_MAX; 00385 gain = 0; 00386 for (n = 0; n < 256; n++) { 00387 g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) * 00388 (1/4096.0); 00389 g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) * 00390 (1/4096.0); 00391 error = 0; 00392 if (cba_idx) { 00393 g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) * 00394 (1/4096.0); 00395 for (i = 0; i < BLOCKSIZE; i++) { 00396 data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] + 00397 g[2] * cb2[i]; 00398 error += (data[i] - sblock_data[i]) * 00399 (data[i] - sblock_data[i]); 00400 } 00401 } else { 00402 for (i = 0; i < BLOCKSIZE; i++) { 00403 data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i]; 00404 error += (data[i] - sblock_data[i]) * 00405 (data[i] - sblock_data[i]); 00406 } 00407 } 00408 if (error < best_error) { 00409 best_error = error; 00410 gain = n; 00411 } 00412 } 00413 put_bits(pb, 7, cba_idx); 00414 put_bits(pb, 8, gain); 00415 put_bits(pb, 7, cb1_idx); 00416 put_bits(pb, 7, cb2_idx); 00417 ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms, 00418 gain); 00419 } 00420 00421 00422 static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame, 00423 int buf_size, void *data) 00424 { 00425 static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4}; 00426 static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2}; 00427 RA144Context *ractx; 00428 PutBitContext pb; 00429 int32_t lpc_data[NBLOCKS * BLOCKSIZE]; 00430 int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER]; 00431 int shift[LPC_ORDER]; 00432 int16_t block_coefs[NBLOCKS][LPC_ORDER]; 00433 int lpc_refl[LPC_ORDER]; 00434 unsigned int refl_rms[NBLOCKS]; 00435 int energy = 0; 00436 int i, idx; 00437 00438 if (buf_size < FRAMESIZE) { 00439 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n"); 00440 return 0; 00441 } 00442 ractx = avctx->priv_data; 00443 00451 for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) { 00452 lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i]; 00453 energy += (lpc_data[i] * lpc_data[i]) >> 4; 00454 } 00455 for (i = 2 * BLOCKSIZE + BLOCKSIZE / 2; i < NBLOCKS * BLOCKSIZE; i++) { 00456 lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >> 00457 2; 00458 energy += (lpc_data[i] * lpc_data[i]) >> 4; 00459 } 00460 energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab, 00461 32)]; 00462 00463 ff_lpc_calc_coefs(&ractx->lpc_ctx, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER, 00464 LPC_ORDER, 16, lpc_coefs, shift, FF_LPC_TYPE_LEVINSON, 00465 0, ORDER_METHOD_EST, 12, 0); 00466 for (i = 0; i < LPC_ORDER; i++) 00467 block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] << 00468 (12 - shift[LPC_ORDER - 1])); 00469 00475 if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) { 00479 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]); 00480 ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx); 00481 } 00482 init_put_bits(&pb, frame, buf_size); 00483 for (i = 0; i < LPC_ORDER; i++) { 00484 idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]); 00485 put_bits(&pb, bit_sizes[i], idx); 00486 lpc_refl[i] = ff_lpc_refl_cb[i][idx]; 00487 } 00488 ractx->lpc_refl_rms[0] = ff_rms(lpc_refl); 00489 ff_eval_coefs(ractx->lpc_coef[0], lpc_refl); 00490 refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy); 00491 refl_rms[1] = ff_interp(ractx, block_coefs[1], 2, 00492 energy <= ractx->old_energy, 00493 ff_t_sqrt(energy * ractx->old_energy) >> 12); 00494 refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy); 00495 refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy); 00496 ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]); 00497 put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32)); 00498 for (i = 0; i < NBLOCKS; i++) 00499 ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE, 00500 block_coefs[i], refl_rms[i], &pb); 00501 flush_put_bits(&pb); 00502 ractx->old_energy = energy; 00503 ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0]; 00504 FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]); 00505 for (i = 0; i < NBLOCKS * BLOCKSIZE; i++) 00506 ractx->curr_block[i] = *((int16_t *)data + i) >> 2; 00507 return FRAMESIZE; 00508 } 00509 00510 00511 AVCodec ff_ra_144_encoder = 00512 { 00513 "real_144", 00514 AVMEDIA_TYPE_AUDIO, 00515 CODEC_ID_RA_144, 00516 sizeof(RA144Context), 00517 ra144_encode_init, 00518 ra144_encode_frame, 00519 ra144_encode_close, 00520 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"), 00521 };