You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
17KB

  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. * @file
  23. * Real Audio 1.0 (14.4K) encoder
  24. * @author Francesco Lavra <francescolavra@interfree.it>
  25. */
  26. #include <float.h>
  27. #include "avcodec.h"
  28. #include "put_bits.h"
  29. #include "lpc.h"
  30. #include "celp_filters.h"
  31. #include "ra144.h"
  32. static av_cold int ra144_encode_init(AVCodecContext * avctx)
  33. {
  34. RA144Context *ractx;
  35. if (avctx->sample_fmt != SAMPLE_FMT_S16) {
  36. av_log(avctx, AV_LOG_ERROR, "invalid sample format\n");
  37. return -1;
  38. }
  39. if (avctx->channels != 1) {
  40. av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n",
  41. avctx->channels);
  42. return -1;
  43. }
  44. avctx->frame_size = NBLOCKS * BLOCKSIZE;
  45. avctx->bit_rate = 8000;
  46. ractx = avctx->priv_data;
  47. ractx->lpc_coef[0] = ractx->lpc_tables[0];
  48. ractx->lpc_coef[1] = ractx->lpc_tables[1];
  49. ractx->avctx = avctx;
  50. dsputil_init(&ractx->dsp, avctx);
  51. return 0;
  52. }
  53. /**
  54. * Quantize a value by searching a sorted table for the element with the
  55. * nearest value
  56. *
  57. * @param value value to quantize
  58. * @param table array containing the quantization table
  59. * @param size size of the quantization table
  60. * @return index of the quantization table corresponding to the element with the
  61. * nearest value
  62. */
  63. static int quantize(int value, const int16_t *table, unsigned int size)
  64. {
  65. unsigned int low = 0, high = size - 1;
  66. while (1) {
  67. int index = (low + high) >> 1;
  68. int error = table[index] - value;
  69. if (index == low)
  70. return table[high] + error > value ? low : high;
  71. if (error > 0) {
  72. high = index;
  73. } else {
  74. low = index;
  75. }
  76. }
  77. }
  78. /**
  79. * Orthogonalize a vector to another vector
  80. *
  81. * @param v vector to orthogonalize
  82. * @param u vector against which orthogonalization is performed
  83. */
  84. static void orthogonalize(float *v, const float *u)
  85. {
  86. int i;
  87. float num = 0, den = 0;
  88. for (i = 0; i < BLOCKSIZE; i++) {
  89. num += v[i] * u[i];
  90. den += u[i] * u[i];
  91. }
  92. num /= den;
  93. for (i = 0; i < BLOCKSIZE; i++)
  94. v[i] -= num * u[i];
  95. }
  96. /**
  97. * Calculate match score and gain of an LPC-filtered vector with respect to
  98. * input data, possibly othogonalizing it to up to 2 other vectors
  99. *
  100. * @param work array used to calculate the filtered vector
  101. * @param coefs coefficients of the LPC filter
  102. * @param vect original vector
  103. * @param ortho1 first vector against which orthogonalization is performed
  104. * @param ortho2 second vector against which orthogonalization is performed
  105. * @param data input data
  106. * @param score pointer to variable where match score is returned
  107. * @param gain pointer to variable where gain is returned
  108. */
  109. static void get_match_score(float *work, const float *coefs, float *vect,
  110. const float *ortho1, const float *ortho2,
  111. const float *data, float *score, float *gain)
  112. {
  113. float c, g;
  114. int i;
  115. ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
  116. if (ortho1)
  117. orthogonalize(work, ortho1);
  118. if (ortho2)
  119. orthogonalize(work, ortho2);
  120. c = g = 0;
  121. for (i = 0; i < BLOCKSIZE; i++) {
  122. g += work[i] * work[i];
  123. c += data[i] * work[i];
  124. }
  125. if (c <= 0) {
  126. *score = 0;
  127. return;
  128. }
  129. *gain = c / g;
  130. *score = *gain * c;
  131. }
  132. /**
  133. * Create a vector from the adaptive codebook at a given lag value
  134. *
  135. * @param vect array where vector is stored
  136. * @param cb adaptive codebook
  137. * @param lag lag value
  138. */
  139. static void create_adapt_vect(float *vect, const int16_t *cb, int lag)
  140. {
  141. int i;
  142. cb += BUFFERSIZE - lag;
  143. for (i = 0; i < FFMIN(BLOCKSIZE, lag); i++)
  144. vect[i] = cb[i];
  145. if (lag < BLOCKSIZE)
  146. for (i = 0; i < BLOCKSIZE - lag; i++)
  147. vect[lag + i] = cb[i];
  148. }
  149. /**
  150. * Search the adaptive codebook for the best entry and gain and remove its
  151. * contribution from input data
  152. *
  153. * @param adapt_cb array from which the adaptive codebook is extracted
  154. * @param work array used to calculate LPC-filtered vectors
  155. * @param coefs coefficients of the LPC filter
  156. * @param data input data
  157. * @return index of the best entry of the adaptive codebook
  158. */
  159. static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
  160. const float *coefs, float *data)
  161. {
  162. int i, best_vect;
  163. float score, gain, best_score, best_gain;
  164. float exc[BLOCKSIZE];
  165. gain = best_score = 0;
  166. for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
  167. create_adapt_vect(exc, adapt_cb, i);
  168. get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
  169. if (score > best_score) {
  170. best_score = score;
  171. best_vect = i;
  172. best_gain = gain;
  173. }
  174. }
  175. if (!best_score)
  176. return 0;
  177. /**
  178. * Re-calculate the filtered vector from the vector with maximum match score
  179. * and remove its contribution from input data.
  180. */
  181. create_adapt_vect(exc, adapt_cb, best_vect);
  182. ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
  183. for (i = 0; i < BLOCKSIZE; i++)
  184. data[i] -= best_gain * work[i];
  185. return (best_vect - BLOCKSIZE / 2 + 1);
  186. }
  187. /**
  188. * Find the best vector of a fixed codebook by applying an LPC filter to
  189. * codebook entries, possibly othogonalizing them to up to 2 other vectors and
  190. * matching the results with input data
  191. *
  192. * @param work array used to calculate the filtered vectors
  193. * @param coefs coefficients of the LPC filter
  194. * @param cb fixed codebook
  195. * @param ortho1 first vector against which orthogonalization is performed
  196. * @param ortho2 second vector against which orthogonalization is performed
  197. * @param data input data
  198. * @param idx pointer to variable where the index of the best codebook entry is
  199. * returned
  200. * @param gain pointer to variable where the gain of the best codebook entry is
  201. * returned
  202. */
  203. static void find_best_vect(float *work, const float *coefs,
  204. const int8_t cb[][BLOCKSIZE], const float *ortho1,
  205. const float *ortho2, float *data, int *idx,
  206. float *gain)
  207. {
  208. int i, j;
  209. float g, score, best_score;
  210. float vect[BLOCKSIZE];
  211. *idx = *gain = best_score = 0;
  212. for (i = 0; i < FIXED_CB_SIZE; i++) {
  213. for (j = 0; j < BLOCKSIZE; j++)
  214. vect[j] = cb[i][j];
  215. get_match_score(work, coefs, vect, ortho1, ortho2, data, &score, &g);
  216. if (score > best_score) {
  217. best_score = score;
  218. *idx = i;
  219. *gain = g;
  220. }
  221. }
  222. }
  223. /**
  224. * Search the two fixed codebooks for the best entry and gain
  225. *
  226. * @param work array used to calculate LPC-filtered vectors
  227. * @param coefs coefficients of the LPC filter
  228. * @param data input data
  229. * @param cba_idx index of the best entry of the adaptive codebook
  230. * @param cb1_idx pointer to variable where the index of the best entry of the
  231. * first fixed codebook is returned
  232. * @param cb2_idx pointer to variable where the index of the best entry of the
  233. * second fixed codebook is returned
  234. */
  235. static void fixed_cb_search(float *work, const float *coefs, float *data,
  236. int cba_idx, int *cb1_idx, int *cb2_idx)
  237. {
  238. int i, ortho_cb1;
  239. float gain;
  240. float cba_vect[BLOCKSIZE], cb1_vect[BLOCKSIZE];
  241. float vect[BLOCKSIZE];
  242. /**
  243. * The filtered vector from the adaptive codebook can be retrieved from
  244. * work, because this function is called just after adaptive_cb_search().
  245. */
  246. if (cba_idx)
  247. memcpy(cba_vect, work, sizeof(cba_vect));
  248. find_best_vect(work, coefs, ff_cb1_vects, cba_idx ? cba_vect : NULL, NULL,
  249. data, cb1_idx, &gain);
  250. /**
  251. * Re-calculate the filtered vector from the vector with maximum match score
  252. * and remove its contribution from input data.
  253. */
  254. if (gain) {
  255. for (i = 0; i < BLOCKSIZE; i++)
  256. vect[i] = ff_cb1_vects[*cb1_idx][i];
  257. ff_celp_lp_synthesis_filterf(work, coefs, vect, BLOCKSIZE, LPC_ORDER);
  258. if (cba_idx)
  259. orthogonalize(work, cba_vect);
  260. for (i = 0; i < BLOCKSIZE; i++)
  261. data[i] -= gain * work[i];
  262. memcpy(cb1_vect, work, sizeof(cb1_vect));
  263. ortho_cb1 = 1;
  264. } else
  265. ortho_cb1 = 0;
  266. find_best_vect(work, coefs, ff_cb2_vects, cba_idx ? cba_vect : NULL,
  267. ortho_cb1 ? cb1_vect : NULL, data, cb2_idx, &gain);
  268. }
  269. /**
  270. * Encode a subblock of the current frame
  271. *
  272. * @param ractx encoder context
  273. * @param sblock_data input data of the subblock
  274. * @param lpc_coefs coefficients of the LPC filter
  275. * @param rms RMS of the reflection coefficients
  276. * @param pb pointer to PutBitContext of the current frame
  277. */
  278. static void ra144_encode_subblock(RA144Context *ractx,
  279. const int16_t *sblock_data,
  280. const int16_t *lpc_coefs, unsigned int rms,
  281. PutBitContext *pb)
  282. {
  283. float data[BLOCKSIZE], work[LPC_ORDER + BLOCKSIZE];
  284. float coefs[LPC_ORDER];
  285. float zero[BLOCKSIZE], cba[BLOCKSIZE], cb1[BLOCKSIZE], cb2[BLOCKSIZE];
  286. int16_t cba_vect[BLOCKSIZE];
  287. int cba_idx, cb1_idx, cb2_idx, gain;
  288. int i, n, m[3];
  289. float g[3];
  290. float error, best_error;
  291. for (i = 0; i < LPC_ORDER; i++) {
  292. work[i] = ractx->curr_sblock[BLOCKSIZE + i];
  293. coefs[i] = lpc_coefs[i] * (1/4096.0);
  294. }
  295. /**
  296. * Calculate the zero-input response of the LPC filter and subtract it from
  297. * input data.
  298. */
  299. memset(data, 0, sizeof(data));
  300. ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, data, BLOCKSIZE,
  301. LPC_ORDER);
  302. for (i = 0; i < BLOCKSIZE; i++) {
  303. zero[i] = work[LPC_ORDER + i];
  304. data[i] = sblock_data[i] - zero[i];
  305. }
  306. /**
  307. * Codebook search is performed without taking into account the contribution
  308. * of the previous subblock, since it has been just subtracted from input
  309. * data.
  310. */
  311. memset(work, 0, LPC_ORDER * sizeof(*work));
  312. cba_idx = adaptive_cb_search(ractx->adapt_cb, work + LPC_ORDER, coefs,
  313. data);
  314. if (cba_idx) {
  315. /**
  316. * The filtered vector from the adaptive codebook can be retrieved from
  317. * work, see implementation of adaptive_cb_search().
  318. */
  319. memcpy(cba, work + LPC_ORDER, sizeof(cba));
  320. ff_copy_and_dup(cba_vect, ractx->adapt_cb, cba_idx + BLOCKSIZE / 2 - 1);
  321. m[0] = (ff_irms(cba_vect) * rms) >> 12;
  322. }
  323. fixed_cb_search(work + LPC_ORDER, coefs, data, cba_idx, &cb1_idx, &cb2_idx);
  324. for (i = 0; i < BLOCKSIZE; i++) {
  325. cb1[i] = ff_cb1_vects[cb1_idx][i];
  326. cb2[i] = ff_cb2_vects[cb2_idx][i];
  327. }
  328. ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb1, BLOCKSIZE,
  329. LPC_ORDER);
  330. memcpy(cb1, work + LPC_ORDER, sizeof(cb1));
  331. m[1] = (ff_cb1_base[cb1_idx] * rms) >> 8;
  332. ff_celp_lp_synthesis_filterf(work + LPC_ORDER, coefs, cb2, BLOCKSIZE,
  333. LPC_ORDER);
  334. memcpy(cb2, work + LPC_ORDER, sizeof(cb2));
  335. m[2] = (ff_cb2_base[cb2_idx] * rms) >> 8;
  336. best_error = FLT_MAX;
  337. gain = 0;
  338. for (n = 0; n < 256; n++) {
  339. g[1] = ((ff_gain_val_tab[n][1] * m[1]) >> ff_gain_exp_tab[n]) *
  340. (1/4096.0);
  341. g[2] = ((ff_gain_val_tab[n][2] * m[2]) >> ff_gain_exp_tab[n]) *
  342. (1/4096.0);
  343. error = 0;
  344. if (cba_idx) {
  345. g[0] = ((ff_gain_val_tab[n][0] * m[0]) >> ff_gain_exp_tab[n]) *
  346. (1/4096.0);
  347. for (i = 0; i < BLOCKSIZE; i++) {
  348. data[i] = zero[i] + g[0] * cba[i] + g[1] * cb1[i] +
  349. g[2] * cb2[i];
  350. error += (data[i] - sblock_data[i]) *
  351. (data[i] - sblock_data[i]);
  352. }
  353. } else {
  354. for (i = 0; i < BLOCKSIZE; i++) {
  355. data[i] = zero[i] + g[1] * cb1[i] + g[2] * cb2[i];
  356. error += (data[i] - sblock_data[i]) *
  357. (data[i] - sblock_data[i]);
  358. }
  359. }
  360. if (error < best_error) {
  361. best_error = error;
  362. gain = n;
  363. }
  364. }
  365. put_bits(pb, 7, cba_idx);
  366. put_bits(pb, 8, gain);
  367. put_bits(pb, 7, cb1_idx);
  368. put_bits(pb, 7, cb2_idx);
  369. ff_subblock_synthesis(ractx, lpc_coefs, cba_idx, cb1_idx, cb2_idx, rms,
  370. gain);
  371. }
  372. static int ra144_encode_frame(AVCodecContext *avctx, uint8_t *frame,
  373. int buf_size, void *data)
  374. {
  375. static const uint8_t sizes[LPC_ORDER] = {64, 32, 32, 16, 16, 8, 8, 8, 8, 4};
  376. static const uint8_t bit_sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
  377. RA144Context *ractx;
  378. PutBitContext pb;
  379. int32_t lpc_data[NBLOCKS * BLOCKSIZE];
  380. int32_t lpc_coefs[LPC_ORDER][MAX_LPC_ORDER];
  381. int shift[LPC_ORDER];
  382. int16_t block_coefs[NBLOCKS][LPC_ORDER];
  383. int lpc_refl[LPC_ORDER]; /**< reflection coefficients of the frame */
  384. unsigned int refl_rms[NBLOCKS]; /**< RMS of the reflection coefficients */
  385. int energy = 0;
  386. int i, idx;
  387. if (buf_size < FRAMESIZE) {
  388. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  389. return 0;
  390. }
  391. ractx = avctx->priv_data;
  392. /**
  393. * Since the LPC coefficients are calculated on a frame centered over the
  394. * fourth subframe, to encode a given frame, data from the next frame is
  395. * needed. In each call to this function, the previous frame (whose data are
  396. * saved in the encoder context) is encoded, and data from the current frame
  397. * are saved in the encoder context to be used in the next function call.
  398. */
  399. for (i = 0; i < (2 * BLOCKSIZE + BLOCKSIZE / 2); i++) {
  400. lpc_data[i] = ractx->curr_block[BLOCKSIZE + BLOCKSIZE / 2 + i];
  401. energy += (lpc_data[i] * lpc_data[i]) >> 4;
  402. }
  403. for (i = 2 * BLOCKSIZE + BLOCKSIZE / 2; i < NBLOCKS * BLOCKSIZE; i++) {
  404. lpc_data[i] = *((int16_t *)data + i - 2 * BLOCKSIZE - BLOCKSIZE / 2) >>
  405. 2;
  406. energy += (lpc_data[i] * lpc_data[i]) >> 4;
  407. }
  408. energy = ff_energy_tab[quantize(ff_t_sqrt(energy >> 5) >> 10, ff_energy_tab,
  409. 32)];
  410. ff_lpc_calc_coefs(&ractx->dsp, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER,
  411. LPC_ORDER, 16, lpc_coefs, shift, 1, ORDER_METHOD_EST, 12,
  412. 0);
  413. for (i = 0; i < LPC_ORDER; i++)
  414. block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] <<
  415. (12 - shift[LPC_ORDER - 1]));
  416. /**
  417. * TODO: apply perceptual weighting of the input speech through bandwidth
  418. * expansion of the LPC filter.
  419. */
  420. if (ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx)) {
  421. /**
  422. * The filter is unstable: use the coefficients of the previous frame.
  423. */
  424. ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[1]);
  425. ff_eval_refl(lpc_refl, block_coefs[NBLOCKS - 1], avctx);
  426. }
  427. init_put_bits(&pb, frame, buf_size);
  428. for (i = 0; i < LPC_ORDER; i++) {
  429. idx = quantize(lpc_refl[i], ff_lpc_refl_cb[i], sizes[i]);
  430. put_bits(&pb, bit_sizes[i], idx);
  431. lpc_refl[i] = ff_lpc_refl_cb[i][idx];
  432. }
  433. ractx->lpc_refl_rms[0] = ff_rms(lpc_refl);
  434. ff_eval_coefs(ractx->lpc_coef[0], lpc_refl);
  435. refl_rms[0] = ff_interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);
  436. refl_rms[1] = ff_interp(ractx, block_coefs[1], 2,
  437. energy <= ractx->old_energy,
  438. ff_t_sqrt(energy * ractx->old_energy) >> 12);
  439. refl_rms[2] = ff_interp(ractx, block_coefs[2], 3, 0, energy);
  440. refl_rms[3] = ff_rescale_rms(ractx->lpc_refl_rms[0], energy);
  441. ff_int_to_int16(block_coefs[NBLOCKS - 1], ractx->lpc_coef[0]);
  442. put_bits(&pb, 5, quantize(energy, ff_energy_tab, 32));
  443. for (i = 0; i < NBLOCKS; i++)
  444. ra144_encode_subblock(ractx, ractx->curr_block + i * BLOCKSIZE,
  445. block_coefs[i], refl_rms[i], &pb);
  446. flush_put_bits(&pb);
  447. ractx->old_energy = energy;
  448. ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];
  449. FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);
  450. for (i = 0; i < NBLOCKS * BLOCKSIZE; i++)
  451. ractx->curr_block[i] = *((int16_t *)data + i) >> 2;
  452. return FRAMESIZE;
  453. }
  454. AVCodec ra_144_encoder =
  455. {
  456. "real_144",
  457. CODEC_TYPE_AUDIO,
  458. CODEC_ID_RA_144,
  459. sizeof(RA144Context),
  460. ra144_encode_init,
  461. ra144_encode_frame,
  462. .long_name = NULL_IF_CONFIG_SMALL("RealAudio 1.0 (14.4K) encoder"),
  463. };