niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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_FrameClassify.c |
| 16 | |
| 17 | ******************************************************************/ |
| 18 | |
Timothy Gu | 3111783 | 2020-12-19 06:25:57 | [diff] [blame] | 19 | #include "modules/audio_coding/codecs/ilbc/frame_classify.h" |
| 20 | |
Mirko Bonadei | 06c2aa9 | 2018-02-01 14:11:41 | [diff] [blame] | 21 | #include "modules/audio_coding/codecs/ilbc/constants.h" |
Timothy Gu | 3111783 | 2020-12-19 06:25:57 | [diff] [blame] | 22 | #include "modules/audio_coding/codecs/ilbc/defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 23 | |
| 24 | /*----------------------------------------------------------------* |
| 25 | * Classification of subframes to localize start state |
| 26 | *---------------------------------------------------------------*/ |
| 27 | |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 28 | size_t WebRtcIlbcfix_FrameClassify( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 29 | /* (o) Index to the max-energy sub frame */ |
pbos@webrtc.org | eb54446 | 2014-12-17 15:23:29 | [diff] [blame] | 30 | IlbcEncoder *iLBCenc_inst, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 31 | /* (i/o) the encoder state structure */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 | [diff] [blame] | 32 | int16_t *residualFIX /* (i) lpc residual signal */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 33 | ){ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 | [diff] [blame] | 34 | int16_t max, scale; |
| 35 | int32_t ssqEn[NSUB_MAX-1]; |
| 36 | int16_t *ssqPtr; |
| 37 | int32_t *seqEnPtr; |
| 38 | int32_t maxW32; |
| 39 | int16_t scale1; |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 40 | size_t pos; |
| 41 | size_t n; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | Calculate the energy of each of the 80 sample blocks |
| 45 | in the draft the 4 first and last samples are windowed with 1/5...4/5 |
| 46 | and 4/5...1/5 respectively. To simplify for the fixpoint we have changed |
| 47 | this to 0 0 1 1 and 1 1 0 0 |
| 48 | */ |
| 49 | |
| 50 | max = WebRtcSpl_MaxAbsValueW16(residualFIX, iLBCenc_inst->blockl); |
bjornv@webrtc.org | ba97ea6 | 2015-02-13 09:51:40 | [diff] [blame] | 51 | scale = WebRtcSpl_GetSizeInBits((uint32_t)(max * max)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 52 | |
| 53 | /* Scale to maximum 24 bits so that it won't overflow for 76 samples */ |
| 54 | scale = scale-24; |
| 55 | scale1 = WEBRTC_SPL_MAX(0, scale); |
| 56 | |
| 57 | /* Calculate energies */ |
| 58 | ssqPtr=residualFIX + 2; |
| 59 | seqEnPtr=ssqEn; |
| 60 | for (n=(iLBCenc_inst->nsub-1); n>0; n--) { |
| 61 | (*seqEnPtr) = WebRtcSpl_DotProductWithScale(ssqPtr, ssqPtr, 76, scale1); |
| 62 | ssqPtr += 40; |
| 63 | seqEnPtr++; |
| 64 | } |
| 65 | |
| 66 | /* Scale to maximum 20 bits in order to allow for the 11 bit window */ |
Peter Kasting | b7e5054 | 2015-06-11 19:55:50 | [diff] [blame] | 67 | maxW32 = WebRtcSpl_MaxValueW32(ssqEn, iLBCenc_inst->nsub - 1); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 68 | scale = WebRtcSpl_GetSizeInBits(maxW32) - 20; |
| 69 | scale1 = WEBRTC_SPL_MAX(0, scale); |
| 70 | |
| 71 | /* Window each 80 block with the ssqEn_winTbl window to give higher probability for |
| 72 | the blocks in the middle |
| 73 | */ |
| 74 | seqEnPtr=ssqEn; |
| 75 | if (iLBCenc_inst->mode==20) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 | [diff] [blame] | 76 | ssqPtr=(int16_t*)WebRtcIlbcfix_kStartSequenceEnrgWin+1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 77 | } else { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 | [diff] [blame] | 78 | ssqPtr=(int16_t*)WebRtcIlbcfix_kStartSequenceEnrgWin; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 79 | } |
| 80 | for (n=(iLBCenc_inst->nsub-1); n>0; n--) { |
| 81 | (*seqEnPtr)=WEBRTC_SPL_MUL(((*seqEnPtr)>>scale1), (*ssqPtr)); |
| 82 | seqEnPtr++; |
| 83 | ssqPtr++; |
| 84 | } |
| 85 | |
| 86 | /* Extract the best choise of start state */ |
Peter Kasting | 1380e26 | 2015-08-29 00:31:03 | [diff] [blame] | 87 | pos = WebRtcSpl_MaxIndexW32(ssqEn, iLBCenc_inst->nsub - 1) + 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 88 | |
| 89 | return(pos); |
| 90 | } |