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.

301 lines
8.1KB

  1. /*
  2. * AMR Audio decoder stub
  3. * Copyright (c) 2003 the ffmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 = AV_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 get_bitrate_mode(int bitrate)
  44. {
  45. /* make the correspondance between bitrate and mode */
  46. static const AMR_bitrates rates[] = {
  47. { 4750, MR475 }, { 5150, MR515 }, { 5900, MR59 }, { 6700, MR67 },
  48. { 7400, MR74 }, { 7950, MR795 }, { 10200, MR102 }, { 12200, MR122 }
  49. };
  50. int i;
  51. for (i = 0; i < 8; i++)
  52. if (rates[i].rate == bitrate)
  53. return rates[i].mode;
  54. /* no bitrate matching, return an error */
  55. return -1;
  56. }
  57. typedef struct AMRContext {
  58. int frame_count;
  59. void *dec_state;
  60. void *enc_state;
  61. int enc_bitrate;
  62. } AMRContext;
  63. static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
  64. {
  65. AMRContext *s = avctx->priv_data;
  66. s->frame_count = 0;
  67. s->dec_state = Decoder_Interface_init();
  68. if (!s->dec_state) {
  69. av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\n");
  70. return -1;
  71. }
  72. amr_decode_fix_avctx(avctx);
  73. if (avctx->channels > 1) {
  74. av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
  75. return AVERROR(ENOSYS);
  76. }
  77. return 0;
  78. }
  79. static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
  80. {
  81. AMRContext *s = avctx->priv_data;
  82. Decoder_Interface_exit(s->dec_state);
  83. return 0;
  84. }
  85. static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
  86. int *data_size, AVPacket *avpkt)
  87. {
  88. const uint8_t *buf = avpkt->data;
  89. int buf_size = avpkt->size;
  90. AMRContext *s = avctx->priv_data;
  91. static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  92. enum Mode dec_mode;
  93. int packet_size;
  94. av_dlog(avctx, "amr_decode_frame buf=%p buf_size=%d frame_count=%d!!\n",
  95. buf, buf_size, s->frame_count);
  96. dec_mode = (buf[0] >> 3) & 0x000F;
  97. packet_size = block_size[dec_mode] + 1;
  98. if (packet_size > buf_size) {
  99. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  100. buf_size, packet_size);
  101. return AVERROR_INVALIDDATA;
  102. }
  103. s->frame_count++;
  104. av_dlog(avctx, "packet_size=%d buf= 0x%X %X %X %X\n",
  105. packet_size, buf[0], buf[1], buf[2], buf[3]);
  106. /* call decoder */
  107. Decoder_Interface_Decode(s->dec_state, buf, data, 0);
  108. *data_size = 160 * 2;
  109. return packet_size;
  110. }
  111. AVCodec ff_libopencore_amrnb_decoder = {
  112. "libopencore_amrnb",
  113. AVMEDIA_TYPE_AUDIO,
  114. CODEC_ID_AMR_NB,
  115. sizeof(AMRContext),
  116. amr_nb_decode_init,
  117. NULL,
  118. amr_nb_decode_close,
  119. amr_nb_decode_frame,
  120. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  121. };
  122. static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
  123. {
  124. AMRContext *s = avctx->priv_data;
  125. s->frame_count = 0;
  126. if (avctx->sample_rate != 8000) {
  127. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  128. return AVERROR(ENOSYS);
  129. }
  130. if (avctx->channels != 1) {
  131. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  132. return AVERROR(ENOSYS);
  133. }
  134. avctx->frame_size = 160;
  135. avctx->coded_frame = avcodec_alloc_frame();
  136. s->enc_state = Encoder_Interface_init(0);
  137. if (!s->enc_state) {
  138. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  139. return -1;
  140. }
  141. if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) {
  142. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  143. return AVERROR(ENOSYS);
  144. }
  145. return 0;
  146. }
  147. static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
  148. {
  149. AMRContext *s = avctx->priv_data;
  150. Encoder_Interface_exit(s->enc_state);
  151. av_freep(&avctx->coded_frame);
  152. return 0;
  153. }
  154. static int amr_nb_encode_frame(AVCodecContext *avctx,
  155. unsigned char *frame/*out*/,
  156. int buf_size, void *data/*in*/)
  157. {
  158. AMRContext *s = avctx->priv_data;
  159. int written;
  160. if ((s->enc_bitrate = get_bitrate_mode(avctx->bit_rate)) < 0) {
  161. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  162. return AVERROR(ENOSYS);
  163. }
  164. written = Encoder_Interface_Encode(s->enc_state, s->enc_bitrate, data,
  165. frame, 0);
  166. av_dlog(avctx, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
  167. written, s->enc_bitrate, frame[0]);
  168. return written;
  169. }
  170. AVCodec ff_libopencore_amrnb_encoder = {
  171. "libopencore_amrnb",
  172. AVMEDIA_TYPE_AUDIO,
  173. CODEC_ID_AMR_NB,
  174. sizeof(AMRContext),
  175. amr_nb_encode_init,
  176. amr_nb_encode_frame,
  177. amr_nb_encode_close,
  178. NULL,
  179. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  180. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
  181. };
  182. #endif
  183. /* -----------AMR wideband ------------*/
  184. #if CONFIG_LIBOPENCORE_AMRWB
  185. #include <opencore-amrwb/dec_if.h>
  186. #include <opencore-amrwb/if_rom.h>
  187. typedef struct AMRWBContext {
  188. void *state;
  189. } AMRWBContext;
  190. static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
  191. {
  192. AMRWBContext *s = avctx->priv_data;
  193. s->state = D_IF_init();
  194. amr_decode_fix_avctx(avctx);
  195. if (avctx->channels > 1) {
  196. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  197. return AVERROR(ENOSYS);
  198. }
  199. return 0;
  200. }
  201. static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
  202. int *data_size, AVPacket *avpkt)
  203. {
  204. const uint8_t *buf = avpkt->data;
  205. int buf_size = avpkt->size;
  206. AMRWBContext *s = avctx->priv_data;
  207. int mode;
  208. int packet_size;
  209. static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  210. if (!buf_size)
  211. /* nothing to do */
  212. return 0;
  213. mode = (buf[0] >> 3) & 0x000F;
  214. packet_size = block_size[mode];
  215. if (packet_size > buf_size) {
  216. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  217. buf_size, packet_size + 1);
  218. return AVERROR_INVALIDDATA;
  219. }
  220. D_IF_decode(s->state, buf, data, _good_frame);
  221. *data_size = 320 * 2;
  222. return packet_size;
  223. }
  224. static int amr_wb_decode_close(AVCodecContext *avctx)
  225. {
  226. AMRWBContext *s = avctx->priv_data;
  227. D_IF_exit(s->state);
  228. return 0;
  229. }
  230. AVCodec ff_libopencore_amrwb_decoder = {
  231. "libopencore_amrwb",
  232. AVMEDIA_TYPE_AUDIO,
  233. CODEC_ID_AMR_WB,
  234. sizeof(AMRWBContext),
  235. amr_wb_decode_init,
  236. NULL,
  237. amr_wb_decode_close,
  238. amr_wb_decode_frame,
  239. .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"),
  240. };
  241. #endif /* CONFIG_LIBOPENCORE_AMRWB */