decode_frame.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main.h"
  31. #include "stack_alloc.h"
  32. #include "PLC.h"
  33. /****************/
  34. /* Decode frame */
  35. /****************/
  36. opus_int silk_decode_frame(
  37. silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */
  38. ec_dec *psRangeDec, /* I/O Compressor data structure */
  39. opus_int16 pOut[], /* O Pointer to output speech frame */
  40. opus_int32 *pN, /* O Pointer to size of output frame */
  41. opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */
  42. opus_int condCoding, /* I The type of conditional coding to use */
  43. int arch /* I Run-time architecture */
  44. )
  45. {
  46. VARDECL( silk_decoder_control, psDecCtrl );
  47. opus_int L, mv_len, ret = 0;
  48. SAVE_STACK;
  49. L = psDec->frame_length;
  50. ALLOC( psDecCtrl, 1, silk_decoder_control );
  51. psDecCtrl->LTP_scale_Q14 = 0;
  52. /* Safety checks */
  53. silk_assert( L > 0 && L <= MAX_FRAME_LENGTH );
  54. if( lostFlag == FLAG_DECODE_NORMAL ||
  55. ( lostFlag == FLAG_DECODE_LBRR && psDec->LBRR_flags[ psDec->nFramesDecoded ] == 1 ) )
  56. {
  57. VARDECL( opus_int16, pulses );
  58. ALLOC( pulses, (L + SHELL_CODEC_FRAME_LENGTH - 1) &
  59. ~(SHELL_CODEC_FRAME_LENGTH - 1), opus_int16 );
  60. /*********************************************/
  61. /* Decode quantization indices of side info */
  62. /*********************************************/
  63. silk_decode_indices( psDec, psRangeDec, psDec->nFramesDecoded, lostFlag, condCoding );
  64. /*********************************************/
  65. /* Decode quantization indices of excitation */
  66. /*********************************************/
  67. silk_decode_pulses( psRangeDec, pulses, psDec->indices.signalType,
  68. psDec->indices.quantOffsetType, psDec->frame_length );
  69. /********************************************/
  70. /* Decode parameters and pulse signal */
  71. /********************************************/
  72. silk_decode_parameters( psDec, psDecCtrl, condCoding );
  73. /********************************************************/
  74. /* Run inverse NSQ */
  75. /********************************************************/
  76. silk_decode_core( psDec, psDecCtrl, pOut, pulses, arch );
  77. /********************************************************/
  78. /* Update PLC state */
  79. /********************************************************/
  80. silk_PLC( psDec, psDecCtrl, pOut, 0, arch );
  81. psDec->lossCnt = 0;
  82. psDec->prevSignalType = psDec->indices.signalType;
  83. silk_assert( psDec->prevSignalType >= 0 && psDec->prevSignalType <= 2 );
  84. /* A frame has been decoded without errors */
  85. psDec->first_frame_after_reset = 0;
  86. } else {
  87. /* Handle packet loss by extrapolation */
  88. silk_PLC( psDec, psDecCtrl, pOut, 1, arch );
  89. }
  90. /*************************/
  91. /* Update output buffer. */
  92. /*************************/
  93. silk_assert( psDec->ltp_mem_length >= psDec->frame_length );
  94. mv_len = psDec->ltp_mem_length - psDec->frame_length;
  95. silk_memmove( psDec->outBuf, &psDec->outBuf[ psDec->frame_length ], mv_len * sizeof(opus_int16) );
  96. silk_memcpy( &psDec->outBuf[ mv_len ], pOut, psDec->frame_length * sizeof( opus_int16 ) );
  97. /************************************************/
  98. /* Comfort noise generation / estimation */
  99. /************************************************/
  100. silk_CNG( psDec, psDecCtrl, pOut, L );
  101. /****************************************************************/
  102. /* Ensure smooth connection of extrapolated and good frames */
  103. /****************************************************************/
  104. silk_PLC_glue_frames( psDec, pOut, L );
  105. /* Update some decoder state variables */
  106. psDec->lagPrev = psDecCtrl->pitchL[ psDec->nb_subfr - 1 ];
  107. /* Set output frame length */
  108. *pN = L;
  109. RESTORE_STACK;
  110. return ret;
  111. }