sipr.c 19 KB

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