1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /******************************************************************
12 
13  iLBC Speech Coder ANSI-C Source Code
14 
15  WebRtcIlbcfix_Encode.c
16 
17 ******************************************************************/
18 
19 #include <string.h>
20 
21 #include "defines.h"
22 #include "lpc_encode.h"
23 #include "frame_classify.h"
24 #include "state_search.h"
25 #include "state_construct.h"
26 #include "constants.h"
27 #include "cb_search.h"
28 #include "cb_construct.h"
29 #include "index_conv_enc.h"
30 #include "pack_bits.h"
31 #include "hp_input.h"
32 #include "rtc_base/checks.h"
33 
34 #ifdef SPLIT_10MS
35 #include "unpack_bits.h"
36 #include "index_conv_dec.h"
37 #endif
38 #ifndef WEBRTC_ARCH_BIG_ENDIAN
39 #include "swap_bytes.h"
40 #endif
41 
42 /*----------------------------------------------------------------*
43  *  main encoder function
44  *---------------------------------------------------------------*/
45 
WebRtcIlbcfix_EncodeImpl(uint16_t * bytes,const int16_t * block,IlbcEncoder * iLBCenc_inst)46 void WebRtcIlbcfix_EncodeImpl(
47     uint16_t *bytes,     /* (o) encoded data bits iLBC */
48     const int16_t *block, /* (i) speech vector to encode */
49     IlbcEncoder *iLBCenc_inst /* (i/o) the general encoder
50                                      state */
51                           ){
52   size_t n, meml_gotten, Nfor;
53   size_t diff, start_pos;
54   size_t index;
55   size_t subcount, subframe;
56   size_t start_count, end_count;
57   int16_t *residual;
58   int32_t en1, en2;
59   int16_t scale, max;
60   int16_t *syntdenum;
61   int16_t *decresidual;
62   int16_t *reverseResidual;
63   int16_t *reverseDecresidual;
64   /* Stack based */
65   int16_t weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
66   int16_t dataVec[BLOCKL_MAX + LPC_FILTERORDER];
67   int16_t memVec[CB_MEML+CB_FILTERLEN];
68   int16_t bitsMemory[sizeof(iLBC_bits)/sizeof(int16_t)];
69   iLBC_bits *iLBCbits_inst = (iLBC_bits*)bitsMemory;
70 
71 
72 #ifdef SPLIT_10MS
73   int16_t *weightdenumbuf = iLBCenc_inst->weightdenumbuf;
74   int16_t last_bit;
75 #endif
76 
77   int16_t *data = &dataVec[LPC_FILTERORDER];
78   int16_t *mem = &memVec[CB_HALFFILTERLEN];
79 
80   /* Reuse som buffers to save stack memory */
81   residual = &iLBCenc_inst->lpc_buffer[LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl];
82   syntdenum = mem;      /* syntdenum[(LPC_FILTERORDER + 1)*NSUB_MAX] and mem are used non overlapping in the code */
83   decresidual = residual;     /* Already encoded residual is overwritten by the decoded version */
84   reverseResidual = data;     /* data and reverseResidual are used non overlapping in the code */
85   reverseDecresidual = reverseResidual; /* Already encoded residual is overwritten by the decoded version */
86 
87 #ifdef SPLIT_10MS
88 
89   WebRtcSpl_MemSetW16 (  (int16_t *) iLBCbits_inst, 0,
90                          sizeof(iLBC_bits) / sizeof(int16_t)  );
91 
92   start_pos = iLBCenc_inst->start_pos;
93   diff = iLBCenc_inst->diff;
94 
95   if (iLBCenc_inst->section != 0){
96     WEBRTC_SPL_MEMCPY_W16 (weightdenum, weightdenumbuf,
97                            SCRATCH_ENCODE_DATAVEC - SCRATCH_ENCODE_WEIGHTDENUM);
98     /* Un-Packetize the frame into parameters */
99     last_bit = WebRtcIlbcfix_UnpackBits (iLBCenc_inst->bytes, iLBCbits_inst, iLBCenc_inst->mode);
100     if (last_bit)
101       return;
102     /* adjust index */
103     WebRtcIlbcfix_IndexConvDec (iLBCbits_inst->cb_index);
104 
105     if (iLBCenc_inst->section == 1){
106       /* Save first 80 samples of a 160/240 sample frame for 20/30msec */
107       WEBRTC_SPL_MEMCPY_W16 (iLBCenc_inst->past_samples, block, 80);
108     }
109     else{ // iLBCenc_inst->section == 2 AND mode = 30ms
110       /* Save second 80 samples of a 240 sample frame for 30msec */
111       WEBRTC_SPL_MEMCPY_W16 (iLBCenc_inst->past_samples + 80, block, 80);
112     }
113   }
114   else{ // iLBCenc_inst->section == 0
115     /* form a complete frame of 160/240 for 20msec/30msec mode */
116     WEBRTC_SPL_MEMCPY_W16 (data + (iLBCenc_inst->mode * 8) - 80, block, 80);
117     WEBRTC_SPL_MEMCPY_W16 (data, iLBCenc_inst->past_samples,
118                            (iLBCenc_inst->mode * 8) - 80);
119     iLBCenc_inst->Nfor_flag = 0;
120     iLBCenc_inst->Nback_flag = 0;
121 #else
122     /* copy input block to data*/
123     WEBRTC_SPL_MEMCPY_W16(data,block,iLBCenc_inst->blockl);
124 #endif
125 
126     /* high pass filtering of input signal and scale down the residual (*0.5) */
127     WebRtcIlbcfix_HpInput(data, (int16_t*)WebRtcIlbcfix_kHpInCoefs,
128                           iLBCenc_inst->hpimemy, iLBCenc_inst->hpimemx,
129                           iLBCenc_inst->blockl);
130 
131     /* LPC of hp filtered input data */
132     WebRtcIlbcfix_LpcEncode(syntdenum, weightdenum, iLBCbits_inst->lsf, data,
133                             iLBCenc_inst);
134 
135     /* Set up state */
136     WEBRTC_SPL_MEMCPY_W16(dataVec, iLBCenc_inst->anaMem, LPC_FILTERORDER);
137 
138     /* inverse filter to get residual */
139     for (n=0; n<iLBCenc_inst->nsub; n++ ) {
140       WebRtcSpl_FilterMAFastQ12(
141           &data[n*SUBL], &residual[n*SUBL],
142           &syntdenum[n*(LPC_FILTERORDER+1)],
143           LPC_FILTERORDER+1, SUBL);
144     }
145 
146     /* Copy the state for next frame */
147     WEBRTC_SPL_MEMCPY_W16(iLBCenc_inst->anaMem, &data[iLBCenc_inst->blockl-LPC_FILTERORDER], LPC_FILTERORDER);
148 
149     /* find state location */
150 
151     iLBCbits_inst->startIdx = WebRtcIlbcfix_FrameClassify(iLBCenc_inst,residual);
152 
153     /* check if state should be in first or last part of the
154        two subframes */
155 
156     index = (iLBCbits_inst->startIdx-1)*SUBL;
157     max=WebRtcSpl_MaxAbsValueW16(&residual[index], 2*SUBL);
158     scale = WebRtcSpl_GetSizeInBits((uint32_t)(max * max));
159 
160     /* Scale to maximum 25 bits so that the MAC won't cause overflow */
161     scale = scale - 25;
162     if(scale < 0) {
163       scale = 0;
164     }
165 
166     diff = STATE_LEN - iLBCenc_inst->state_short_len;
167     en1=WebRtcSpl_DotProductWithScale(&residual[index], &residual[index],
168                                       iLBCenc_inst->state_short_len, scale);
169     index += diff;
170     en2=WebRtcSpl_DotProductWithScale(&residual[index], &residual[index],
171                                       iLBCenc_inst->state_short_len, scale);
172     if (en1 > en2) {
173       iLBCbits_inst->state_first = 1;
174       start_pos = (iLBCbits_inst->startIdx-1)*SUBL;
175     } else {
176       iLBCbits_inst->state_first = 0;
177       start_pos = (iLBCbits_inst->startIdx-1)*SUBL + diff;
178     }
179 
180     /* scalar quantization of state */
181 
182     WebRtcIlbcfix_StateSearch(iLBCenc_inst, iLBCbits_inst, &residual[start_pos],
183                               &syntdenum[(iLBCbits_inst->startIdx-1)*(LPC_FILTERORDER+1)],
184                               &weightdenum[(iLBCbits_inst->startIdx-1)*(LPC_FILTERORDER+1)]);
185 
186     WebRtcIlbcfix_StateConstruct(iLBCbits_inst->idxForMax, iLBCbits_inst->idxVec,
187                                  &syntdenum[(iLBCbits_inst->startIdx-1)*(LPC_FILTERORDER+1)],
188                                  &decresidual[start_pos], iLBCenc_inst->state_short_len
189                                  );
190 
191     /* predictive quantization in state */
192 
193     if (iLBCbits_inst->state_first) { /* put adaptive part in the end */
194 
195       /* setup memory */
196 
197       WebRtcSpl_MemSetW16(mem, 0, CB_MEML - iLBCenc_inst->state_short_len);
198       WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-iLBCenc_inst->state_short_len,
199                             decresidual+start_pos, iLBCenc_inst->state_short_len);
200 
201       /* encode subframes */
202 
203       WebRtcIlbcfix_CbSearch(iLBCenc_inst, iLBCbits_inst->cb_index, iLBCbits_inst->gain_index,
204                              &residual[start_pos+iLBCenc_inst->state_short_len],
205                              mem+CB_MEML-ST_MEM_L_TBL, ST_MEM_L_TBL, diff,
206                              &weightdenum[iLBCbits_inst->startIdx*(LPC_FILTERORDER+1)], 0);
207 
208       /* construct decoded vector */
209 
210       RTC_CHECK(WebRtcIlbcfix_CbConstruct(
211           &decresidual[start_pos + iLBCenc_inst->state_short_len],
212           iLBCbits_inst->cb_index, iLBCbits_inst->gain_index,
213           mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, diff));
214 
215     }
216     else { /* put adaptive part in the beginning */
217 
218       /* create reversed vectors for prediction */
219 
220       WebRtcSpl_MemCpyReversedOrder(&reverseResidual[diff-1],
221                                     &residual[(iLBCbits_inst->startIdx+1)*SUBL-STATE_LEN], diff);
222 
223       /* setup memory */
224 
225       meml_gotten = iLBCenc_inst->state_short_len;
226       WebRtcSpl_MemCpyReversedOrder(&mem[CB_MEML-1], &decresidual[start_pos], meml_gotten);
227       WebRtcSpl_MemSetW16(mem, 0, CB_MEML - iLBCenc_inst->state_short_len);
228 
229       /* encode subframes */
230       WebRtcIlbcfix_CbSearch(iLBCenc_inst, iLBCbits_inst->cb_index, iLBCbits_inst->gain_index,
231                              reverseResidual, mem+CB_MEML-ST_MEM_L_TBL, ST_MEM_L_TBL, diff,
232                              &weightdenum[(iLBCbits_inst->startIdx-1)*(LPC_FILTERORDER+1)],
233                              0);
234 
235       /* construct decoded vector */
236       RTC_CHECK(WebRtcIlbcfix_CbConstruct(
237             reverseDecresidual, iLBCbits_inst->cb_index,
238             iLBCbits_inst->gain_index, mem + CB_MEML - ST_MEM_L_TBL,
239             ST_MEM_L_TBL, diff));
240 
241       /* get decoded residual from reversed vector */
242 
243       WebRtcSpl_MemCpyReversedOrder(&decresidual[start_pos-1], reverseDecresidual, diff);
244     }
245 
246 #ifdef SPLIT_10MS
247     iLBCenc_inst->start_pos = start_pos;
248     iLBCenc_inst->diff = diff;
249     iLBCenc_inst->section++;
250     /* adjust index */
251     WebRtcIlbcfix_IndexConvEnc (iLBCbits_inst->cb_index);
252     /* Packetize the parameters into the frame */
253     WebRtcIlbcfix_PackBits (iLBCenc_inst->bytes, iLBCbits_inst, iLBCenc_inst->mode);
254     WEBRTC_SPL_MEMCPY_W16 (weightdenumbuf, weightdenum,
255                            SCRATCH_ENCODE_DATAVEC - SCRATCH_ENCODE_WEIGHTDENUM);
256     return;
257   }
258 #endif
259 
260   /* forward prediction of subframes */
261 
262   Nfor = iLBCenc_inst->nsub-iLBCbits_inst->startIdx-1;
263 
264   /* counter for predicted subframes */
265 #ifdef SPLIT_10MS
266   if (iLBCenc_inst->mode == 20)
267   {
268     subcount = 1;
269   }
270   if (iLBCenc_inst->mode == 30)
271   {
272     if (iLBCenc_inst->section == 1)
273     {
274       subcount = 1;
275     }
276     if (iLBCenc_inst->section == 2)
277     {
278       subcount = 3;
279     }
280   }
281 #else
282   subcount=1;
283 #endif
284 
285   if( Nfor > 0 ){
286 
287     /* setup memory */
288 
289     WebRtcSpl_MemSetW16(mem, 0, CB_MEML-STATE_LEN);
290     WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-STATE_LEN,
291                           decresidual+(iLBCbits_inst->startIdx-1)*SUBL, STATE_LEN);
292 
293 #ifdef SPLIT_10MS
294     if (iLBCenc_inst->Nfor_flag > 0)
295     {
296       for (subframe = 0; subframe < WEBRTC_SPL_MIN (Nfor, 2); subframe++)
297       {
298         /* update memory */
299         WEBRTC_SPL_MEMCPY_W16 (mem, mem + SUBL, (CB_MEML - SUBL));
300         WEBRTC_SPL_MEMCPY_W16 (mem + CB_MEML - SUBL,
301                                &decresidual[(iLBCbits_inst->startIdx + 1 +
302                                              subframe) * SUBL], SUBL);
303       }
304     }
305 
306     iLBCenc_inst->Nfor_flag++;
307 
308     if (iLBCenc_inst->mode == 20)
309     {
310       start_count = 0;
311       end_count = Nfor;
312     }
313     if (iLBCenc_inst->mode == 30)
314     {
315       if (iLBCenc_inst->section == 1)
316       {
317         start_count = 0;
318         end_count = WEBRTC_SPL_MIN (Nfor, (size_t)2);
319       }
320       if (iLBCenc_inst->section == 2)
321       {
322         start_count = WEBRTC_SPL_MIN (Nfor, (size_t)2);
323         end_count = Nfor;
324       }
325     }
326 #else
327     start_count = 0;
328     end_count = Nfor;
329 #endif
330 
331     /* loop over subframes to encode */
332 
333     for (subframe = start_count; subframe < end_count; subframe++){
334 
335       /* encode subframe */
336 
337       WebRtcIlbcfix_CbSearch(iLBCenc_inst, iLBCbits_inst->cb_index+subcount*CB_NSTAGES,
338                              iLBCbits_inst->gain_index+subcount*CB_NSTAGES,
339                              &residual[(iLBCbits_inst->startIdx+1+subframe)*SUBL],
340                              mem, MEM_LF_TBL, SUBL,
341                              &weightdenum[(iLBCbits_inst->startIdx+1+subframe)*(LPC_FILTERORDER+1)],
342                              subcount);
343 
344       /* construct decoded vector */
345       RTC_CHECK(WebRtcIlbcfix_CbConstruct(
346             &decresidual[(iLBCbits_inst->startIdx + 1 + subframe) * SUBL],
347             iLBCbits_inst->cb_index + subcount * CB_NSTAGES,
348             iLBCbits_inst->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL,
349             SUBL));
350 
351       /* update memory */
352 
353       memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
354       WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
355                             &decresidual[(iLBCbits_inst->startIdx+1+subframe)*SUBL], SUBL);
356 
357       subcount++;
358     }
359   }
360 
361 #ifdef SPLIT_10MS
362   if ((iLBCenc_inst->section == 1) &&
363       (iLBCenc_inst->mode == 30) && (Nfor > 0) && (end_count == 2))
364   {
365     iLBCenc_inst->section++;
366     /* adjust index */
367     WebRtcIlbcfix_IndexConvEnc (iLBCbits_inst->cb_index);
368     /* Packetize the parameters into the frame */
369     WebRtcIlbcfix_PackBits (iLBCenc_inst->bytes, iLBCbits_inst, iLBCenc_inst->mode);
370     WEBRTC_SPL_MEMCPY_W16 (weightdenumbuf, weightdenum,
371                            SCRATCH_ENCODE_DATAVEC - SCRATCH_ENCODE_WEIGHTDENUM);
372     return;
373   }
374 #endif
375 
376   /* backward prediction of subframes */
377 
378   if (iLBCbits_inst->startIdx > 1) {
379 
380     /* create reverse order vectors
381        (The decresidual does not need to be copied since it is
382        contained in the same vector as the residual)
383     */
384 
385     size_t Nback = iLBCbits_inst->startIdx - 1;
386     WebRtcSpl_MemCpyReversedOrder(&reverseResidual[Nback*SUBL-1], residual, Nback*SUBL);
387 
388     /* setup memory */
389 
390     meml_gotten = SUBL*(iLBCenc_inst->nsub+1-iLBCbits_inst->startIdx);
391     if( meml_gotten > CB_MEML ) {
392       meml_gotten=CB_MEML;
393     }
394 
395     WebRtcSpl_MemCpyReversedOrder(&mem[CB_MEML-1], &decresidual[Nback*SUBL], meml_gotten);
396     WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten);
397 
398 #ifdef SPLIT_10MS
399     if (iLBCenc_inst->Nback_flag > 0)
400     {
401       for (subframe = 0; subframe < WEBRTC_SPL_MAX (2 - Nfor, 0); subframe++)
402       {
403         /* update memory */
404         WEBRTC_SPL_MEMCPY_W16 (mem, mem + SUBL, (CB_MEML - SUBL));
405         WEBRTC_SPL_MEMCPY_W16 (mem + CB_MEML - SUBL,
406                                &reverseDecresidual[subframe * SUBL], SUBL);
407       }
408     }
409 
410     iLBCenc_inst->Nback_flag++;
411 
412 
413     if (iLBCenc_inst->mode == 20)
414     {
415       start_count = 0;
416       end_count = Nback;
417     }
418     if (iLBCenc_inst->mode == 30)
419     {
420       if (iLBCenc_inst->section == 1)
421       {
422         start_count = 0;
423         end_count = (Nfor >= 2) ? 0 : (2 - NFor);
424       }
425       if (iLBCenc_inst->section == 2)
426       {
427         start_count = (Nfor >= 2) ? 0 : (2 - NFor);
428         end_count = Nback;
429       }
430     }
431 #else
432     start_count = 0;
433     end_count = Nback;
434 #endif
435 
436     /* loop over subframes to encode */
437 
438     for (subframe = start_count; subframe < end_count; subframe++){
439 
440       /* encode subframe */
441 
442       WebRtcIlbcfix_CbSearch(iLBCenc_inst, iLBCbits_inst->cb_index+subcount*CB_NSTAGES,
443                              iLBCbits_inst->gain_index+subcount*CB_NSTAGES, &reverseResidual[subframe*SUBL],
444                              mem, MEM_LF_TBL, SUBL,
445                              &weightdenum[(iLBCbits_inst->startIdx-2-subframe)*(LPC_FILTERORDER+1)],
446                              subcount);
447 
448       /* construct decoded vector */
449       RTC_CHECK(WebRtcIlbcfix_CbConstruct(
450             &reverseDecresidual[subframe * SUBL],
451             iLBCbits_inst->cb_index + subcount * CB_NSTAGES,
452             iLBCbits_inst->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL,
453             SUBL));
454 
455       /* update memory */
456       memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
457       WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
458                             &reverseDecresidual[subframe*SUBL], SUBL);
459 
460       subcount++;
461 
462     }
463 
464     /* get decoded residual from reversed vector */
465 
466     WebRtcSpl_MemCpyReversedOrder(&decresidual[SUBL*Nback-1], reverseDecresidual, SUBL*Nback);
467   }
468   /* end encoding part */
469 
470   /* adjust index */
471 
472   WebRtcIlbcfix_IndexConvEnc(iLBCbits_inst->cb_index);
473 
474   /* Packetize the parameters into the frame */
475 
476 #ifdef SPLIT_10MS
477   if( (iLBCenc_inst->mode==30) && (iLBCenc_inst->section==1) ){
478     WebRtcIlbcfix_PackBits(iLBCenc_inst->bytes, iLBCbits_inst, iLBCenc_inst->mode);
479   }
480   else{
481     WebRtcIlbcfix_PackBits(bytes, iLBCbits_inst, iLBCenc_inst->mode);
482   }
483 #else
484   WebRtcIlbcfix_PackBits(bytes, iLBCbits_inst, iLBCenc_inst->mode);
485 #endif
486 
487 #ifndef WEBRTC_ARCH_BIG_ENDIAN
488   /* Swap bytes for LITTLE ENDIAN since the packbits()
489      function assumes BIG_ENDIAN machine */
490 #ifdef SPLIT_10MS
491   if (( (iLBCenc_inst->section == 1) && (iLBCenc_inst->mode == 20) ) ||
492       ( (iLBCenc_inst->section == 2) && (iLBCenc_inst->mode == 30) )){
493     WebRtcIlbcfix_SwapBytes(bytes, iLBCenc_inst->no_of_words, bytes);
494   }
495 #else
496   WebRtcIlbcfix_SwapBytes(bytes, iLBCenc_inst->no_of_words, bytes);
497 #endif
498 #endif
499 
500 #ifdef SPLIT_10MS
501   if (subcount == (iLBCenc_inst->nsub - 1))
502   {
503     iLBCenc_inst->section = 0;
504   }
505   else
506   {
507     iLBCenc_inst->section++;
508     WEBRTC_SPL_MEMCPY_W16 (weightdenumbuf, weightdenum,
509                            SCRATCH_ENCODE_DATAVEC - SCRATCH_ENCODE_WEIGHTDENUM);
510   }
511 #endif
512 
513 }
514