g723_1.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. /*
  2. * G.723.1 compatible decoder
  3. * Copyright (c) 2006 Benjamin Larsson
  4. * Copyright (c) 2010 Mohamed Naufal Basheer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * G.723.1 compatible decoder
  25. */
  26. #define BITSTREAM_READER_LE
  27. #include "libavutil/audioconvert.h"
  28. #include "libavutil/lzo.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "get_bits.h"
  33. #include "acelp_vectors.h"
  34. #include "celp_filters.h"
  35. #include "celp_math.h"
  36. #include "g723_1_data.h"
  37. typedef struct g723_1_context {
  38. AVClass *class;
  39. AVFrame frame;
  40. G723_1_Subframe subframe[4];
  41. enum FrameType cur_frame_type;
  42. enum FrameType past_frame_type;
  43. enum Rate cur_rate;
  44. uint8_t lsp_index[LSP_BANDS];
  45. int pitch_lag[2];
  46. int erased_frames;
  47. int16_t prev_lsp[LPC_ORDER];
  48. int16_t prev_excitation[PITCH_MAX];
  49. int16_t excitation[PITCH_MAX + FRAME_LEN + 4];
  50. int16_t synth_mem[LPC_ORDER];
  51. int16_t fir_mem[LPC_ORDER];
  52. int iir_mem[LPC_ORDER];
  53. int random_seed;
  54. int interp_index;
  55. int interp_gain;
  56. int sid_gain;
  57. int cur_gain;
  58. int reflection_coef;
  59. int pf_gain; ///< formant postfilter
  60. ///< gain scaling unit memory
  61. int postfilter;
  62. int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX];
  63. int16_t prev_data[HALF_FRAME_LEN];
  64. int16_t prev_weight_sig[PITCH_MAX];
  65. int16_t hpf_fir_mem; ///< highpass filter fir
  66. int hpf_iir_mem; ///< and iir memories
  67. int16_t perf_fir_mem[LPC_ORDER]; ///< perceptual filter fir
  68. int16_t perf_iir_mem[LPC_ORDER]; ///< and iir memories
  69. int16_t harmonic_mem[PITCH_MAX];
  70. } G723_1_Context;
  71. static av_cold int g723_1_decode_init(AVCodecContext *avctx)
  72. {
  73. G723_1_Context *p = avctx->priv_data;
  74. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  75. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  76. avctx->channels = 1;
  77. p->pf_gain = 1 << 12;
  78. avcodec_get_frame_defaults(&p->frame);
  79. avctx->coded_frame = &p->frame;
  80. memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
  81. return 0;
  82. }
  83. /**
  84. * Unpack the frame into parameters.
  85. *
  86. * @param p the context
  87. * @param buf pointer to the input buffer
  88. * @param buf_size size of the input buffer
  89. */
  90. static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
  91. int buf_size)
  92. {
  93. GetBitContext gb;
  94. int ad_cb_len;
  95. int temp, info_bits, i;
  96. init_get_bits(&gb, buf, buf_size * 8);
  97. /* Extract frame type and rate info */
  98. info_bits = get_bits(&gb, 2);
  99. if (info_bits == 3) {
  100. p->cur_frame_type = UNTRANSMITTED_FRAME;
  101. return 0;
  102. }
  103. /* Extract 24 bit lsp indices, 8 bit for each band */
  104. p->lsp_index[2] = get_bits(&gb, 8);
  105. p->lsp_index[1] = get_bits(&gb, 8);
  106. p->lsp_index[0] = get_bits(&gb, 8);
  107. if (info_bits == 2) {
  108. p->cur_frame_type = SID_FRAME;
  109. p->subframe[0].amp_index = get_bits(&gb, 6);
  110. return 0;
  111. }
  112. /* Extract the info common to both rates */
  113. p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
  114. p->cur_frame_type = ACTIVE_FRAME;
  115. p->pitch_lag[0] = get_bits(&gb, 7);
  116. if (p->pitch_lag[0] > 123) /* test if forbidden code */
  117. return -1;
  118. p->pitch_lag[0] += PITCH_MIN;
  119. p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
  120. p->pitch_lag[1] = get_bits(&gb, 7);
  121. if (p->pitch_lag[1] > 123)
  122. return -1;
  123. p->pitch_lag[1] += PITCH_MIN;
  124. p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
  125. p->subframe[0].ad_cb_lag = 1;
  126. p->subframe[2].ad_cb_lag = 1;
  127. for (i = 0; i < SUBFRAMES; i++) {
  128. /* Extract combined gain */
  129. temp = get_bits(&gb, 12);
  130. ad_cb_len = 170;
  131. p->subframe[i].dirac_train = 0;
  132. if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
  133. p->subframe[i].dirac_train = temp >> 11;
  134. temp &= 0x7FF;
  135. ad_cb_len = 85;
  136. }
  137. p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
  138. if (p->subframe[i].ad_cb_gain < ad_cb_len) {
  139. p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
  140. GAIN_LEVELS;
  141. } else {
  142. return -1;
  143. }
  144. }
  145. p->subframe[0].grid_index = get_bits1(&gb);
  146. p->subframe[1].grid_index = get_bits1(&gb);
  147. p->subframe[2].grid_index = get_bits1(&gb);
  148. p->subframe[3].grid_index = get_bits1(&gb);
  149. if (p->cur_rate == RATE_6300) {
  150. skip_bits1(&gb); /* skip reserved bit */
  151. /* Compute pulse_pos index using the 13-bit combined position index */
  152. temp = get_bits(&gb, 13);
  153. p->subframe[0].pulse_pos = temp / 810;
  154. temp -= p->subframe[0].pulse_pos * 810;
  155. p->subframe[1].pulse_pos = FASTDIV(temp, 90);
  156. temp -= p->subframe[1].pulse_pos * 90;
  157. p->subframe[2].pulse_pos = FASTDIV(temp, 9);
  158. p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
  159. p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
  160. get_bits(&gb, 16);
  161. p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
  162. get_bits(&gb, 14);
  163. p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
  164. get_bits(&gb, 16);
  165. p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
  166. get_bits(&gb, 14);
  167. p->subframe[0].pulse_sign = get_bits(&gb, 6);
  168. p->subframe[1].pulse_sign = get_bits(&gb, 5);
  169. p->subframe[2].pulse_sign = get_bits(&gb, 6);
  170. p->subframe[3].pulse_sign = get_bits(&gb, 5);
  171. } else { /* 5300 bps */
  172. p->subframe[0].pulse_pos = get_bits(&gb, 12);
  173. p->subframe[1].pulse_pos = get_bits(&gb, 12);
  174. p->subframe[2].pulse_pos = get_bits(&gb, 12);
  175. p->subframe[3].pulse_pos = get_bits(&gb, 12);
  176. p->subframe[0].pulse_sign = get_bits(&gb, 4);
  177. p->subframe[1].pulse_sign = get_bits(&gb, 4);
  178. p->subframe[2].pulse_sign = get_bits(&gb, 4);
  179. p->subframe[3].pulse_sign = get_bits(&gb, 4);
  180. }
  181. return 0;
  182. }
  183. /**
  184. * Bitexact implementation of sqrt(val/2).
  185. */
  186. static int16_t square_root(int val)
  187. {
  188. return (ff_sqrt(val << 1) >> 1) & (~1);
  189. }
  190. /**
  191. * Calculate the number of left-shifts required for normalizing the input.
  192. *
  193. * @param num input number
  194. * @param width width of the input, 15 or 31 bits
  195. */
  196. static int normalize_bits(int num, int width)
  197. {
  198. return width - av_log2(num) - 1;
  199. }
  200. #define normalize_bits_int16(num) normalize_bits(num, 15)
  201. #define normalize_bits_int32(num) normalize_bits(num, 31)
  202. /**
  203. * Scale vector contents based on the largest of their absolutes.
  204. */
  205. static int scale_vector(int16_t *dst, const int16_t *vector, int length)
  206. {
  207. int bits, max = 0;
  208. int i;
  209. for (i = 0; i < length; i++)
  210. max |= FFABS(vector[i]);
  211. bits= 14 - av_log2_16bit(max);
  212. bits= FFMAX(bits, 0);
  213. for (i = 0; i < length; i++)
  214. dst[i] = vector[i] << bits >> 3;
  215. return bits - 3;
  216. }
  217. /**
  218. * Perform inverse quantization of LSP frequencies.
  219. *
  220. * @param cur_lsp the current LSP vector
  221. * @param prev_lsp the previous LSP vector
  222. * @param lsp_index VQ indices
  223. * @param bad_frame bad frame flag
  224. */
  225. static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
  226. uint8_t *lsp_index, int bad_frame)
  227. {
  228. int min_dist, pred;
  229. int i, j, temp, stable;
  230. /* Check for frame erasure */
  231. if (!bad_frame) {
  232. min_dist = 0x100;
  233. pred = 12288;
  234. } else {
  235. min_dist = 0x200;
  236. pred = 23552;
  237. lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
  238. }
  239. /* Get the VQ table entry corresponding to the transmitted index */
  240. cur_lsp[0] = lsp_band0[lsp_index[0]][0];
  241. cur_lsp[1] = lsp_band0[lsp_index[0]][1];
  242. cur_lsp[2] = lsp_band0[lsp_index[0]][2];
  243. cur_lsp[3] = lsp_band1[lsp_index[1]][0];
  244. cur_lsp[4] = lsp_band1[lsp_index[1]][1];
  245. cur_lsp[5] = lsp_band1[lsp_index[1]][2];
  246. cur_lsp[6] = lsp_band2[lsp_index[2]][0];
  247. cur_lsp[7] = lsp_band2[lsp_index[2]][1];
  248. cur_lsp[8] = lsp_band2[lsp_index[2]][2];
  249. cur_lsp[9] = lsp_band2[lsp_index[2]][3];
  250. /* Add predicted vector & DC component to the previously quantized vector */
  251. for (i = 0; i < LPC_ORDER; i++) {
  252. temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
  253. cur_lsp[i] += dc_lsp[i] + temp;
  254. }
  255. for (i = 0; i < LPC_ORDER; i++) {
  256. cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
  257. cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
  258. /* Stability check */
  259. for (j = 1; j < LPC_ORDER; j++) {
  260. temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
  261. if (temp > 0) {
  262. temp >>= 1;
  263. cur_lsp[j - 1] -= temp;
  264. cur_lsp[j] += temp;
  265. }
  266. }
  267. stable = 1;
  268. for (j = 1; j < LPC_ORDER; j++) {
  269. temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
  270. if (temp > 0) {
  271. stable = 0;
  272. break;
  273. }
  274. }
  275. if (stable)
  276. break;
  277. }
  278. if (!stable)
  279. memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
  280. }
  281. /**
  282. * Bitexact implementation of 2ab scaled by 1/2^16.
  283. *
  284. * @param a 32 bit multiplicand
  285. * @param b 16 bit multiplier
  286. */
  287. #define MULL2(a, b) \
  288. MULL(a,b,15)
  289. /**
  290. * Convert LSP frequencies to LPC coefficients.
  291. *
  292. * @param lpc buffer for LPC coefficients
  293. */
  294. static void lsp2lpc(int16_t *lpc)
  295. {
  296. int f1[LPC_ORDER / 2 + 1];
  297. int f2[LPC_ORDER / 2 + 1];
  298. int i, j;
  299. /* Calculate negative cosine */
  300. for (j = 0; j < LPC_ORDER; j++) {
  301. int index = lpc[j] >> 7;
  302. int offset = lpc[j] & 0x7f;
  303. int temp1 = cos_tab[index] << 16;
  304. int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
  305. ((offset << 8) + 0x80) << 1;
  306. lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
  307. }
  308. /*
  309. * Compute sum and difference polynomial coefficients
  310. * (bitexact alternative to lsp2poly() in lsp.c)
  311. */
  312. /* Initialize with values in Q28 */
  313. f1[0] = 1 << 28;
  314. f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
  315. f1[2] = lpc[0] * lpc[2] + (2 << 28);
  316. f2[0] = 1 << 28;
  317. f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
  318. f2[2] = lpc[1] * lpc[3] + (2 << 28);
  319. /*
  320. * Calculate and scale the coefficients by 1/2 in
  321. * each iteration for a final scaling factor of Q25
  322. */
  323. for (i = 2; i < LPC_ORDER / 2; i++) {
  324. f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
  325. f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
  326. for (j = i; j >= 2; j--) {
  327. f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
  328. (f1[j] >> 1) + (f1[j - 2] >> 1);
  329. f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
  330. (f2[j] >> 1) + (f2[j - 2] >> 1);
  331. }
  332. f1[0] >>= 1;
  333. f2[0] >>= 1;
  334. f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
  335. f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
  336. }
  337. /* Convert polynomial coefficients to LPC coefficients */
  338. for (i = 0; i < LPC_ORDER / 2; i++) {
  339. int64_t ff1 = f1[i + 1] + f1[i];
  340. int64_t ff2 = f2[i + 1] - f2[i];
  341. lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
  342. lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
  343. (1 << 15)) >> 16;
  344. }
  345. }
  346. /**
  347. * Quantize LSP frequencies by interpolation and convert them to
  348. * the corresponding LPC coefficients.
  349. *
  350. * @param lpc buffer for LPC coefficients
  351. * @param cur_lsp the current LSP vector
  352. * @param prev_lsp the previous LSP vector
  353. */
  354. static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
  355. {
  356. int i;
  357. int16_t *lpc_ptr = lpc;
  358. /* cur_lsp * 0.25 + prev_lsp * 0.75 */
  359. ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
  360. 4096, 12288, 1 << 13, 14, LPC_ORDER);
  361. ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
  362. 8192, 8192, 1 << 13, 14, LPC_ORDER);
  363. ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
  364. 12288, 4096, 1 << 13, 14, LPC_ORDER);
  365. memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
  366. for (i = 0; i < SUBFRAMES; i++) {
  367. lsp2lpc(lpc_ptr);
  368. lpc_ptr += LPC_ORDER;
  369. }
  370. }
  371. /**
  372. * Generate a train of dirac functions with period as pitch lag.
  373. */
  374. static void gen_dirac_train(int16_t *buf, int pitch_lag)
  375. {
  376. int16_t vector[SUBFRAME_LEN];
  377. int i, j;
  378. memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
  379. for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
  380. for (j = 0; j < SUBFRAME_LEN - i; j++)
  381. buf[i + j] += vector[j];
  382. }
  383. }
  384. /**
  385. * Generate fixed codebook excitation vector.
  386. *
  387. * @param vector decoded excitation vector
  388. * @param subfrm current subframe
  389. * @param cur_rate current bitrate
  390. * @param pitch_lag closed loop pitch lag
  391. * @param index current subframe index
  392. */
  393. static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
  394. enum Rate cur_rate, int pitch_lag, int index)
  395. {
  396. int temp, i, j;
  397. memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
  398. if (cur_rate == RATE_6300) {
  399. if (subfrm->pulse_pos >= max_pos[index])
  400. return;
  401. /* Decode amplitudes and positions */
  402. j = PULSE_MAX - pulses[index];
  403. temp = subfrm->pulse_pos;
  404. for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
  405. temp -= combinatorial_table[j][i];
  406. if (temp >= 0)
  407. continue;
  408. temp += combinatorial_table[j++][i];
  409. if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
  410. vector[subfrm->grid_index + GRID_SIZE * i] =
  411. -fixed_cb_gain[subfrm->amp_index];
  412. } else {
  413. vector[subfrm->grid_index + GRID_SIZE * i] =
  414. fixed_cb_gain[subfrm->amp_index];
  415. }
  416. if (j == PULSE_MAX)
  417. break;
  418. }
  419. if (subfrm->dirac_train == 1)
  420. gen_dirac_train(vector, pitch_lag);
  421. } else { /* 5300 bps */
  422. int cb_gain = fixed_cb_gain[subfrm->amp_index];
  423. int cb_shift = subfrm->grid_index;
  424. int cb_sign = subfrm->pulse_sign;
  425. int cb_pos = subfrm->pulse_pos;
  426. int offset, beta, lag;
  427. for (i = 0; i < 8; i += 2) {
  428. offset = ((cb_pos & 7) << 3) + cb_shift + i;
  429. vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
  430. cb_pos >>= 3;
  431. cb_sign >>= 1;
  432. }
  433. /* Enhance harmonic components */
  434. lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
  435. subfrm->ad_cb_lag - 1;
  436. beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
  437. if (lag < SUBFRAME_LEN - 2) {
  438. for (i = lag; i < SUBFRAME_LEN; i++)
  439. vector[i] += beta * vector[i - lag] >> 15;
  440. }
  441. }
  442. }
  443. /**
  444. * Get delayed contribution from the previous excitation vector.
  445. */
  446. static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
  447. {
  448. int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
  449. int i;
  450. residual[0] = prev_excitation[offset];
  451. residual[1] = prev_excitation[offset + 1];
  452. offset += 2;
  453. for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
  454. residual[i] = prev_excitation[offset + (i - 2) % lag];
  455. }
  456. static int dot_product(const int16_t *a, const int16_t *b, int length)
  457. {
  458. int sum = ff_dot_product(a,b,length);
  459. return av_sat_add32(sum, sum);
  460. }
  461. /**
  462. * Generate adaptive codebook excitation.
  463. */
  464. static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
  465. int pitch_lag, G723_1_Subframe *subfrm,
  466. enum Rate cur_rate)
  467. {
  468. int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
  469. const int16_t *cb_ptr;
  470. int lag = pitch_lag + subfrm->ad_cb_lag - 1;
  471. int i;
  472. int sum;
  473. get_residual(residual, prev_excitation, lag);
  474. /* Select quantization table */
  475. if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2) {
  476. cb_ptr = adaptive_cb_gain85;
  477. } else
  478. cb_ptr = adaptive_cb_gain170;
  479. /* Calculate adaptive vector */
  480. cb_ptr += subfrm->ad_cb_gain * 20;
  481. for (i = 0; i < SUBFRAME_LEN; i++) {
  482. sum = ff_dot_product(residual + i, cb_ptr, PITCH_ORDER);
  483. vector[i] = av_sat_dadd32(1 << 15, av_sat_add32(sum, sum)) >> 16;
  484. }
  485. }
  486. /**
  487. * Estimate maximum auto-correlation around pitch lag.
  488. *
  489. * @param buf buffer with offset applied
  490. * @param offset offset of the excitation vector
  491. * @param ccr_max pointer to the maximum auto-correlation
  492. * @param pitch_lag decoded pitch lag
  493. * @param length length of autocorrelation
  494. * @param dir forward lag(1) / backward lag(-1)
  495. */
  496. static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
  497. int pitch_lag, int length, int dir)
  498. {
  499. int limit, ccr, lag = 0;
  500. int i;
  501. pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
  502. if (dir > 0)
  503. limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
  504. else
  505. limit = pitch_lag + 3;
  506. for (i = pitch_lag - 3; i <= limit; i++) {
  507. ccr = dot_product(buf, buf + dir * i, length);
  508. if (ccr > *ccr_max) {
  509. *ccr_max = ccr;
  510. lag = i;
  511. }
  512. }
  513. return lag;
  514. }
  515. /**
  516. * Calculate pitch postfilter optimal and scaling gains.
  517. *
  518. * @param lag pitch postfilter forward/backward lag
  519. * @param ppf pitch postfilter parameters
  520. * @param cur_rate current bitrate
  521. * @param tgt_eng target energy
  522. * @param ccr cross-correlation
  523. * @param res_eng residual energy
  524. */
  525. static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
  526. int tgt_eng, int ccr, int res_eng)
  527. {
  528. int pf_residual; /* square of postfiltered residual */
  529. int temp1, temp2;
  530. ppf->index = lag;
  531. temp1 = tgt_eng * res_eng >> 1;
  532. temp2 = ccr * ccr << 1;
  533. if (temp2 > temp1) {
  534. if (ccr >= res_eng) {
  535. ppf->opt_gain = ppf_gain_weight[cur_rate];
  536. } else {
  537. ppf->opt_gain = (ccr << 15) / res_eng *
  538. ppf_gain_weight[cur_rate] >> 15;
  539. }
  540. /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
  541. temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
  542. temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
  543. pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
  544. if (tgt_eng >= pf_residual << 1) {
  545. temp1 = 0x7fff;
  546. } else {
  547. temp1 = (tgt_eng << 14) / pf_residual;
  548. }
  549. /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
  550. ppf->sc_gain = square_root(temp1 << 16);
  551. } else {
  552. ppf->opt_gain = 0;
  553. ppf->sc_gain = 0x7fff;
  554. }
  555. ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
  556. }
  557. /**
  558. * Calculate pitch postfilter parameters.
  559. *
  560. * @param p the context
  561. * @param offset offset of the excitation vector
  562. * @param pitch_lag decoded pitch lag
  563. * @param ppf pitch postfilter parameters
  564. * @param cur_rate current bitrate
  565. */
  566. static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
  567. PPFParam *ppf, enum Rate cur_rate)
  568. {
  569. int16_t scale;
  570. int i;
  571. int temp1, temp2;
  572. /*
  573. * 0 - target energy
  574. * 1 - forward cross-correlation
  575. * 2 - forward residual energy
  576. * 3 - backward cross-correlation
  577. * 4 - backward residual energy
  578. */
  579. int energy[5] = {0, 0, 0, 0, 0};
  580. int16_t *buf = p->audio + LPC_ORDER + offset;
  581. int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
  582. SUBFRAME_LEN, 1);
  583. int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
  584. SUBFRAME_LEN, -1);
  585. ppf->index = 0;
  586. ppf->opt_gain = 0;
  587. ppf->sc_gain = 0x7fff;
  588. /* Case 0, Section 3.6 */
  589. if (!back_lag && !fwd_lag)
  590. return;
  591. /* Compute target energy */
  592. energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
  593. /* Compute forward residual energy */
  594. if (fwd_lag)
  595. energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
  596. /* Compute backward residual energy */
  597. if (back_lag)
  598. energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
  599. /* Normalize and shorten */
  600. temp1 = 0;
  601. for (i = 0; i < 5; i++)
  602. temp1 = FFMAX(energy[i], temp1);
  603. scale = normalize_bits(temp1, 31);
  604. for (i = 0; i < 5; i++)
  605. energy[i] = av_clipl_int32(energy[i] << scale) >> 16;
  606. if (fwd_lag && !back_lag) { /* Case 1 */
  607. comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
  608. energy[2]);
  609. } else if (!fwd_lag) { /* Case 2 */
  610. comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
  611. energy[4]);
  612. } else { /* Case 3 */
  613. /*
  614. * Select the largest of energy[1]^2/energy[2]
  615. * and energy[3]^2/energy[4]
  616. */
  617. temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
  618. temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
  619. if (temp1 >= temp2) {
  620. comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
  621. energy[2]);
  622. } else {
  623. comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
  624. energy[4]);
  625. }
  626. }
  627. }
  628. /**
  629. * Classify frames as voiced/unvoiced.
  630. *
  631. * @param p the context
  632. * @param pitch_lag decoded pitch_lag
  633. * @param exc_eng excitation energy estimation
  634. * @param scale scaling factor of exc_eng
  635. *
  636. * @return residual interpolation index if voiced, 0 otherwise
  637. */
  638. static int comp_interp_index(G723_1_Context *p, int pitch_lag,
  639. int *exc_eng, int *scale)
  640. {
  641. int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
  642. int16_t *buf = p->audio + LPC_ORDER;
  643. int index, ccr, tgt_eng, best_eng, temp;
  644. *scale = scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
  645. buf += offset;
  646. /* Compute maximum backward cross-correlation */
  647. ccr = 0;
  648. index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
  649. ccr = av_sat_add32(ccr, 1 << 15) >> 16;
  650. /* Compute target energy */
  651. tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
  652. *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
  653. if (ccr <= 0)
  654. return 0;
  655. /* Compute best energy */
  656. best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
  657. best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
  658. temp = best_eng * *exc_eng >> 3;
  659. if (temp < ccr * ccr) {
  660. return index;
  661. } else
  662. return 0;
  663. }
  664. /**
  665. * Peform residual interpolation based on frame classification.
  666. *
  667. * @param buf decoded excitation vector
  668. * @param out output vector
  669. * @param lag decoded pitch lag
  670. * @param gain interpolated gain
  671. * @param rseed seed for random number generator
  672. */
  673. static void residual_interp(int16_t *buf, int16_t *out, int lag,
  674. int gain, int *rseed)
  675. {
  676. int i;
  677. if (lag) { /* Voiced */
  678. int16_t *vector_ptr = buf + PITCH_MAX;
  679. /* Attenuate */
  680. for (i = 0; i < lag; i++)
  681. out[i] = vector_ptr[i - lag] * 3 >> 2;
  682. av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
  683. (FRAME_LEN - lag) * sizeof(*out));
  684. } else { /* Unvoiced */
  685. for (i = 0; i < FRAME_LEN; i++) {
  686. *rseed = *rseed * 521 + 259;
  687. out[i] = gain * *rseed >> 15;
  688. }
  689. memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
  690. }
  691. }
  692. /**
  693. * Perform IIR filtering.
  694. *
  695. * @param fir_coef FIR coefficients
  696. * @param iir_coef IIR coefficients
  697. * @param src source vector
  698. * @param dest destination vector
  699. * @param width width of the output, 16 bits(0) / 32 bits(1)
  700. */
  701. #define iir_filter(fir_coef, iir_coef, src, dest, width)\
  702. {\
  703. int m, n;\
  704. int res_shift = 16 & ~-(width);\
  705. int in_shift = 16 - res_shift;\
  706. \
  707. for (m = 0; m < SUBFRAME_LEN; m++) {\
  708. int64_t filter = 0;\
  709. for (n = 1; n <= LPC_ORDER; n++) {\
  710. filter -= (fir_coef)[n - 1] * (src)[m - n] -\
  711. (iir_coef)[n - 1] * ((dest)[m - n] >> in_shift);\
  712. }\
  713. \
  714. (dest)[m] = av_clipl_int32(((src)[m] << 16) + (filter << 3) +\
  715. (1 << 15)) >> res_shift;\
  716. }\
  717. }
  718. /**
  719. * Adjust gain of postfiltered signal.
  720. *
  721. * @param p the context
  722. * @param buf postfiltered output vector
  723. * @param energy input energy coefficient
  724. */
  725. static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
  726. {
  727. int num, denom, gain, bits1, bits2;
  728. int i;
  729. num = energy;
  730. denom = 0;
  731. for (i = 0; i < SUBFRAME_LEN; i++) {
  732. int temp = buf[i] >> 2;
  733. temp *= temp;
  734. denom = av_sat_dadd32(denom, temp);
  735. }
  736. if (num && denom) {
  737. bits1 = normalize_bits(num, 31);
  738. bits2 = normalize_bits(denom, 31);
  739. num = num << bits1 >> 1;
  740. denom <<= bits2;
  741. bits2 = 5 + bits1 - bits2;
  742. bits2 = FFMAX(0, bits2);
  743. gain = (num >> 1) / (denom >> 16);
  744. gain = square_root(gain << 16 >> bits2);
  745. } else {
  746. gain = 1 << 12;
  747. }
  748. for (i = 0; i < SUBFRAME_LEN; i++) {
  749. p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
  750. buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
  751. (1 << 10)) >> 11);
  752. }
  753. }
  754. /**
  755. * Perform formant filtering.
  756. *
  757. * @param p the context
  758. * @param lpc quantized lpc coefficients
  759. * @param buf input buffer
  760. * @param dst output buffer
  761. */
  762. static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
  763. int16_t *buf, int16_t *dst)
  764. {
  765. int16_t filter_coef[2][LPC_ORDER];
  766. int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
  767. int i, j, k;
  768. memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
  769. memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
  770. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  771. for (k = 0; k < LPC_ORDER; k++) {
  772. filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
  773. (1 << 14)) >> 15;
  774. filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
  775. (1 << 14)) >> 15;
  776. }
  777. iir_filter(filter_coef[0], filter_coef[1], buf + i,
  778. filter_signal + i, 1);
  779. lpc += LPC_ORDER;
  780. }
  781. memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(int16_t));
  782. memcpy(p->iir_mem, filter_signal + FRAME_LEN, LPC_ORDER * sizeof(int));
  783. buf += LPC_ORDER;
  784. signal_ptr = filter_signal + LPC_ORDER;
  785. for (i = 0; i < SUBFRAMES; i++) {
  786. int temp;
  787. int auto_corr[2];
  788. int scale, energy;
  789. /* Normalize */
  790. scale = scale_vector(dst, buf, SUBFRAME_LEN);
  791. /* Compute auto correlation coefficients */
  792. auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
  793. auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
  794. /* Compute reflection coefficient */
  795. temp = auto_corr[1] >> 16;
  796. if (temp) {
  797. temp = (auto_corr[0] >> 2) / temp;
  798. }
  799. p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
  800. temp = -p->reflection_coef >> 1 & ~3;
  801. /* Compensation filter */
  802. for (j = 0; j < SUBFRAME_LEN; j++) {
  803. dst[j] = av_sat_dadd32(signal_ptr[j],
  804. (signal_ptr[j - 1] >> 16) * temp) >> 16;
  805. }
  806. /* Compute normalized signal energy */
  807. temp = 2 * scale + 4;
  808. if (temp < 0) {
  809. energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
  810. } else
  811. energy = auto_corr[1] >> temp;
  812. gain_scale(p, dst, energy);
  813. buf += SUBFRAME_LEN;
  814. signal_ptr += SUBFRAME_LEN;
  815. dst += SUBFRAME_LEN;
  816. }
  817. }
  818. static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
  819. int *got_frame_ptr, AVPacket *avpkt)
  820. {
  821. G723_1_Context *p = avctx->priv_data;
  822. const uint8_t *buf = avpkt->data;
  823. int buf_size = avpkt->size;
  824. int dec_mode = buf[0] & 3;
  825. PPFParam ppf[SUBFRAMES];
  826. int16_t cur_lsp[LPC_ORDER];
  827. int16_t lpc[SUBFRAMES * LPC_ORDER];
  828. int16_t acb_vector[SUBFRAME_LEN];
  829. int16_t *out;
  830. int bad_frame = 0, i, j, ret;
  831. int16_t *audio = p->audio;
  832. if (buf_size < frame_size[dec_mode]) {
  833. if (buf_size)
  834. av_log(avctx, AV_LOG_WARNING,
  835. "Expected %d bytes, got %d - skipping packet\n",
  836. frame_size[dec_mode], buf_size);
  837. *got_frame_ptr = 0;
  838. return buf_size;
  839. }
  840. if (unpack_bitstream(p, buf, buf_size) < 0) {
  841. bad_frame = 1;
  842. if (p->past_frame_type == ACTIVE_FRAME)
  843. p->cur_frame_type = ACTIVE_FRAME;
  844. else
  845. p->cur_frame_type = UNTRANSMITTED_FRAME;
  846. }
  847. p->frame.nb_samples = FRAME_LEN;
  848. if ((ret = avctx->get_buffer(avctx, &p->frame)) < 0) {
  849. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  850. return ret;
  851. }
  852. out = (int16_t *)p->frame.data[0];
  853. if (p->cur_frame_type == ACTIVE_FRAME) {
  854. if (!bad_frame)
  855. p->erased_frames = 0;
  856. else if (p->erased_frames != 3)
  857. p->erased_frames++;
  858. inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
  859. lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
  860. /* Save the lsp_vector for the next frame */
  861. memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
  862. /* Generate the excitation for the frame */
  863. memcpy(p->excitation, p->prev_excitation,
  864. PITCH_MAX * sizeof(*p->excitation));
  865. if (!p->erased_frames) {
  866. int16_t *vector_ptr = p->excitation + PITCH_MAX;
  867. /* Update interpolation gain memory */
  868. p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
  869. p->subframe[3].amp_index) >> 1];
  870. for (i = 0; i < SUBFRAMES; i++) {
  871. gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
  872. p->pitch_lag[i >> 1], i);
  873. gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
  874. p->pitch_lag[i >> 1], &p->subframe[i],
  875. p->cur_rate);
  876. /* Get the total excitation */
  877. for (j = 0; j < SUBFRAME_LEN; j++) {
  878. int v = av_clip_int16(vector_ptr[j] << 1);
  879. vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
  880. }
  881. vector_ptr += SUBFRAME_LEN;
  882. }
  883. vector_ptr = p->excitation + PITCH_MAX;
  884. p->interp_index = comp_interp_index(p, p->pitch_lag[1],
  885. &p->sid_gain, &p->cur_gain);
  886. /* Peform pitch postfiltering */
  887. if (p->postfilter) {
  888. i = PITCH_MAX;
  889. for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  890. comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
  891. ppf + j, p->cur_rate);
  892. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  893. ff_acelp_weighted_vector_sum(p->audio + LPC_ORDER + i,
  894. vector_ptr + i,
  895. vector_ptr + i + ppf[j].index,
  896. ppf[j].sc_gain,
  897. ppf[j].opt_gain,
  898. 1 << 14, 15, SUBFRAME_LEN);
  899. } else {
  900. audio = vector_ptr - LPC_ORDER;
  901. }
  902. /* Save the excitation for the next frame */
  903. memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
  904. PITCH_MAX * sizeof(*p->excitation));
  905. } else {
  906. p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
  907. if (p->erased_frames == 3) {
  908. /* Mute output */
  909. memset(p->excitation, 0,
  910. (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
  911. memset(p->prev_excitation, 0,
  912. PITCH_MAX * sizeof(*p->excitation));
  913. memset(p->frame.data[0], 0,
  914. (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
  915. } else {
  916. int16_t *buf = p->audio + LPC_ORDER;
  917. /* Regenerate frame */
  918. residual_interp(p->excitation, buf, p->interp_index,
  919. p->interp_gain, &p->random_seed);
  920. /* Save the excitation for the next frame */
  921. memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
  922. PITCH_MAX * sizeof(*p->excitation));
  923. }
  924. }
  925. } else {
  926. memset(out, 0, FRAME_LEN * 2);
  927. av_log(avctx, AV_LOG_WARNING,
  928. "G.723.1: Comfort noise generation not supported yet\n");
  929. *got_frame_ptr = 1;
  930. *(AVFrame *)data = p->frame;
  931. return frame_size[dec_mode];
  932. }
  933. p->past_frame_type = p->cur_frame_type;
  934. memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
  935. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  936. ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
  937. audio + i, SUBFRAME_LEN, LPC_ORDER,
  938. 0, 1, 1 << 12);
  939. memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
  940. if (p->postfilter) {
  941. formant_postfilter(p, lpc, p->audio, out);
  942. } else { // if output is not postfiltered it should be scaled by 2
  943. for (i = 0; i < FRAME_LEN; i++)
  944. out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
  945. }
  946. *got_frame_ptr = 1;
  947. *(AVFrame *)data = p->frame;
  948. return frame_size[dec_mode];
  949. }
  950. #define OFFSET(x) offsetof(G723_1_Context, x)
  951. #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  952. static const AVOption options[] = {
  953. { "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
  954. { 1 }, 0, 1, AD },
  955. { NULL }
  956. };
  957. static const AVClass g723_1dec_class = {
  958. .class_name = "G.723.1 decoder",
  959. .item_name = av_default_item_name,
  960. .option = options,
  961. .version = LIBAVUTIL_VERSION_INT,
  962. };
  963. AVCodec ff_g723_1_decoder = {
  964. .name = "g723_1",
  965. .type = AVMEDIA_TYPE_AUDIO,
  966. .id = AV_CODEC_ID_G723_1,
  967. .priv_data_size = sizeof(G723_1_Context),
  968. .init = g723_1_decode_init,
  969. .decode = g723_1_decode_frame,
  970. .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
  971. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  972. .priv_class = &g723_1dec_class,
  973. };
  974. #if CONFIG_G723_1_ENCODER
  975. #define BITSTREAM_WRITER_LE
  976. #include "put_bits.h"
  977. static av_cold int g723_1_encode_init(AVCodecContext *avctx)
  978. {
  979. G723_1_Context *p = avctx->priv_data;
  980. if (avctx->sample_rate != 8000) {
  981. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  982. return -1;
  983. }
  984. if (avctx->channels != 1) {
  985. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  986. return AVERROR(EINVAL);
  987. }
  988. if (avctx->bit_rate == 6300) {
  989. p->cur_rate = RATE_6300;
  990. } else if (avctx->bit_rate == 5300) {
  991. av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6.3k\n");
  992. return AVERROR_PATCHWELCOME;
  993. } else {
  994. av_log(avctx, AV_LOG_ERROR,
  995. "Bitrate not supported, use 6.3k\n");
  996. return AVERROR(EINVAL);
  997. }
  998. avctx->frame_size = 240;
  999. memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
  1000. return 0;
  1001. }
  1002. /**
  1003. * Remove DC component from the input signal.
  1004. *
  1005. * @param buf input signal
  1006. * @param fir zero memory
  1007. * @param iir pole memory
  1008. */
  1009. static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
  1010. {
  1011. int i;
  1012. for (i = 0; i < FRAME_LEN; i++) {
  1013. *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
  1014. *fir = buf[i];
  1015. buf[i] = av_clipl_int32((int64_t)*iir + (1 << 15)) >> 16;
  1016. }
  1017. }
  1018. /**
  1019. * Estimate autocorrelation of the input vector.
  1020. *
  1021. * @param buf input buffer
  1022. * @param autocorr autocorrelation coefficients vector
  1023. */
  1024. static void comp_autocorr(int16_t *buf, int16_t *autocorr)
  1025. {
  1026. int i, scale, temp;
  1027. int16_t vector[LPC_FRAME];
  1028. scale_vector(vector, buf, LPC_FRAME);
  1029. /* Apply the Hamming window */
  1030. for (i = 0; i < LPC_FRAME; i++)
  1031. vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
  1032. /* Compute the first autocorrelation coefficient */
  1033. temp = ff_dot_product(vector, vector, LPC_FRAME);
  1034. /* Apply a white noise correlation factor of (1025/1024) */
  1035. temp += temp >> 10;
  1036. /* Normalize */
  1037. scale = normalize_bits_int32(temp);
  1038. autocorr[0] = av_clipl_int32((int64_t)(temp << scale) +
  1039. (1 << 15)) >> 16;
  1040. /* Compute the remaining coefficients */
  1041. if (!autocorr[0]) {
  1042. memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
  1043. } else {
  1044. for (i = 1; i <= LPC_ORDER; i++) {
  1045. temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
  1046. temp = MULL2((temp << scale), binomial_window[i - 1]);
  1047. autocorr[i] = av_clipl_int32((int64_t)temp + (1 << 15)) >> 16;
  1048. }
  1049. }
  1050. }
  1051. /**
  1052. * Use Levinson-Durbin recursion to compute LPC coefficients from
  1053. * autocorrelation values.
  1054. *
  1055. * @param lpc LPC coefficients vector
  1056. * @param autocorr autocorrelation coefficients vector
  1057. * @param error prediction error
  1058. */
  1059. static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
  1060. {
  1061. int16_t vector[LPC_ORDER];
  1062. int16_t partial_corr;
  1063. int i, j, temp;
  1064. memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
  1065. for (i = 0; i < LPC_ORDER; i++) {
  1066. /* Compute the partial correlation coefficient */
  1067. temp = 0;
  1068. for (j = 0; j < i; j++)
  1069. temp -= lpc[j] * autocorr[i - j - 1];
  1070. temp = ((autocorr[i] << 13) + temp) << 3;
  1071. if (FFABS(temp) >= (error << 16))
  1072. break;
  1073. partial_corr = temp / (error << 1);
  1074. lpc[i] = av_clipl_int32((int64_t)(partial_corr << 14) +
  1075. (1 << 15)) >> 16;
  1076. /* Update the prediction error */
  1077. temp = MULL2(temp, partial_corr);
  1078. error = av_clipl_int32((int64_t)(error << 16) - temp +
  1079. (1 << 15)) >> 16;
  1080. memcpy(vector, lpc, i * sizeof(int16_t));
  1081. for (j = 0; j < i; j++) {
  1082. temp = partial_corr * vector[i - j - 1] << 1;
  1083. lpc[j] = av_clipl_int32((int64_t)(lpc[j] << 16) - temp +
  1084. (1 << 15)) >> 16;
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * Calculate LPC coefficients for the current frame.
  1090. *
  1091. * @param buf current frame
  1092. * @param prev_data 2 trailing subframes of the previous frame
  1093. * @param lpc LPC coefficients vector
  1094. */
  1095. static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
  1096. {
  1097. int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
  1098. int16_t *autocorr_ptr = autocorr;
  1099. int16_t *lpc_ptr = lpc;
  1100. int i, j;
  1101. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  1102. comp_autocorr(buf + i, autocorr_ptr);
  1103. levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
  1104. lpc_ptr += LPC_ORDER;
  1105. autocorr_ptr += LPC_ORDER + 1;
  1106. }
  1107. }
  1108. static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
  1109. {
  1110. int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
  1111. ///< polynomials (F1, F2) ordered as
  1112. ///< f1[0], f2[0], ...., f1[5], f2[5]
  1113. int max, shift, cur_val, prev_val, count, p;
  1114. int i, j;
  1115. int64_t temp;
  1116. /* Initialize f1[0] and f2[0] to 1 in Q25 */
  1117. for (i = 0; i < LPC_ORDER; i++)
  1118. lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
  1119. /* Apply bandwidth expansion on the LPC coefficients */
  1120. f[0] = f[1] = 1 << 25;
  1121. /* Compute the remaining coefficients */
  1122. for (i = 0; i < LPC_ORDER / 2; i++) {
  1123. /* f1 */
  1124. f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
  1125. /* f2 */
  1126. f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
  1127. }
  1128. /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
  1129. f[LPC_ORDER] >>= 1;
  1130. f[LPC_ORDER + 1] >>= 1;
  1131. /* Normalize and shorten */
  1132. max = FFABS(f[0]);
  1133. for (i = 1; i < LPC_ORDER + 2; i++)
  1134. max = FFMAX(max, FFABS(f[i]));
  1135. shift = normalize_bits_int32(max);
  1136. for (i = 0; i < LPC_ORDER + 2; i++)
  1137. f[i] = av_clipl_int32((int64_t)(f[i] << shift) + (1 << 15)) >> 16;
  1138. /**
  1139. * Evaluate F1 and F2 at uniform intervals of pi/256 along the
  1140. * unit circle and check for zero crossings.
  1141. */
  1142. p = 0;
  1143. temp = 0;
  1144. for (i = 0; i <= LPC_ORDER / 2; i++)
  1145. temp += f[2 * i] * cos_tab[0];
  1146. prev_val = av_clipl_int32(temp << 1);
  1147. count = 0;
  1148. for ( i = 1; i < COS_TBL_SIZE / 2; i++) {
  1149. /* Evaluate */
  1150. temp = 0;
  1151. for (j = 0; j <= LPC_ORDER / 2; j++)
  1152. temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
  1153. cur_val = av_clipl_int32(temp << 1);
  1154. /* Check for sign change, indicating a zero crossing */
  1155. if ((cur_val ^ prev_val) < 0) {
  1156. int abs_cur = FFABS(cur_val);
  1157. int abs_prev = FFABS(prev_val);
  1158. int sum = abs_cur + abs_prev;
  1159. shift = normalize_bits_int32(sum);
  1160. sum <<= shift;
  1161. abs_prev = abs_prev << shift >> 8;
  1162. lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
  1163. if (count == LPC_ORDER)
  1164. break;
  1165. /* Switch between sum and difference polynomials */
  1166. p ^= 1;
  1167. /* Evaluate */
  1168. temp = 0;
  1169. for (j = 0; j <= LPC_ORDER / 2; j++){
  1170. temp += f[LPC_ORDER - 2 * j + p] *
  1171. cos_tab[i * j % COS_TBL_SIZE];
  1172. }
  1173. cur_val = av_clipl_int32(temp<<1);
  1174. }
  1175. prev_val = cur_val;
  1176. }
  1177. if (count != LPC_ORDER)
  1178. memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
  1179. }
  1180. /**
  1181. * Quantize the current LSP subvector.
  1182. *
  1183. * @param num band number
  1184. * @param offset offset of the current subvector in an LPC_ORDER vector
  1185. * @param size size of the current subvector
  1186. */
  1187. #define get_index(num, offset, size) \
  1188. {\
  1189. int error, max = -1;\
  1190. int16_t temp[4];\
  1191. int i, j;\
  1192. for (i = 0; i < LSP_CB_SIZE; i++) {\
  1193. for (j = 0; j < size; j++){\
  1194. temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] +\
  1195. (1 << 14)) >> 15;\
  1196. }\
  1197. error = dot_product(lsp + (offset), temp, size) << 1;\
  1198. error -= dot_product(lsp_band##num[i], temp, size);\
  1199. if (error > max) {\
  1200. max = error;\
  1201. lsp_index[num] = i;\
  1202. }\
  1203. }\
  1204. }
  1205. /**
  1206. * Vector quantize the LSP frequencies.
  1207. *
  1208. * @param lsp the current lsp vector
  1209. * @param prev_lsp the previous lsp vector
  1210. */
  1211. static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
  1212. {
  1213. int16_t weight[LPC_ORDER];
  1214. int16_t min, max;
  1215. int shift, i;
  1216. /* Calculate the VQ weighting vector */
  1217. weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
  1218. weight[LPC_ORDER - 1] = (1 << 20) /
  1219. (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
  1220. for (i = 1; i < LPC_ORDER - 1; i++) {
  1221. min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
  1222. if (min > 0x20)
  1223. weight[i] = (1 << 20) / min;
  1224. else
  1225. weight[i] = INT16_MAX;
  1226. }
  1227. /* Normalize */
  1228. max = 0;
  1229. for (i = 0; i < LPC_ORDER; i++)
  1230. max = FFMAX(weight[i], max);
  1231. shift = normalize_bits_int16(max);
  1232. for (i = 0; i < LPC_ORDER; i++) {
  1233. weight[i] <<= shift;
  1234. }
  1235. /* Compute the VQ target vector */
  1236. for (i = 0; i < LPC_ORDER; i++) {
  1237. lsp[i] -= dc_lsp[i] +
  1238. (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
  1239. }
  1240. get_index(0, 0, 3);
  1241. get_index(1, 3, 3);
  1242. get_index(2, 6, 4);
  1243. }
  1244. /**
  1245. * Apply the formant perceptual weighting filter.
  1246. *
  1247. * @param flt_coef filter coefficients
  1248. * @param unq_lpc unquantized lpc vector
  1249. */
  1250. static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
  1251. int16_t *unq_lpc, int16_t *buf)
  1252. {
  1253. int16_t vector[FRAME_LEN + LPC_ORDER];
  1254. int i, j, k, l = 0;
  1255. memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
  1256. memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
  1257. memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  1258. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  1259. for (k = 0; k < LPC_ORDER; k++) {
  1260. flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
  1261. (1 << 14)) >> 15;
  1262. flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
  1263. percept_flt_tbl[1][k] +
  1264. (1 << 14)) >> 15;
  1265. }
  1266. iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER, vector + i,
  1267. buf + i, 0);
  1268. l += LPC_ORDER;
  1269. }
  1270. memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  1271. memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  1272. }
  1273. /**
  1274. * Estimate the open loop pitch period.
  1275. *
  1276. * @param buf perceptually weighted speech
  1277. * @param start estimation is carried out from this position
  1278. */
  1279. static int estimate_pitch(int16_t *buf, int start)
  1280. {
  1281. int max_exp = 32;
  1282. int max_ccr = 0x4000;
  1283. int max_eng = 0x7fff;
  1284. int index = PITCH_MIN;
  1285. int offset = start - PITCH_MIN + 1;
  1286. int ccr, eng, orig_eng, ccr_eng, exp;
  1287. int diff, temp;
  1288. int i;
  1289. orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
  1290. for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
  1291. offset--;
  1292. /* Update energy and compute correlation */
  1293. orig_eng += buf[offset] * buf[offset] -
  1294. buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
  1295. ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
  1296. if (ccr <= 0)
  1297. continue;
  1298. /* Split into mantissa and exponent to maintain precision */
  1299. exp = normalize_bits_int32(ccr);
  1300. ccr = av_clipl_int32((int64_t)(ccr << exp) + (1 << 15)) >> 16;
  1301. exp <<= 1;
  1302. ccr *= ccr;
  1303. temp = normalize_bits_int32(ccr);
  1304. ccr = ccr << temp >> 16;
  1305. exp += temp;
  1306. temp = normalize_bits_int32(orig_eng);
  1307. eng = av_clipl_int32((int64_t)(orig_eng << temp) + (1 << 15)) >> 16;
  1308. exp -= temp;
  1309. if (ccr >= eng) {
  1310. exp--;
  1311. ccr >>= 1;
  1312. }
  1313. if (exp > max_exp)
  1314. continue;
  1315. if (exp + 1 < max_exp)
  1316. goto update;
  1317. /* Equalize exponents before comparison */
  1318. if (exp + 1 == max_exp)
  1319. temp = max_ccr >> 1;
  1320. else
  1321. temp = max_ccr;
  1322. ccr_eng = ccr * max_eng;
  1323. diff = ccr_eng - eng * temp;
  1324. if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
  1325. update:
  1326. index = i;
  1327. max_exp = exp;
  1328. max_ccr = ccr;
  1329. max_eng = eng;
  1330. }
  1331. }
  1332. return index;
  1333. }
  1334. /**
  1335. * Compute harmonic noise filter parameters.
  1336. *
  1337. * @param buf perceptually weighted speech
  1338. * @param pitch_lag open loop pitch period
  1339. * @param hf harmonic filter parameters
  1340. */
  1341. static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
  1342. {
  1343. int ccr, eng, max_ccr, max_eng;
  1344. int exp, max, diff;
  1345. int energy[15];
  1346. int i, j;
  1347. for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
  1348. /* Compute residual energy */
  1349. energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
  1350. /* Compute correlation */
  1351. energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
  1352. }
  1353. /* Compute target energy */
  1354. energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
  1355. /* Normalize */
  1356. max = 0;
  1357. for (i = 0; i < 15; i++)
  1358. max = FFMAX(max, FFABS(energy[i]));
  1359. exp = normalize_bits_int32(max);
  1360. for (i = 0; i < 15; i++) {
  1361. energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
  1362. (1 << 15)) >> 16;
  1363. }
  1364. hf->index = -1;
  1365. hf->gain = 0;
  1366. max_ccr = 1;
  1367. max_eng = 0x7fff;
  1368. for (i = 0; i <= 6; i++) {
  1369. eng = energy[i << 1];
  1370. ccr = energy[(i << 1) + 1];
  1371. if (ccr <= 0)
  1372. continue;
  1373. ccr = (ccr * ccr + (1 << 14)) >> 15;
  1374. diff = ccr * max_eng - eng * max_ccr;
  1375. if (diff > 0) {
  1376. max_ccr = ccr;
  1377. max_eng = eng;
  1378. hf->index = i;
  1379. }
  1380. }
  1381. if (hf->index == -1) {
  1382. hf->index = pitch_lag;
  1383. return;
  1384. }
  1385. eng = energy[14] * max_eng;
  1386. eng = (eng >> 2) + (eng >> 3);
  1387. ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
  1388. if (eng < ccr) {
  1389. eng = energy[(hf->index << 1) + 1];
  1390. if (eng >= max_eng)
  1391. hf->gain = 0x2800;
  1392. else
  1393. hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
  1394. }
  1395. hf->index += pitch_lag - 3;
  1396. }
  1397. /**
  1398. * Apply the harmonic noise shaping filter.
  1399. *
  1400. * @param hf filter parameters
  1401. */
  1402. static void harmonic_filter(HFParam *hf, int16_t *src, int16_t *dest)
  1403. {
  1404. int i;
  1405. for (i = 0; i < SUBFRAME_LEN; i++) {
  1406. int64_t temp = hf->gain * src[i - hf->index] << 1;
  1407. dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
  1408. }
  1409. }
  1410. static void harmonic_noise_sub(HFParam *hf, int16_t *src, int16_t *dest)
  1411. {
  1412. int i;
  1413. for (i = 0; i < SUBFRAME_LEN; i++) {
  1414. int64_t temp = hf->gain * src[i - hf->index] << 1;
  1415. dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
  1416. (1 << 15)) >> 16;
  1417. }
  1418. }
  1419. /**
  1420. * Combined synthesis and formant perceptual weighting filer.
  1421. *
  1422. * @param qnt_lpc quantized lpc coefficients
  1423. * @param perf_lpc perceptual filter coefficients
  1424. * @param perf_fir perceptual filter fir memory
  1425. * @param perf_iir perceptual filter iir memory
  1426. * @param scale the filter output will be scaled by 2^scale
  1427. */
  1428. static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
  1429. int16_t *perf_fir, int16_t *perf_iir,
  1430. int16_t *src, int16_t *dest, int scale)
  1431. {
  1432. int i, j;
  1433. int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
  1434. int64_t buf[SUBFRAME_LEN];
  1435. int16_t *bptr_16 = buf_16 + LPC_ORDER;
  1436. memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
  1437. memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
  1438. for (i = 0; i < SUBFRAME_LEN; i++) {
  1439. int64_t temp = 0;
  1440. for (j = 1; j <= LPC_ORDER; j++)
  1441. temp -= qnt_lpc[j - 1] * bptr_16[i - j];
  1442. buf[i] = (src[i] << 15) + (temp << 3);
  1443. bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
  1444. }
  1445. for (i = 0; i < SUBFRAME_LEN; i++) {
  1446. int64_t fir = 0, iir = 0;
  1447. for (j = 1; j <= LPC_ORDER; j++) {
  1448. fir -= perf_lpc[j - 1] * bptr_16[i - j];
  1449. iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
  1450. }
  1451. dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
  1452. (1 << 15)) >> 16;
  1453. }
  1454. memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  1455. memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
  1456. sizeof(int16_t) * LPC_ORDER);
  1457. }
  1458. /**
  1459. * Compute the adaptive codebook contribution.
  1460. *
  1461. * @param buf input signal
  1462. * @param index the current subframe index
  1463. */
  1464. static void acb_search(G723_1_Context *p, int16_t *residual,
  1465. int16_t *impulse_resp, int16_t *buf,
  1466. int index)
  1467. {
  1468. int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
  1469. const int16_t *cb_tbl = adaptive_cb_gain85;
  1470. int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
  1471. int pitch_lag = p->pitch_lag[index >> 1];
  1472. int acb_lag = 1;
  1473. int acb_gain = 0;
  1474. int odd_frame = index & 1;
  1475. int iter = 3 + odd_frame;
  1476. int count = 0;
  1477. int tbl_size = 85;
  1478. int i, j, k, l, max;
  1479. int64_t temp;
  1480. if (!odd_frame) {
  1481. if (pitch_lag == PITCH_MIN)
  1482. pitch_lag++;
  1483. else
  1484. pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
  1485. }
  1486. for (i = 0; i < iter; i++) {
  1487. get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
  1488. for (j = 0; j < SUBFRAME_LEN; j++) {
  1489. temp = 0;
  1490. for (k = 0; k <= j; k++)
  1491. temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
  1492. flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
  1493. (1 << 15)) >> 16;
  1494. }
  1495. for (j = PITCH_ORDER - 2; j >= 0; j--) {
  1496. flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
  1497. for (k = 1; k < SUBFRAME_LEN; k++) {
  1498. temp = (flt_buf[j + 1][k - 1] << 15) +
  1499. residual[j] * impulse_resp[k];
  1500. flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
  1501. }
  1502. }
  1503. /* Compute crosscorrelation with the signal */
  1504. for (j = 0; j < PITCH_ORDER; j++) {
  1505. temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
  1506. ccr_buf[count++] = av_clipl_int32(temp << 1);
  1507. }
  1508. /* Compute energies */
  1509. for (j = 0; j < PITCH_ORDER; j++) {
  1510. ccr_buf[count++] = dot_product(flt_buf[j], flt_buf[j],
  1511. SUBFRAME_LEN);
  1512. }
  1513. for (j = 1; j < PITCH_ORDER; j++) {
  1514. for (k = 0; k < j; k++) {
  1515. temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
  1516. ccr_buf[count++] = av_clipl_int32(temp<<2);
  1517. }
  1518. }
  1519. }
  1520. /* Normalize and shorten */
  1521. max = 0;
  1522. for (i = 0; i < 20 * iter; i++)
  1523. max = FFMAX(max, FFABS(ccr_buf[i]));
  1524. temp = normalize_bits_int32(max);
  1525. for (i = 0; i < 20 * iter; i++){
  1526. ccr_buf[i] = av_clipl_int32((int64_t)(ccr_buf[i] << temp) +
  1527. (1 << 15)) >> 16;
  1528. }
  1529. max = 0;
  1530. for (i = 0; i < iter; i++) {
  1531. /* Select quantization table */
  1532. if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
  1533. odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
  1534. cb_tbl = adaptive_cb_gain170;
  1535. tbl_size = 170;
  1536. }
  1537. for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
  1538. temp = 0;
  1539. for (l = 0; l < 20; l++)
  1540. temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
  1541. temp = av_clipl_int32(temp);
  1542. if (temp > max) {
  1543. max = temp;
  1544. acb_gain = j;
  1545. acb_lag = i;
  1546. }
  1547. }
  1548. }
  1549. if (!odd_frame) {
  1550. pitch_lag += acb_lag - 1;
  1551. acb_lag = 1;
  1552. }
  1553. p->pitch_lag[index >> 1] = pitch_lag;
  1554. p->subframe[index].ad_cb_lag = acb_lag;
  1555. p->subframe[index].ad_cb_gain = acb_gain;
  1556. }
  1557. /**
  1558. * Subtract the adaptive codebook contribution from the input
  1559. * to obtain the residual.
  1560. *
  1561. * @param buf target vector
  1562. */
  1563. static void sub_acb_contrib(int16_t *residual, int16_t *impulse_resp,
  1564. int16_t *buf)
  1565. {
  1566. int i, j;
  1567. /* Subtract adaptive CB contribution to obtain the residual */
  1568. for (i = 0; i < SUBFRAME_LEN; i++) {
  1569. int64_t temp = buf[i] << 14;
  1570. for (j = 0; j <= i; j++)
  1571. temp -= residual[j] * impulse_resp[i - j];
  1572. buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
  1573. }
  1574. }
  1575. /**
  1576. * Quantize the residual signal using the fixed codebook (MP-MLQ).
  1577. *
  1578. * @param optim optimized fixed codebook parameters
  1579. * @param buf excitation vector
  1580. */
  1581. static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
  1582. int16_t *buf, int pulse_cnt, int pitch_lag)
  1583. {
  1584. FCBParam param;
  1585. int16_t impulse_r[SUBFRAME_LEN];
  1586. int16_t temp_corr[SUBFRAME_LEN];
  1587. int16_t impulse_corr[SUBFRAME_LEN];
  1588. int ccr1[SUBFRAME_LEN];
  1589. int ccr2[SUBFRAME_LEN];
  1590. int amp, err, max, max_amp_index, min, scale, i, j, k, l;
  1591. int64_t temp;
  1592. /* Update impulse response */
  1593. memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
  1594. param.dirac_train = 0;
  1595. if (pitch_lag < SUBFRAME_LEN - 2) {
  1596. param.dirac_train = 1;
  1597. gen_dirac_train(impulse_r, pitch_lag);
  1598. }
  1599. for (i = 0; i < SUBFRAME_LEN; i++)
  1600. temp_corr[i] = impulse_r[i] >> 1;
  1601. /* Compute impulse response autocorrelation */
  1602. temp = dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
  1603. scale = normalize_bits_int32(temp);
  1604. impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
  1605. for (i = 1; i < SUBFRAME_LEN; i++) {
  1606. temp = dot_product(temp_corr + i, temp_corr, SUBFRAME_LEN - i);
  1607. impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
  1608. }
  1609. /* Compute crosscorrelation of impulse response with residual signal */
  1610. scale -= 4;
  1611. for (i = 0; i < SUBFRAME_LEN; i++){
  1612. temp = dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
  1613. if (scale < 0)
  1614. ccr1[i] = temp >> -scale;
  1615. else
  1616. ccr1[i] = av_clipl_int32(temp << scale);
  1617. }
  1618. /* Search loop */
  1619. for (i = 0; i < GRID_SIZE; i++) {
  1620. /* Maximize the crosscorrelation */
  1621. max = 0;
  1622. for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
  1623. temp = FFABS(ccr1[j]);
  1624. if (temp >= max) {
  1625. max = temp;
  1626. param.pulse_pos[0] = j;
  1627. }
  1628. }
  1629. /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
  1630. amp = max;
  1631. min = 1 << 30;
  1632. max_amp_index = GAIN_LEVELS - 2;
  1633. for (j = max_amp_index; j >= 2; j--) {
  1634. temp = av_clipl_int32((int64_t)fixed_cb_gain[j] *
  1635. impulse_corr[0] << 1);
  1636. temp = FFABS(temp - amp);
  1637. if (temp < min) {
  1638. min = temp;
  1639. max_amp_index = j;
  1640. }
  1641. }
  1642. max_amp_index--;
  1643. /* Select additional gain values */
  1644. for (j = 1; j < 5; j++) {
  1645. for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
  1646. temp_corr[k] = 0;
  1647. ccr2[k] = ccr1[k];
  1648. }
  1649. param.amp_index = max_amp_index + j - 2;
  1650. amp = fixed_cb_gain[param.amp_index];
  1651. param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
  1652. temp_corr[param.pulse_pos[0]] = 1;
  1653. for (k = 1; k < pulse_cnt; k++) {
  1654. max = -1 << 30;
  1655. for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
  1656. if (temp_corr[l])
  1657. continue;
  1658. temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
  1659. temp = av_clipl_int32((int64_t)temp *
  1660. param.pulse_sign[k - 1] << 1);
  1661. ccr2[l] -= temp;
  1662. temp = FFABS(ccr2[l]);
  1663. if (temp > max) {
  1664. max = temp;
  1665. param.pulse_pos[k] = l;
  1666. }
  1667. }
  1668. param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
  1669. -amp : amp;
  1670. temp_corr[param.pulse_pos[k]] = 1;
  1671. }
  1672. /* Create the error vector */
  1673. memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
  1674. for (k = 0; k < pulse_cnt; k++)
  1675. temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
  1676. for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
  1677. temp = 0;
  1678. for (l = 0; l <= k; l++) {
  1679. int prod = av_clipl_int32((int64_t)temp_corr[l] *
  1680. impulse_r[k - l] << 1);
  1681. temp = av_clipl_int32(temp + prod);
  1682. }
  1683. temp_corr[k] = temp << 2 >> 16;
  1684. }
  1685. /* Compute square of error */
  1686. err = 0;
  1687. for (k = 0; k < SUBFRAME_LEN; k++) {
  1688. int64_t prod;
  1689. prod = av_clipl_int32((int64_t)buf[k] * temp_corr[k] << 1);
  1690. err = av_clipl_int32(err - prod);
  1691. prod = av_clipl_int32((int64_t)temp_corr[k] * temp_corr[k]);
  1692. err = av_clipl_int32(err + prod);
  1693. }
  1694. /* Minimize */
  1695. if (err < optim->min_err) {
  1696. optim->min_err = err;
  1697. optim->grid_index = i;
  1698. optim->amp_index = param.amp_index;
  1699. optim->dirac_train = param.dirac_train;
  1700. for (k = 0; k < pulse_cnt; k++) {
  1701. optim->pulse_sign[k] = param.pulse_sign[k];
  1702. optim->pulse_pos[k] = param.pulse_pos[k];
  1703. }
  1704. }
  1705. }
  1706. }
  1707. }
  1708. /**
  1709. * Encode the pulse position and gain of the current subframe.
  1710. *
  1711. * @param optim optimized fixed CB parameters
  1712. * @param buf excitation vector
  1713. */
  1714. static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
  1715. int16_t *buf, int pulse_cnt)
  1716. {
  1717. int i, j;
  1718. j = PULSE_MAX - pulse_cnt;
  1719. subfrm->pulse_sign = 0;
  1720. subfrm->pulse_pos = 0;
  1721. for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
  1722. int val = buf[optim->grid_index + (i << 1)];
  1723. if (!val) {
  1724. subfrm->pulse_pos += combinatorial_table[j][i];
  1725. } else {
  1726. subfrm->pulse_sign <<= 1;
  1727. if (val < 0) subfrm->pulse_sign++;
  1728. j++;
  1729. if (j == PULSE_MAX) break;
  1730. }
  1731. }
  1732. subfrm->amp_index = optim->amp_index;
  1733. subfrm->grid_index = optim->grid_index;
  1734. subfrm->dirac_train = optim->dirac_train;
  1735. }
  1736. /**
  1737. * Compute the fixed codebook excitation.
  1738. *
  1739. * @param buf target vector
  1740. * @param impulse_resp impulse response of the combined filter
  1741. */
  1742. static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
  1743. int16_t *buf, int index)
  1744. {
  1745. FCBParam optim;
  1746. int pulse_cnt = pulses[index];
  1747. int i;
  1748. optim.min_err = 1 << 30;
  1749. get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
  1750. if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
  1751. get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
  1752. p->pitch_lag[index >> 1]);
  1753. }
  1754. /* Reconstruct the excitation */
  1755. memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
  1756. for (i = 0; i < pulse_cnt; i++)
  1757. buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
  1758. pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
  1759. if (optim.dirac_train)
  1760. gen_dirac_train(buf, p->pitch_lag[index >> 1]);
  1761. }
  1762. /**
  1763. * Pack the frame parameters into output bitstream.
  1764. *
  1765. * @param frame output buffer
  1766. * @param size size of the buffer
  1767. */
  1768. static int pack_bitstream(G723_1_Context *p, unsigned char *frame, int size)
  1769. {
  1770. PutBitContext pb;
  1771. int info_bits, i, temp;
  1772. init_put_bits(&pb, frame, size);
  1773. if (p->cur_rate == RATE_6300) {
  1774. info_bits = 0;
  1775. put_bits(&pb, 2, info_bits);
  1776. }
  1777. put_bits(&pb, 8, p->lsp_index[2]);
  1778. put_bits(&pb, 8, p->lsp_index[1]);
  1779. put_bits(&pb, 8, p->lsp_index[0]);
  1780. put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
  1781. put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
  1782. put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
  1783. put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
  1784. /* Write 12 bit combined gain */
  1785. for (i = 0; i < SUBFRAMES; i++) {
  1786. temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
  1787. p->subframe[i].amp_index;
  1788. if (p->cur_rate == RATE_6300)
  1789. temp += p->subframe[i].dirac_train << 11;
  1790. put_bits(&pb, 12, temp);
  1791. }
  1792. put_bits(&pb, 1, p->subframe[0].grid_index);
  1793. put_bits(&pb, 1, p->subframe[1].grid_index);
  1794. put_bits(&pb, 1, p->subframe[2].grid_index);
  1795. put_bits(&pb, 1, p->subframe[3].grid_index);
  1796. if (p->cur_rate == RATE_6300) {
  1797. skip_put_bits(&pb, 1); /* reserved bit */
  1798. /* Write 13 bit combined position index */
  1799. temp = (p->subframe[0].pulse_pos >> 16) * 810 +
  1800. (p->subframe[1].pulse_pos >> 14) * 90 +
  1801. (p->subframe[2].pulse_pos >> 16) * 9 +
  1802. (p->subframe[3].pulse_pos >> 14);
  1803. put_bits(&pb, 13, temp);
  1804. put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
  1805. put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
  1806. put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
  1807. put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
  1808. put_bits(&pb, 6, p->subframe[0].pulse_sign);
  1809. put_bits(&pb, 5, p->subframe[1].pulse_sign);
  1810. put_bits(&pb, 6, p->subframe[2].pulse_sign);
  1811. put_bits(&pb, 5, p->subframe[3].pulse_sign);
  1812. }
  1813. flush_put_bits(&pb);
  1814. return frame_size[info_bits];
  1815. }
  1816. static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  1817. const AVFrame *frame, int *got_packet_ptr)
  1818. {
  1819. G723_1_Context *p = avctx->priv_data;
  1820. int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
  1821. int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
  1822. int16_t cur_lsp[LPC_ORDER];
  1823. int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
  1824. int16_t vector[FRAME_LEN + PITCH_MAX];
  1825. int offset, ret;
  1826. int16_t *in = (const int16_t *)frame->data[0];
  1827. HFParam hf[4];
  1828. int i, j;
  1829. highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
  1830. memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
  1831. memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
  1832. comp_lpc_coeff(vector, unq_lpc);
  1833. lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
  1834. lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
  1835. /* Update memory */
  1836. memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
  1837. sizeof(int16_t) * SUBFRAME_LEN);
  1838. memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
  1839. sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
  1840. memcpy(p->prev_data, in + HALF_FRAME_LEN,
  1841. sizeof(int16_t) * HALF_FRAME_LEN);
  1842. memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  1843. perceptual_filter(p, weighted_lpc, unq_lpc, vector);
  1844. memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  1845. memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
  1846. memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
  1847. scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
  1848. p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
  1849. p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
  1850. for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  1851. comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
  1852. memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
  1853. memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
  1854. memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
  1855. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  1856. harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
  1857. inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
  1858. lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
  1859. memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
  1860. offset = 0;
  1861. for (i = 0; i < SUBFRAMES; i++) {
  1862. int16_t impulse_resp[SUBFRAME_LEN];
  1863. int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
  1864. int16_t flt_in[SUBFRAME_LEN];
  1865. int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
  1866. /**
  1867. * Compute the combined impulse response of the synthesis filter,
  1868. * formant perceptual weighting filter and harmonic noise shaping filter
  1869. */
  1870. memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
  1871. memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
  1872. memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
  1873. flt_in[0] = 1 << 13; /* Unit impulse */
  1874. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  1875. zero, zero, flt_in, vector + PITCH_MAX, 1);
  1876. harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
  1877. /* Compute the combined zero input response */
  1878. flt_in[0] = 0;
  1879. memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
  1880. memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
  1881. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  1882. fir, iir, flt_in, vector + PITCH_MAX, 0);
  1883. memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
  1884. harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
  1885. acb_search(p, residual, impulse_resp, in, i);
  1886. gen_acb_excitation(residual, p->prev_excitation,p->pitch_lag[i >> 1],
  1887. &p->subframe[i], p->cur_rate);
  1888. sub_acb_contrib(residual, impulse_resp, in);
  1889. fcb_search(p, impulse_resp, in, i);
  1890. /* Reconstruct the excitation */
  1891. gen_acb_excitation(impulse_resp, p->prev_excitation, p->pitch_lag[i >> 1],
  1892. &p->subframe[i], RATE_6300);
  1893. memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
  1894. sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
  1895. for (j = 0; j < SUBFRAME_LEN; j++)
  1896. in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
  1897. memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
  1898. sizeof(int16_t) * SUBFRAME_LEN);
  1899. /* Update filter memories */
  1900. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  1901. p->perf_fir_mem, p->perf_iir_mem,
  1902. in, vector + PITCH_MAX, 0);
  1903. memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
  1904. sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
  1905. memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
  1906. sizeof(int16_t) * SUBFRAME_LEN);
  1907. in += SUBFRAME_LEN;
  1908. offset += LPC_ORDER;
  1909. }
  1910. if ((ret = ff_alloc_packet2(avctx, avpkt, 24)))
  1911. return ret;
  1912. *got_packet_ptr = 1;
  1913. avpkt->size = pack_bitstream(p, avpkt->data, avpkt->size);
  1914. return 0;
  1915. }
  1916. AVCodec ff_g723_1_encoder = {
  1917. .name = "g723_1",
  1918. .type = AVMEDIA_TYPE_AUDIO,
  1919. .id = AV_CODEC_ID_G723_1,
  1920. .priv_data_size = sizeof(G723_1_Context),
  1921. .init = g723_1_encode_init,
  1922. .encode2 = g723_1_encode_frame,
  1923. .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
  1924. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,
  1925. AV_SAMPLE_FMT_NONE},
  1926. };
  1927. #endif