You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.5KB

  1. /*
  2. * AMR Audio decoder stub
  3. * Copyright (c) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. //#define DEBUG
  20. #define MMS_IO
  21. #include "avcodec.h"
  22. #include "amr/sp_dec.h"
  23. #include "amr/d_homing.h"
  24. #include "amr/typedef.h"
  25. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  26. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  27. typedef struct AMRDecodeContext {
  28. int frameCount;
  29. Speech_Decode_FrameState *speech_decoder_state;
  30. enum RXFrameType rx_type;
  31. enum Mode mode;
  32. Word16 reset_flag;
  33. Word16 reset_flag_old;
  34. } AMRDecodeContext;
  35. static int amr_nb_decode_init(AVCodecContext * avctx)
  36. {
  37. AMRDecodeContext *s = avctx->priv_data;
  38. s->frameCount=0;
  39. s->speech_decoder_state=NULL;
  40. s->rx_type = (enum RXFrameType)0;
  41. s->mode= (enum Mode)0;
  42. s->reset_flag=0;
  43. s->reset_flag_old=1;
  44. if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
  45. {
  46. printf("error\r\n");
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. static int amr_decode_close(AVCodecContext * avctx)
  52. {
  53. AMRDecodeContext *s = avctx->priv_data;
  54. Speech_Decode_Frame_exit(&s->speech_decoder_state);
  55. }
  56. static int amr_nb_decode_frame(AVCodecContext * avctx,
  57. void *data, int *data_size,
  58. uint8_t * buf, int buf_size)
  59. {
  60. AMRDecodeContext *s = avctx->priv_data;
  61. uint8_t*amrData=buf;
  62. int offset=0;
  63. UWord8 toc, q, ft;
  64. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  65. Word16 *synth;
  66. UWord8 *packed_bits;
  67. static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  68. int i;
  69. //printf("amr_decode_frame data=0x%X data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",data,&data_size,buf,buf_size,s->frameCount);
  70. synth=data;
  71. while(offset<buf_size)
  72. {
  73. toc=amrData[offset++];
  74. /* read rest of the frame based on ToC byte */
  75. q = (toc >> 2) & 0x01;
  76. ft = (toc >> 3) & 0x0F;
  77. packed_bits=amrData+offset;
  78. offset+=packed_size[ft];
  79. //Unsort and unpack bits
  80. s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
  81. //We have a new frame
  82. s->frameCount++;
  83. if (s->rx_type == RX_NO_DATA)
  84. {
  85. s->mode = s->speech_decoder_state->prev_mode;
  86. }
  87. else {
  88. s->speech_decoder_state->prev_mode = s->mode;
  89. }
  90. /* if homed: check if this frame is another homing frame */
  91. if (s->reset_flag_old == 1)
  92. {
  93. /* only check until end of first subframe */
  94. s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
  95. }
  96. /* produce encoder homing frame if homed & input=decoder homing frame */
  97. if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
  98. {
  99. for (i = 0; i < L_FRAME; i++)
  100. {
  101. synth[i] = EHF_MASK;
  102. }
  103. }
  104. else
  105. {
  106. /* decode frame */
  107. Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
  108. }
  109. //Each AMR-frame results in 160 16-bit samples
  110. *data_size+=160*2;
  111. synth+=160;
  112. /* if not homed: check whether current frame is a homing frame */
  113. if (s->reset_flag_old == 0)
  114. {
  115. /* check whole frame */
  116. s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
  117. }
  118. /* reset decoder if current frame is a homing frame */
  119. if (s->reset_flag != 0)
  120. {
  121. Speech_Decode_Frame_reset(s->speech_decoder_state);
  122. }
  123. s->reset_flag_old = s->reset_flag;
  124. }
  125. return buf_size;
  126. }
  127. AVCodec amr_nb_decoder =
  128. {
  129. "amr_nb",
  130. CODEC_TYPE_AUDIO,
  131. CODEC_ID_AMR_NB,
  132. sizeof(AMRDecodeContext),
  133. amr_nb_decode_init,
  134. NULL,
  135. amr_decode_close,
  136. amr_nb_decode_frame,
  137. };