libamr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /** @file
  22. * Adaptive Multi-Rate (AMR) Audio decoder stub.
  23. *
  24. * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand
  25. * (AMR-WB) audio encoder/decoder through external reference code from
  26. * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you
  27. * have to download the code separately.
  28. *
  29. * \section AMR-NB
  30. *
  31. * The float version (default) can be downloaded from:
  32. * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip
  33. *
  34. * \subsection Specification
  35. * The specification for AMR-NB can be found in TS 26.071
  36. * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
  37. * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
  38. *
  39. * \section AMR-WB
  40. *
  41. * The reference code can be downloaded from:
  42. * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
  43. *
  44. * \subsection Specification
  45. * The specification for AMR-WB can be found in TS 26.171
  46. * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other
  47. * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
  48. *
  49. */
  50. #include "avcodec.h"
  51. static void amr_decode_fix_avctx(AVCodecContext *avctx)
  52. {
  53. const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
  54. if (!avctx->sample_rate)
  55. avctx->sample_rate = 8000 * is_amr_wb;
  56. if (!avctx->channels)
  57. avctx->channels = 1;
  58. avctx->frame_size = 160 * is_amr_wb;
  59. avctx->sample_fmt = SAMPLE_FMT_S16;
  60. }
  61. #if CONFIG_LIBAMR_NB
  62. #include <amrnb/interf_dec.h>
  63. #include <amrnb/interf_enc.h>
  64. static const char nb_bitrate_unsupported[] =
  65. "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
  66. typedef struct AMR_bitrates {
  67. int rate;
  68. enum Mode mode;
  69. } AMR_bitrates;
  70. /* Match desired bitrate */
  71. static int getBitrateMode(int bitrate)
  72. {
  73. /* make the correspondance between bitrate and mode */
  74. AMR_bitrates rates[] = { { 4750, MR475},
  75. { 5150, MR515},
  76. { 5900, MR59},
  77. { 6700, MR67},
  78. { 7400, MR74},
  79. { 7950, MR795},
  80. {10200, MR102},
  81. {12200, MR122}, };
  82. int i;
  83. for (i = 0; i < 8; i++)
  84. if (rates[i].rate == bitrate)
  85. return rates[i].mode;
  86. /* no bitrate matching, return an error */
  87. return -1;
  88. }
  89. typedef struct AMRContext {
  90. int frameCount;
  91. void *decState;
  92. int *enstate;
  93. int enc_bitrate;
  94. } AMRContext;
  95. static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
  96. {
  97. AMRContext *s = avctx->priv_data;
  98. s->frameCount = 0;
  99. s->decState = Decoder_Interface_init();
  100. if (!s->decState) {
  101. av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
  102. return -1;
  103. }
  104. amr_decode_fix_avctx(avctx);
  105. if (avctx->channels > 1) {
  106. av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
  112. {
  113. AMRContext *s = avctx->priv_data;
  114. Decoder_Interface_exit(s->decState);
  115. return 0;
  116. }
  117. static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
  118. int *data_size,
  119. const uint8_t *buf, int buf_size)
  120. {
  121. AMRContext *s = avctx->priv_data;
  122. const uint8_t *amrData = buf;
  123. static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  124. enum Mode dec_mode;
  125. int packet_size;
  126. /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
  127. buf, buf_size, s->frameCount); */
  128. dec_mode = (buf[0] >> 3) & 0x000F;
  129. packet_size = block_size[dec_mode] + 1;
  130. if (packet_size > buf_size) {
  131. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  132. buf_size, packet_size);
  133. return -1;
  134. }
  135. s->frameCount++;
  136. /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
  137. packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
  138. /* call decoder */
  139. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  140. *data_size = 160 * 2;
  141. return packet_size;
  142. }
  143. AVCodec libamr_nb_decoder = {
  144. "libamr_nb",
  145. CODEC_TYPE_AUDIO,
  146. CODEC_ID_AMR_NB,
  147. sizeof(AMRContext),
  148. amr_nb_decode_init,
  149. NULL,
  150. amr_nb_decode_close,
  151. amr_nb_decode_frame,
  152. .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
  153. };
  154. static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
  155. {
  156. AMRContext *s = avctx->priv_data;
  157. s->frameCount = 0;
  158. if (avctx->sample_rate != 8000) {
  159. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  160. return -1;
  161. }
  162. if (avctx->channels != 1) {
  163. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  164. return -1;
  165. }
  166. avctx->frame_size = 160;
  167. avctx->coded_frame = avcodec_alloc_frame();
  168. s->enstate=Encoder_Interface_init(0);
  169. if (!s->enstate) {
  170. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  171. return -1;
  172. }
  173. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  174. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
  180. {
  181. AMRContext *s = avctx->priv_data;
  182. Encoder_Interface_exit(s->enstate);
  183. av_freep(&avctx->coded_frame);
  184. return 0;
  185. }
  186. static int amr_nb_encode_frame(AVCodecContext *avctx,
  187. unsigned char *frame/*out*/,
  188. int buf_size, void *data/*in*/)
  189. {
  190. AMRContext *s = avctx->priv_data;
  191. int written;
  192. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  193. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  194. return -1;
  195. }
  196. written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
  197. frame, 0);
  198. /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
  199. written, s->enc_bitrate, frame[0] ); */
  200. return written;
  201. }
  202. AVCodec libamr_nb_encoder = {
  203. "libamr_nb",
  204. CODEC_TYPE_AUDIO,
  205. CODEC_ID_AMR_NB,
  206. sizeof(AMRContext),
  207. amr_nb_encode_init,
  208. amr_nb_encode_frame,
  209. amr_nb_encode_close,
  210. NULL,
  211. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  212. .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
  213. };
  214. #endif
  215. /* -----------AMR wideband ------------*/
  216. #if CONFIG_LIBAMR_WB
  217. #ifdef _TYPEDEF_H
  218. //To avoid duplicate typedefs from typedef in amr-nb
  219. #define typedef_h
  220. #endif
  221. #include <amrwb/dec_if.h>
  222. #include <amrwb/if_rom.h>
  223. static const char wb_bitrate_unsupported[] =
  224. "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";
  225. typedef struct AMRWB_bitrates {
  226. int rate;
  227. int mode;
  228. } AMRWB_bitrates;
  229. typedef struct AMRWBContext {
  230. int frameCount;
  231. void *state;
  232. int mode;
  233. Word16 allow_dtx;
  234. } AMRWBContext;
  235. #if CONFIG_LIBAMR_WB_ENCODER
  236. #include <amrwb/enc_if.h>
  237. static int getWBBitrateMode(int bitrate)
  238. {
  239. /* make the correspondance between bitrate and mode */
  240. AMRWB_bitrates rates[] = { { 6600, 0},
  241. { 8850, 1},
  242. {12650, 2},
  243. {14250, 3},
  244. {15850, 4},
  245. {18250, 5},
  246. {19850, 6},
  247. {23050, 7},
  248. {23850, 8}, };
  249. int i;
  250. for (i = 0; i < 9; i++)
  251. if (rates[i].rate == bitrate)
  252. return rates[i].mode;
  253. /* no bitrate matching, return an error */
  254. return -1;
  255. }
  256. static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
  257. {
  258. AMRWBContext *s = avctx->priv_data;
  259. s->frameCount = 0;
  260. if (avctx->sample_rate != 16000) {
  261. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  262. return -1;
  263. }
  264. if (avctx->channels != 1) {
  265. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  266. return -1;
  267. }
  268. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  269. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  270. return -1;
  271. }
  272. avctx->frame_size = 320;
  273. avctx->coded_frame = avcodec_alloc_frame();
  274. s->state = E_IF_init();
  275. s->allow_dtx = 0;
  276. return 0;
  277. }
  278. static int amr_wb_encode_close(AVCodecContext *avctx)
  279. {
  280. AMRWBContext *s = avctx->priv_data;
  281. E_IF_exit(s->state);
  282. av_freep(&avctx->coded_frame);
  283. s->frameCount++;
  284. return 0;
  285. }
  286. static int amr_wb_encode_frame(AVCodecContext *avctx,
  287. unsigned char *frame/*out*/,
  288. int buf_size, void *data/*in*/)
  289. {
  290. AMRWBContext *s = avctx->priv_data;
  291. int size;
  292. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  293. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  294. return -1;
  295. }
  296. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  297. return size;
  298. }
  299. AVCodec libamr_wb_encoder = {
  300. "libamr_wb",
  301. CODEC_TYPE_AUDIO,
  302. CODEC_ID_AMR_WB,
  303. sizeof(AMRWBContext),
  304. amr_wb_encode_init,
  305. amr_wb_encode_frame,
  306. amr_wb_encode_close,
  307. NULL,
  308. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  309. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  310. };
  311. #endif
  312. static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
  313. {
  314. AMRWBContext *s = avctx->priv_data;
  315. s->frameCount = 0;
  316. s->state = D_IF_init();
  317. amr_decode_fix_avctx(avctx);
  318. if (avctx->channels > 1) {
  319. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  320. return -1;
  321. }
  322. return 0;
  323. }
  324. static int amr_wb_decode_frame(AVCodecContext *avctx,
  325. void *data, int *data_size,
  326. const uint8_t *buf, int buf_size)
  327. {
  328. AMRWBContext *s = avctx->priv_data;
  329. const uint8_t *amrData = buf;
  330. int mode;
  331. int packet_size;
  332. static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  333. if (!buf_size)
  334. /* nothing to do */
  335. return 0;
  336. mode = (amrData[0] >> 3) & 0x000F;
  337. packet_size = block_size[mode];
  338. if (packet_size > buf_size) {
  339. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  340. buf_size, packet_size + 1);
  341. return -1;
  342. }
  343. s->frameCount++;
  344. D_IF_decode(s->state, amrData, data, _good_frame);
  345. *data_size = 320 * 2;
  346. return packet_size;
  347. }
  348. static int amr_wb_decode_close(AVCodecContext *avctx)
  349. {
  350. AMRWBContext *s = avctx->priv_data;
  351. D_IF_exit(s->state);
  352. return 0;
  353. }
  354. AVCodec libamr_wb_decoder = {
  355. "libamr_wb",
  356. CODEC_TYPE_AUDIO,
  357. CODEC_ID_AMR_WB,
  358. sizeof(AMRWBContext),
  359. amr_wb_decode_init,
  360. NULL,
  361. amr_wb_decode_close,
  362. amr_wb_decode_frame,
  363. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  364. };
  365. #endif //CONFIG_LIBAMR_WB