libopencore-amr.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * AMR Audio decoder stub
  3. * Copyright (c) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. static void amr_decode_fix_avctx(AVCodecContext *avctx)
  23. {
  24. const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
  25. if (!avctx->sample_rate)
  26. avctx->sample_rate = 8000 * is_amr_wb;
  27. if (!avctx->channels)
  28. avctx->channels = 1;
  29. avctx->frame_size = 160 * is_amr_wb;
  30. avctx->sample_fmt = SAMPLE_FMT_S16;
  31. }
  32. #if CONFIG_LIBOPENCORE_AMRNB
  33. #include <opencore-amrnb/interf_dec.h>
  34. #include <opencore-amrnb/interf_enc.h>
  35. static const char nb_bitrate_unsupported[] =
  36. "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
  37. /* Common code for fixed and float version*/
  38. typedef struct AMR_bitrates {
  39. int rate;
  40. enum Mode mode;
  41. } AMR_bitrates;
  42. /* Match desired bitrate */
  43. static int getBitrateMode(int bitrate)
  44. {
  45. /* make the correspondance between bitrate and mode */
  46. AMR_bitrates rates[] = { { 4750, MR475},
  47. { 5150, MR515},
  48. { 5900, MR59},
  49. { 6700, MR67},
  50. { 7400, MR74},
  51. { 7950, MR795},
  52. {10200, MR102},
  53. {12200, MR122}, };
  54. int i;
  55. for (i = 0; i < 8; i++)
  56. if (rates[i].rate == bitrate)
  57. return rates[i].mode;
  58. /* no bitrate matching, return an error */
  59. return -1;
  60. }
  61. typedef struct AMRContext {
  62. int frameCount;
  63. void *decState;
  64. int *enstate;
  65. int enc_bitrate;
  66. } AMRContext;
  67. static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
  68. {
  69. AMRContext *s = avctx->priv_data;
  70. s->frameCount = 0;
  71. s->decState = Decoder_Interface_init();
  72. if (!s->decState) {
  73. av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
  74. return -1;
  75. }
  76. amr_decode_fix_avctx(avctx);
  77. if (avctx->channels > 1) {
  78. av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
  84. {
  85. AMRContext *s = avctx->priv_data;
  86. Decoder_Interface_exit(s->decState);
  87. return 0;
  88. }
  89. static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
  90. int *data_size,
  91. const uint8_t *buf, int buf_size)
  92. {
  93. AMRContext *s = avctx->priv_data;
  94. const uint8_t *amrData = buf;
  95. static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  96. enum Mode dec_mode;
  97. int packet_size;
  98. /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
  99. buf, buf_size, s->frameCount); */
  100. dec_mode = (buf[0] >> 3) & 0x000F;
  101. packet_size = block_size[dec_mode] + 1;
  102. if (packet_size > buf_size) {
  103. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  104. buf_size, packet_size);
  105. return -1;
  106. }
  107. s->frameCount++;
  108. /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
  109. packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
  110. /* call decoder */
  111. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  112. *data_size = 160 * 2;
  113. return packet_size;
  114. }
  115. AVCodec libopencore_amrnb_decoder = {
  116. "libopencore_amrnb",
  117. CODEC_TYPE_AUDIO,
  118. CODEC_ID_AMR_NB,
  119. sizeof(AMRContext),
  120. amr_nb_decode_init,
  121. NULL,
  122. amr_nb_decode_close,
  123. amr_nb_decode_frame,
  124. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  125. };
  126. static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
  127. {
  128. AMRContext *s = avctx->priv_data;
  129. s->frameCount = 0;
  130. if (avctx->sample_rate != 8000) {
  131. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  132. return -1;
  133. }
  134. if (avctx->channels != 1) {
  135. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  136. return -1;
  137. }
  138. avctx->frame_size = 160;
  139. avctx->coded_frame = avcodec_alloc_frame();
  140. s->enstate=Encoder_Interface_init(0);
  141. if (!s->enstate) {
  142. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  143. return -1;
  144. }
  145. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  146. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
  152. {
  153. AMRContext *s = avctx->priv_data;
  154. Encoder_Interface_exit(s->enstate);
  155. av_freep(&avctx->coded_frame);
  156. return 0;
  157. }
  158. static int amr_nb_encode_frame(AVCodecContext *avctx,
  159. unsigned char *frame/*out*/,
  160. int buf_size, void *data/*in*/)
  161. {
  162. AMRContext *s = avctx->priv_data;
  163. int written;
  164. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  165. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  166. return -1;
  167. }
  168. written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
  169. frame, 0);
  170. /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
  171. written, s->enc_bitrate, frame[0] ); */
  172. return written;
  173. }
  174. AVCodec libopencore_amrnb_encoder = {
  175. "libopencore_amrnb",
  176. CODEC_TYPE_AUDIO,
  177. CODEC_ID_AMR_NB,
  178. sizeof(AMRContext),
  179. amr_nb_encode_init,
  180. amr_nb_encode_frame,
  181. amr_nb_encode_close,
  182. NULL,
  183. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  184. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  185. };
  186. #endif
  187. /* -----------AMR wideband ------------*/
  188. #if CONFIG_LIBOPENCORE_AMRWB
  189. #ifdef _TYPEDEF_H
  190. //To avoid duplicate typedefs from typedef in amr-nb
  191. #define typedef_h
  192. #endif
  193. #include <opencore-amrwb/dec_if.h>
  194. #include <opencore-amrwb/if_rom.h>
  195. static const char wb_bitrate_unsupported[] =
  196. "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k\n";
  197. /* Common code for fixed and float version*/
  198. typedef struct AMRWB_bitrates {
  199. int rate;
  200. int mode;
  201. } AMRWB_bitrates;
  202. typedef struct AMRWBContext {
  203. int frameCount;
  204. void *state;
  205. int mode;
  206. Word16 allow_dtx;
  207. } AMRWBContext;
  208. static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
  209. {
  210. AMRWBContext *s = avctx->priv_data;
  211. s->frameCount = 0;
  212. s->state = D_IF_init();
  213. amr_decode_fix_avctx(avctx);
  214. if (avctx->channels > 1) {
  215. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. static int amr_wb_decode_frame(AVCodecContext *avctx,
  221. void *data, int *data_size,
  222. const uint8_t *buf, int buf_size)
  223. {
  224. AMRWBContext *s = avctx->priv_data;
  225. const uint8_t *amrData = buf;
  226. int mode;
  227. int packet_size;
  228. static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  229. if (!buf_size)
  230. /* nothing to do */
  231. return 0;
  232. mode = (amrData[0] >> 3) & 0x000F;
  233. packet_size = block_size[mode];
  234. if (packet_size > buf_size) {
  235. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  236. buf_size, packet_size + 1);
  237. return -1;
  238. }
  239. s->frameCount++;
  240. D_IF_decode(s->state, amrData, data, _good_frame);
  241. *data_size = 320 * 2;
  242. return packet_size;
  243. }
  244. static int amr_wb_decode_close(AVCodecContext *avctx)
  245. {
  246. AMRWBContext *s = avctx->priv_data;
  247. D_IF_exit(s->state);
  248. return 0;
  249. }
  250. AVCodec libopencore_amrwb_decoder = {
  251. "libopencore_amrwb",
  252. CODEC_TYPE_AUDIO,
  253. CODEC_ID_AMR_WB,
  254. sizeof(AMRWBContext),
  255. amr_wb_decode_init,
  256. NULL,
  257. amr_wb_decode_close,
  258. amr_wb_decode_frame,
  259. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"),
  260. };
  261. #endif /* CONFIG_LIBOPENCORE_AMRWB */