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.

563 lines
18KB

  1. /*
  2. * SIPR / ACELP.NET decoder
  3. *
  4. * Copyright (c) 2008 Vladimir Voroshilov
  5. * Copyright (c) 2009 Vitor Sessak
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <math.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "libavutil/mathematics.h"
  27. #include "avcodec.h"
  28. #define ALT_BITSTREAM_READER_LE
  29. #include "get_bits.h"
  30. #include "dsputil.h"
  31. #include "lsp.h"
  32. #include "celp_math.h"
  33. #include "acelp_vectors.h"
  34. #include "acelp_pitch_delay.h"
  35. #include "acelp_filters.h"
  36. #include "celp_filters.h"
  37. #define MAX_SUBFRAME_COUNT 5
  38. #include "sipr.h"
  39. #include "siprdata.h"
  40. typedef struct {
  41. const char *mode_name;
  42. uint16_t bits_per_frame;
  43. uint8_t subframe_count;
  44. uint8_t frames_per_packet;
  45. float pitch_sharp_factor;
  46. /* bitstream parameters */
  47. uint8_t number_of_fc_indexes;
  48. uint8_t ma_predictor_bits; ///< size in bits of the switched MA predictor
  49. /** size in bits of the i-th stage vector of quantizer */
  50. uint8_t vq_indexes_bits[5];
  51. /** size in bits of the adaptive-codebook index for every subframe */
  52. uint8_t pitch_delay_bits[5];
  53. uint8_t gp_index_bits;
  54. uint8_t fc_index_bits[10]; ///< size in bits of the fixed codebook indexes
  55. uint8_t gc_index_bits; ///< size in bits of the gain codebook indexes
  56. } SiprModeParam;
  57. static const SiprModeParam modes[MODE_COUNT] = {
  58. [MODE_16k] = {
  59. .mode_name = "16k",
  60. .bits_per_frame = 160,
  61. .subframe_count = SUBFRAME_COUNT_16k,
  62. .frames_per_packet = 1,
  63. .pitch_sharp_factor = 0.00,
  64. .number_of_fc_indexes = 10,
  65. .ma_predictor_bits = 1,
  66. .vq_indexes_bits = {7, 8, 7, 7, 7},
  67. .pitch_delay_bits = {9, 6},
  68. .gp_index_bits = 4,
  69. .fc_index_bits = {4, 5, 4, 5, 4, 5, 4, 5, 4, 5},
  70. .gc_index_bits = 5
  71. },
  72. [MODE_8k5] = {
  73. .mode_name = "8k5",
  74. .bits_per_frame = 152,
  75. .subframe_count = 3,
  76. .frames_per_packet = 1,
  77. .pitch_sharp_factor = 0.8,
  78. .number_of_fc_indexes = 3,
  79. .ma_predictor_bits = 0,
  80. .vq_indexes_bits = {6, 7, 7, 7, 5},
  81. .pitch_delay_bits = {8, 5, 5},
  82. .gp_index_bits = 0,
  83. .fc_index_bits = {9, 9, 9},
  84. .gc_index_bits = 7
  85. },
  86. [MODE_6k5] = {
  87. .mode_name = "6k5",
  88. .bits_per_frame = 232,
  89. .subframe_count = 3,
  90. .frames_per_packet = 2,
  91. .pitch_sharp_factor = 0.8,
  92. .number_of_fc_indexes = 3,
  93. .ma_predictor_bits = 0,
  94. .vq_indexes_bits = {6, 7, 7, 7, 5},
  95. .pitch_delay_bits = {8, 5, 5},
  96. .gp_index_bits = 0,
  97. .fc_index_bits = {5, 5, 5},
  98. .gc_index_bits = 7
  99. },
  100. [MODE_5k0] = {
  101. .mode_name = "5k0",
  102. .bits_per_frame = 296,
  103. .subframe_count = 5,
  104. .frames_per_packet = 2,
  105. .pitch_sharp_factor = 0.85,
  106. .number_of_fc_indexes = 1,
  107. .ma_predictor_bits = 0,
  108. .vq_indexes_bits = {6, 7, 7, 7, 5},
  109. .pitch_delay_bits = {8, 5, 8, 5, 5},
  110. .gp_index_bits = 0,
  111. .fc_index_bits = {10},
  112. .gc_index_bits = 7
  113. }
  114. };
  115. const float ff_pow_0_5[] = {
  116. 1.0/(1 << 1), 1.0/(1 << 2), 1.0/(1 << 3), 1.0/(1 << 4),
  117. 1.0/(1 << 5), 1.0/(1 << 6), 1.0/(1 << 7), 1.0/(1 << 8),
  118. 1.0/(1 << 9), 1.0/(1 << 10), 1.0/(1 << 11), 1.0/(1 << 12),
  119. 1.0/(1 << 13), 1.0/(1 << 14), 1.0/(1 << 15), 1.0/(1 << 16)
  120. };
  121. static void dequant(float *out, const int *idx, const float *cbs[])
  122. {
  123. int i;
  124. int stride = 2;
  125. int num_vec = 5;
  126. for (i = 0; i < num_vec; i++)
  127. memcpy(out + stride*i, cbs[i] + stride*idx[i], stride*sizeof(float));
  128. }
  129. static void lsf_decode_fp(float *lsfnew, float *lsf_history,
  130. const SiprParameters *parm)
  131. {
  132. int i;
  133. float lsf_tmp[LP_FILTER_ORDER];
  134. dequant(lsf_tmp, parm->vq_indexes, lsf_codebooks);
  135. for (i = 0; i < LP_FILTER_ORDER; i++)
  136. lsfnew[i] = lsf_history[i] * 0.33 + lsf_tmp[i] + mean_lsf[i];
  137. ff_sort_nearly_sorted_floats(lsfnew, LP_FILTER_ORDER - 1);
  138. /* Note that a minimum distance is not enforced between the last value and
  139. the previous one, contrary to what is done in ff_acelp_reorder_lsf() */
  140. ff_set_min_dist_lsf(lsfnew, LSFQ_DIFF_MIN, LP_FILTER_ORDER - 1);
  141. lsfnew[9] = FFMIN(lsfnew[LP_FILTER_ORDER - 1], 1.3 * M_PI);
  142. memcpy(lsf_history, lsf_tmp, LP_FILTER_ORDER * sizeof(*lsf_history));
  143. for (i = 0; i < LP_FILTER_ORDER - 1; i++)
  144. lsfnew[i] = cos(lsfnew[i]);
  145. lsfnew[LP_FILTER_ORDER - 1] *= 6.153848 / M_PI;
  146. }
  147. /** Apply pitch lag to the fixed vector (AMR section 6.1.2). */
  148. static void pitch_sharpening(int pitch_lag_int, float beta,
  149. float *fixed_vector)
  150. {
  151. int i;
  152. for (i = pitch_lag_int; i < SUBFR_SIZE; i++)
  153. fixed_vector[i] += beta * fixed_vector[i - pitch_lag_int];
  154. }
  155. /**
  156. * Extract decoding parameters from the input bitstream.
  157. * @param parms parameters structure
  158. * @param pgb pointer to initialized GetBitContext structure
  159. */
  160. static void decode_parameters(SiprParameters* parms, GetBitContext *pgb,
  161. const SiprModeParam *p)
  162. {
  163. int i, j;
  164. parms->ma_pred_switch = get_bits(pgb, p->ma_predictor_bits);
  165. for (i = 0; i < 5; i++)
  166. parms->vq_indexes[i] = get_bits(pgb, p->vq_indexes_bits[i]);
  167. for (i = 0; i < p->subframe_count; i++) {
  168. parms->pitch_delay[i] = get_bits(pgb, p->pitch_delay_bits[i]);
  169. parms->gp_index[i] = get_bits(pgb, p->gp_index_bits);
  170. for (j = 0; j < p->number_of_fc_indexes; j++)
  171. parms->fc_indexes[i][j] = get_bits(pgb, p->fc_index_bits[j]);
  172. parms->gc_index[i] = get_bits(pgb, p->gc_index_bits);
  173. }
  174. }
  175. static void sipr_decode_lp(float *lsfnew, const float *lsfold, float *Az,
  176. int num_subfr)
  177. {
  178. double lsfint[LP_FILTER_ORDER];
  179. int i,j;
  180. float t, t0 = 1.0 / num_subfr;
  181. t = t0 * 0.5;
  182. for (i = 0; i < num_subfr; i++) {
  183. for (j = 0; j < LP_FILTER_ORDER; j++)
  184. lsfint[j] = lsfold[j] * (1 - t) + t * lsfnew[j];
  185. ff_amrwb_lsp2lpc(lsfint, Az, LP_FILTER_ORDER);
  186. Az += LP_FILTER_ORDER;
  187. t += t0;
  188. }
  189. }
  190. /**
  191. * Evaluate the adaptive impulse response.
  192. */
  193. static void eval_ir(const float *Az, int pitch_lag, float *freq,
  194. float pitch_sharp_factor)
  195. {
  196. float tmp1[SUBFR_SIZE+1], tmp2[LP_FILTER_ORDER+1];
  197. int i;
  198. tmp1[0] = 1.;
  199. for (i = 0; i < LP_FILTER_ORDER; i++) {
  200. tmp1[i+1] = Az[i] * ff_pow_0_55[i];
  201. tmp2[i ] = Az[i] * ff_pow_0_7 [i];
  202. }
  203. memset(tmp1 + 11, 0, 37 * sizeof(float));
  204. ff_celp_lp_synthesis_filterf(freq, tmp2, tmp1, SUBFR_SIZE,
  205. LP_FILTER_ORDER);
  206. pitch_sharpening(pitch_lag, pitch_sharp_factor, freq);
  207. }
  208. /**
  209. * Evaluate the convolution of a vector with a sparse vector.
  210. */
  211. static void convolute_with_sparse(float *out, const AMRFixed *pulses,
  212. const float *shape, int length)
  213. {
  214. int i, j;
  215. memset(out, 0, length*sizeof(float));
  216. for (i = 0; i < pulses->n; i++)
  217. for (j = pulses->x[i]; j < length; j++)
  218. out[j] += pulses->y[i] * shape[j - pulses->x[i]];
  219. }
  220. /**
  221. * Apply postfilter, very similar to AMR one.
  222. */
  223. static void postfilter_5k0(SiprContext *ctx, const float *lpc, float *samples)
  224. {
  225. float buf[SUBFR_SIZE + LP_FILTER_ORDER];
  226. float *pole_out = buf + LP_FILTER_ORDER;
  227. float lpc_n[LP_FILTER_ORDER];
  228. float lpc_d[LP_FILTER_ORDER];
  229. int i;
  230. for (i = 0; i < LP_FILTER_ORDER; i++) {
  231. lpc_d[i] = lpc[i] * ff_pow_0_75[i];
  232. lpc_n[i] = lpc[i] * ff_pow_0_5 [i];
  233. };
  234. memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem,
  235. LP_FILTER_ORDER*sizeof(float));
  236. ff_celp_lp_synthesis_filterf(pole_out, lpc_d, samples, SUBFR_SIZE,
  237. LP_FILTER_ORDER);
  238. memcpy(ctx->postfilter_mem, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
  239. LP_FILTER_ORDER*sizeof(float));
  240. ff_tilt_compensation(&ctx->tilt_mem, 0.4, pole_out, SUBFR_SIZE);
  241. memcpy(pole_out - LP_FILTER_ORDER, ctx->postfilter_mem5k0,
  242. LP_FILTER_ORDER*sizeof(*pole_out));
  243. memcpy(ctx->postfilter_mem5k0, pole_out + SUBFR_SIZE - LP_FILTER_ORDER,
  244. LP_FILTER_ORDER*sizeof(*pole_out));
  245. ff_celp_lp_zero_synthesis_filterf(samples, lpc_n, pole_out, SUBFR_SIZE,
  246. LP_FILTER_ORDER);
  247. }
  248. static void decode_fixed_sparse(AMRFixed *fixed_sparse, const int16_t *pulses,
  249. SiprMode mode, int low_gain)
  250. {
  251. int i;
  252. switch (mode) {
  253. case MODE_6k5:
  254. for (i = 0; i < 3; i++) {
  255. fixed_sparse->x[i] = 3 * (pulses[i] & 0xf) + i;
  256. fixed_sparse->y[i] = pulses[i] & 0x10 ? -1 : 1;
  257. }
  258. fixed_sparse->n = 3;
  259. break;
  260. case MODE_8k5:
  261. for (i = 0; i < 3; i++) {
  262. fixed_sparse->x[2*i ] = 3 * ((pulses[i] >> 4) & 0xf) + i;
  263. fixed_sparse->x[2*i + 1] = 3 * ( pulses[i] & 0xf) + i;
  264. fixed_sparse->y[2*i ] = (pulses[i] & 0x100) ? -1.0: 1.0;
  265. fixed_sparse->y[2*i + 1] =
  266. (fixed_sparse->x[2*i + 1] < fixed_sparse->x[2*i]) ?
  267. -fixed_sparse->y[2*i ] : fixed_sparse->y[2*i];
  268. }
  269. fixed_sparse->n = 6;
  270. break;
  271. case MODE_5k0:
  272. default:
  273. if (low_gain) {
  274. int offset = (pulses[0] & 0x200) ? 2 : 0;
  275. int val = pulses[0];
  276. for (i = 0; i < 3; i++) {
  277. int index = (val & 0x7) * 6 + 4 - i*2;
  278. fixed_sparse->y[i] = (offset + index) & 0x3 ? -1 : 1;
  279. fixed_sparse->x[i] = index;
  280. val >>= 3;
  281. }
  282. fixed_sparse->n = 3;
  283. } else {
  284. int pulse_subset = (pulses[0] >> 8) & 1;
  285. fixed_sparse->x[0] = ((pulses[0] >> 4) & 15) * 3 + pulse_subset;
  286. fixed_sparse->x[1] = ( pulses[0] & 15) * 3 + pulse_subset + 1;
  287. fixed_sparse->y[0] = pulses[0] & 0x200 ? -1 : 1;
  288. fixed_sparse->y[1] = -fixed_sparse->y[0];
  289. fixed_sparse->n = 2;
  290. }
  291. break;
  292. }
  293. }
  294. static void decode_frame(SiprContext *ctx, SiprParameters *params,
  295. float *out_data)
  296. {
  297. int i, j;
  298. int subframe_count = modes[ctx->mode].subframe_count;
  299. int frame_size = subframe_count * SUBFR_SIZE;
  300. float Az[LP_FILTER_ORDER * MAX_SUBFRAME_COUNT];
  301. float *excitation;
  302. float ir_buf[SUBFR_SIZE + LP_FILTER_ORDER];
  303. float lsf_new[LP_FILTER_ORDER];
  304. float *impulse_response = ir_buf + LP_FILTER_ORDER;
  305. float *synth = ctx->synth_buf + 16; // 16 instead of LP_FILTER_ORDER for
  306. // memory alignment
  307. int t0_first = 0;
  308. AMRFixed fixed_cb;
  309. memset(ir_buf, 0, LP_FILTER_ORDER * sizeof(float));
  310. lsf_decode_fp(lsf_new, ctx->lsf_history, params);
  311. sipr_decode_lp(lsf_new, ctx->lsp_history, Az, subframe_count);
  312. memcpy(ctx->lsp_history, lsf_new, LP_FILTER_ORDER * sizeof(float));
  313. excitation = ctx->excitation + PITCH_DELAY_MAX + L_INTERPOL;
  314. for (i = 0; i < subframe_count; i++) {
  315. float *pAz = Az + i*LP_FILTER_ORDER;
  316. float fixed_vector[SUBFR_SIZE];
  317. int T0,T0_frac;
  318. float pitch_gain, gain_code, avg_energy;
  319. ff_decode_pitch_lag(&T0, &T0_frac, params->pitch_delay[i], t0_first, i,
  320. ctx->mode == MODE_5k0, 6);
  321. if (i == 0 || (i == 2 && ctx->mode == MODE_5k0))
  322. t0_first = T0;
  323. ff_acelp_interpolatef(excitation, excitation - T0 + (T0_frac <= 0),
  324. ff_b60_sinc, 6,
  325. 2 * ((2 + T0_frac)%3 + 1), LP_FILTER_ORDER,
  326. SUBFR_SIZE);
  327. decode_fixed_sparse(&fixed_cb, params->fc_indexes[i], ctx->mode,
  328. ctx->past_pitch_gain < 0.8);
  329. eval_ir(pAz, T0, impulse_response, modes[ctx->mode].pitch_sharp_factor);
  330. convolute_with_sparse(fixed_vector, &fixed_cb, impulse_response,
  331. SUBFR_SIZE);
  332. avg_energy =
  333. (0.01 + ff_dot_productf(fixed_vector, fixed_vector, SUBFR_SIZE))/
  334. SUBFR_SIZE;
  335. ctx->past_pitch_gain = pitch_gain = gain_cb[params->gc_index[i]][0];
  336. gain_code = ff_amr_set_fixed_gain(gain_cb[params->gc_index[i]][1],
  337. avg_energy, ctx->energy_history,
  338. 34 - 15.0/(0.05*M_LN10/M_LN2),
  339. pred);
  340. ff_weighted_vector_sumf(excitation, excitation, fixed_vector,
  341. pitch_gain, gain_code, SUBFR_SIZE);
  342. pitch_gain *= 0.5 * pitch_gain;
  343. pitch_gain = FFMIN(pitch_gain, 0.4);
  344. ctx->gain_mem = 0.7 * ctx->gain_mem + 0.3 * pitch_gain;
  345. ctx->gain_mem = FFMIN(ctx->gain_mem, pitch_gain);
  346. gain_code *= ctx->gain_mem;
  347. for (j = 0; j < SUBFR_SIZE; j++)
  348. fixed_vector[j] = excitation[j] - gain_code * fixed_vector[j];
  349. if (ctx->mode == MODE_5k0) {
  350. postfilter_5k0(ctx, pAz, fixed_vector);
  351. ff_celp_lp_synthesis_filterf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
  352. pAz, excitation, SUBFR_SIZE,
  353. LP_FILTER_ORDER);
  354. }
  355. ff_celp_lp_synthesis_filterf(synth + i*SUBFR_SIZE, pAz, fixed_vector,
  356. SUBFR_SIZE, LP_FILTER_ORDER);
  357. excitation += SUBFR_SIZE;
  358. }
  359. memcpy(synth - LP_FILTER_ORDER, synth + frame_size - LP_FILTER_ORDER,
  360. LP_FILTER_ORDER * sizeof(float));
  361. if (ctx->mode == MODE_5k0) {
  362. for (i = 0; i < subframe_count; i++) {
  363. float energy = ff_dot_productf(ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
  364. ctx->postfilter_syn5k0 + LP_FILTER_ORDER + i*SUBFR_SIZE,
  365. SUBFR_SIZE);
  366. ff_adaptive_gain_control(&synth[i * SUBFR_SIZE],
  367. &synth[i * SUBFR_SIZE], energy,
  368. SUBFR_SIZE, 0.9, &ctx->postfilter_agc);
  369. }
  370. memcpy(ctx->postfilter_syn5k0, ctx->postfilter_syn5k0 + frame_size,
  371. LP_FILTER_ORDER*sizeof(float));
  372. }
  373. memmove(ctx->excitation, excitation - PITCH_DELAY_MAX - L_INTERPOL,
  374. (PITCH_DELAY_MAX + L_INTERPOL) * sizeof(float));
  375. ff_acelp_apply_order_2_transfer_function(out_data, synth,
  376. (const float[2]) {-1.99997 , 1.000000000},
  377. (const float[2]) {-1.93307352, 0.935891986},
  378. 0.939805806,
  379. ctx->highpass_filt_mem,
  380. frame_size);
  381. }
  382. static av_cold int sipr_decoder_init(AVCodecContext * avctx)
  383. {
  384. SiprContext *ctx = avctx->priv_data;
  385. int i;
  386. if (avctx->bit_rate > 12200) ctx->mode = MODE_16k;
  387. else if (avctx->bit_rate > 7500 ) ctx->mode = MODE_8k5;
  388. else if (avctx->bit_rate > 5750 ) ctx->mode = MODE_6k5;
  389. else ctx->mode = MODE_5k0;
  390. av_log(avctx, AV_LOG_DEBUG, "Mode: %s\n", modes[ctx->mode].mode_name);
  391. if (ctx->mode == MODE_16k)
  392. ff_sipr_init_16k(ctx);
  393. for (i = 0; i < LP_FILTER_ORDER; i++)
  394. ctx->lsp_history[i] = cos((i+1) * M_PI / (LP_FILTER_ORDER + 1));
  395. for (i = 0; i < 4; i++)
  396. ctx->energy_history[i] = -14;
  397. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  398. return 0;
  399. }
  400. static int sipr_decode_frame(AVCodecContext *avctx, void *datap,
  401. int *data_size, AVPacket *avpkt)
  402. {
  403. SiprContext *ctx = avctx->priv_data;
  404. const uint8_t *buf=avpkt->data;
  405. SiprParameters parm;
  406. const SiprModeParam *mode_par = &modes[ctx->mode];
  407. GetBitContext gb;
  408. float *data = datap;
  409. int subframe_size = ctx->mode == MODE_16k ? L_SUBFR_16k : SUBFR_SIZE;
  410. int i, out_size;
  411. ctx->avctx = avctx;
  412. if (avpkt->size < (mode_par->bits_per_frame >> 3)) {
  413. av_log(avctx, AV_LOG_ERROR,
  414. "Error processing packet: packet size (%d) too small\n",
  415. avpkt->size);
  416. *data_size = 0;
  417. return -1;
  418. }
  419. out_size = mode_par->frames_per_packet * subframe_size *
  420. mode_par->subframe_count *
  421. av_get_bytes_per_sample(avctx->sample_fmt);
  422. if (*data_size < out_size) {
  423. av_log(avctx, AV_LOG_ERROR,
  424. "Error processing packet: output buffer (%d) too small\n",
  425. *data_size);
  426. *data_size = 0;
  427. return -1;
  428. }
  429. init_get_bits(&gb, buf, mode_par->bits_per_frame);
  430. for (i = 0; i < mode_par->frames_per_packet; i++) {
  431. decode_parameters(&parm, &gb, mode_par);
  432. if (ctx->mode == MODE_16k)
  433. ff_sipr_decode_frame_16k(ctx, &parm, data);
  434. else
  435. decode_frame(ctx, &parm, data);
  436. data += subframe_size * mode_par->subframe_count;
  437. }
  438. *data_size = out_size;
  439. return mode_par->bits_per_frame >> 3;
  440. }
  441. AVCodec ff_sipr_decoder = {
  442. .name = "sipr",
  443. .type = AVMEDIA_TYPE_AUDIO,
  444. .id = CODEC_ID_SIPR,
  445. .priv_data_size = sizeof(SiprContext),
  446. .init = sipr_decoder_init,
  447. .decode = sipr_decode_frame,
  448. .long_name = NULL_IF_CONFIG_SMALL("RealAudio SIPR / ACELP.NET"),
  449. };