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.

326 lines
8.7KB

  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, AVPacket *avpkt)
  91. {
  92. const uint8_t *buf = avpkt->data;
  93. int buf_size = avpkt->size;
  94. AMRContext *s = avctx->priv_data;
  95. const uint8_t *amrData = buf;
  96. static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  97. enum Mode dec_mode;
  98. int packet_size;
  99. /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
  100. buf, buf_size, s->frameCount); */
  101. dec_mode = (buf[0] >> 3) & 0x000F;
  102. packet_size = block_size[dec_mode] + 1;
  103. if (packet_size > buf_size) {
  104. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  105. buf_size, packet_size);
  106. return -1;
  107. }
  108. s->frameCount++;
  109. /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
  110. packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
  111. /* call decoder */
  112. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  113. *data_size = 160 * 2;
  114. return packet_size;
  115. }
  116. AVCodec libopencore_amrnb_decoder = {
  117. "libopencore_amrnb",
  118. CODEC_TYPE_AUDIO,
  119. CODEC_ID_AMR_NB,
  120. sizeof(AMRContext),
  121. amr_nb_decode_init,
  122. NULL,
  123. amr_nb_decode_close,
  124. amr_nb_decode_frame,
  125. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  126. };
  127. static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
  128. {
  129. AMRContext *s = avctx->priv_data;
  130. s->frameCount = 0;
  131. if (avctx->sample_rate != 8000) {
  132. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  133. return -1;
  134. }
  135. if (avctx->channels != 1) {
  136. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  137. return -1;
  138. }
  139. avctx->frame_size = 160;
  140. avctx->coded_frame = avcodec_alloc_frame();
  141. s->enstate=Encoder_Interface_init(0);
  142. if (!s->enstate) {
  143. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  144. return -1;
  145. }
  146. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  147. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
  153. {
  154. AMRContext *s = avctx->priv_data;
  155. Encoder_Interface_exit(s->enstate);
  156. av_freep(&avctx->coded_frame);
  157. return 0;
  158. }
  159. static int amr_nb_encode_frame(AVCodecContext *avctx,
  160. unsigned char *frame/*out*/,
  161. int buf_size, void *data/*in*/)
  162. {
  163. AMRContext *s = avctx->priv_data;
  164. int written;
  165. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  166. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  167. return -1;
  168. }
  169. written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
  170. frame, 0);
  171. /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
  172. written, s->enc_bitrate, frame[0] ); */
  173. return written;
  174. }
  175. AVCodec libopencore_amrnb_encoder = {
  176. "libopencore_amrnb",
  177. CODEC_TYPE_AUDIO,
  178. CODEC_ID_AMR_NB,
  179. sizeof(AMRContext),
  180. amr_nb_encode_init,
  181. amr_nb_encode_frame,
  182. amr_nb_encode_close,
  183. NULL,
  184. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  185. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  186. };
  187. #endif
  188. /* -----------AMR wideband ------------*/
  189. #if CONFIG_LIBOPENCORE_AMRWB
  190. #ifdef _TYPEDEF_H
  191. //To avoid duplicate typedefs from typedef in amr-nb
  192. #define typedef_h
  193. #endif
  194. #include <opencore-amrwb/dec_if.h>
  195. #include <opencore-amrwb/if_rom.h>
  196. static const char wb_bitrate_unsupported[] =
  197. "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";
  198. /* Common code for fixed and float version*/
  199. typedef struct AMRWB_bitrates {
  200. int rate;
  201. int mode;
  202. } AMRWB_bitrates;
  203. typedef struct AMRWBContext {
  204. int frameCount;
  205. void *state;
  206. int mode;
  207. Word16 allow_dtx;
  208. } AMRWBContext;
  209. static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
  210. {
  211. AMRWBContext *s = avctx->priv_data;
  212. s->frameCount = 0;
  213. s->state = D_IF_init();
  214. amr_decode_fix_avctx(avctx);
  215. if (avctx->channels > 1) {
  216. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
  222. int *data_size, AVPacket *avpkt)
  223. {
  224. const uint8_t *buf = avpkt->data;
  225. int buf_size = avpkt->size;
  226. AMRWBContext *s = avctx->priv_data;
  227. const uint8_t *amrData = buf;
  228. int mode;
  229. int packet_size;
  230. static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  231. if (!buf_size)
  232. /* nothing to do */
  233. return 0;
  234. mode = (amrData[0] >> 3) & 0x000F;
  235. packet_size = block_size[mode];
  236. if (packet_size > buf_size) {
  237. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  238. buf_size, packet_size + 1);
  239. return -1;
  240. }
  241. s->frameCount++;
  242. D_IF_decode(s->state, amrData, data, _good_frame);
  243. *data_size = 320 * 2;
  244. return packet_size;
  245. }
  246. static int amr_wb_decode_close(AVCodecContext *avctx)
  247. {
  248. AMRWBContext *s = avctx->priv_data;
  249. D_IF_exit(s->state);
  250. return 0;
  251. }
  252. AVCodec libopencore_amrwb_decoder = {
  253. "libopencore_amrwb",
  254. CODEC_TYPE_AUDIO,
  255. CODEC_ID_AMR_WB,
  256. sizeof(AMRWBContext),
  257. amr_wb_decode_init,
  258. NULL,
  259. amr_wb_decode_close,
  260. amr_wb_decode_frame,
  261. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"),
  262. };
  263. #endif /* CONFIG_LIBOPENCORE_AMRWB */