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.

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