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.

449 lines
12KB

  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, AVPacket *avpkt)
  119. {
  120. const uint8_t *buf = avpkt->data;
  121. int buf_size = avpkt->size;
  122. AMRContext *s = avctx->priv_data;
  123. const uint8_t *amrData = buf;
  124. static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  125. enum Mode dec_mode;
  126. int packet_size;
  127. /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
  128. buf, buf_size, s->frameCount); */
  129. dec_mode = (buf[0] >> 3) & 0x000F;
  130. packet_size = block_size[dec_mode] + 1;
  131. if (packet_size > buf_size) {
  132. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  133. buf_size, packet_size);
  134. return -1;
  135. }
  136. s->frameCount++;
  137. /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
  138. packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
  139. /* call decoder */
  140. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  141. *data_size = 160 * 2;
  142. return packet_size;
  143. }
  144. AVCodec libamr_nb_decoder = {
  145. "libamr_nb",
  146. CODEC_TYPE_AUDIO,
  147. CODEC_ID_AMR_NB,
  148. sizeof(AMRContext),
  149. amr_nb_decode_init,
  150. NULL,
  151. amr_nb_decode_close,
  152. amr_nb_decode_frame,
  153. .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
  154. };
  155. static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
  156. {
  157. AMRContext *s = avctx->priv_data;
  158. s->frameCount = 0;
  159. if (avctx->sample_rate != 8000) {
  160. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  161. return -1;
  162. }
  163. if (avctx->channels != 1) {
  164. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  165. return -1;
  166. }
  167. avctx->frame_size = 160;
  168. avctx->coded_frame = avcodec_alloc_frame();
  169. s->enstate=Encoder_Interface_init(0);
  170. if (!s->enstate) {
  171. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  172. return -1;
  173. }
  174. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  175. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  176. return -1;
  177. }
  178. return 0;
  179. }
  180. static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
  181. {
  182. AMRContext *s = avctx->priv_data;
  183. Encoder_Interface_exit(s->enstate);
  184. av_freep(&avctx->coded_frame);
  185. return 0;
  186. }
  187. static int amr_nb_encode_frame(AVCodecContext *avctx,
  188. unsigned char *frame/*out*/,
  189. int buf_size, void *data/*in*/)
  190. {
  191. AMRContext *s = avctx->priv_data;
  192. int written;
  193. if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
  194. av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
  195. return -1;
  196. }
  197. written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
  198. frame, 0);
  199. /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
  200. written, s->enc_bitrate, frame[0] ); */
  201. return written;
  202. }
  203. AVCodec libamr_nb_encoder = {
  204. "libamr_nb",
  205. CODEC_TYPE_AUDIO,
  206. CODEC_ID_AMR_NB,
  207. sizeof(AMRContext),
  208. amr_nb_encode_init,
  209. amr_nb_encode_frame,
  210. amr_nb_encode_close,
  211. NULL,
  212. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  213. .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
  214. };
  215. #endif
  216. /* -----------AMR wideband ------------*/
  217. #if CONFIG_LIBAMR_WB
  218. #ifdef _TYPEDEF_H
  219. //To avoid duplicate typedefs from typedef in amr-nb
  220. #define typedef_h
  221. #endif
  222. #include <amrwb/dec_if.h>
  223. #include <amrwb/if_rom.h>
  224. static const char wb_bitrate_unsupported[] =
  225. "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";
  226. typedef struct AMRWB_bitrates {
  227. int rate;
  228. int mode;
  229. } AMRWB_bitrates;
  230. typedef struct AMRWBContext {
  231. int frameCount;
  232. void *state;
  233. int mode;
  234. Word16 allow_dtx;
  235. } AMRWBContext;
  236. #if CONFIG_LIBAMR_WB_ENCODER
  237. #include <amrwb/enc_if.h>
  238. static int getWBBitrateMode(int bitrate)
  239. {
  240. /* make the correspondance between bitrate and mode */
  241. AMRWB_bitrates rates[] = { { 6600, 0},
  242. { 8850, 1},
  243. {12650, 2},
  244. {14250, 3},
  245. {15850, 4},
  246. {18250, 5},
  247. {19850, 6},
  248. {23050, 7},
  249. {23850, 8}, };
  250. int i;
  251. for (i = 0; i < 9; i++)
  252. if (rates[i].rate == bitrate)
  253. return rates[i].mode;
  254. /* no bitrate matching, return an error */
  255. return -1;
  256. }
  257. static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
  258. {
  259. AMRWBContext *s = avctx->priv_data;
  260. s->frameCount = 0;
  261. if (avctx->sample_rate != 16000) {
  262. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  263. return -1;
  264. }
  265. if (avctx->channels != 1) {
  266. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  267. return -1;
  268. }
  269. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  270. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  271. return -1;
  272. }
  273. avctx->frame_size = 320;
  274. avctx->coded_frame = avcodec_alloc_frame();
  275. s->state = E_IF_init();
  276. s->allow_dtx = 0;
  277. return 0;
  278. }
  279. static int amr_wb_encode_close(AVCodecContext *avctx)
  280. {
  281. AMRWBContext *s = avctx->priv_data;
  282. E_IF_exit(s->state);
  283. av_freep(&avctx->coded_frame);
  284. s->frameCount++;
  285. return 0;
  286. }
  287. static int amr_wb_encode_frame(AVCodecContext *avctx,
  288. unsigned char *frame/*out*/,
  289. int buf_size, void *data/*in*/)
  290. {
  291. AMRWBContext *s = avctx->priv_data;
  292. int size;
  293. if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
  294. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  295. return -1;
  296. }
  297. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  298. return size;
  299. }
  300. AVCodec libamr_wb_encoder = {
  301. "libamr_wb",
  302. CODEC_TYPE_AUDIO,
  303. CODEC_ID_AMR_WB,
  304. sizeof(AMRWBContext),
  305. amr_wb_encode_init,
  306. amr_wb_encode_frame,
  307. amr_wb_encode_close,
  308. NULL,
  309. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  310. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  311. };
  312. #endif
  313. static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
  314. {
  315. AMRWBContext *s = avctx->priv_data;
  316. s->frameCount = 0;
  317. s->state = D_IF_init();
  318. amr_decode_fix_avctx(avctx);
  319. if (avctx->channels > 1) {
  320. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  321. return -1;
  322. }
  323. return 0;
  324. }
  325. static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
  326. int *data_size, AVPacket *avpkt)
  327. {
  328. const uint8_t *buf = avpkt->data;
  329. int buf_size = avpkt->size;
  330. AMRWBContext *s = avctx->priv_data;
  331. const uint8_t *amrData = buf;
  332. int mode;
  333. int packet_size;
  334. static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  335. if (!buf_size)
  336. /* nothing to do */
  337. return 0;
  338. mode = (amrData[0] >> 3) & 0x000F;
  339. packet_size = block_size[mode];
  340. if (packet_size > buf_size) {
  341. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
  342. buf_size, packet_size + 1);
  343. return -1;
  344. }
  345. s->frameCount++;
  346. D_IF_decode(s->state, amrData, data, _good_frame);
  347. *data_size = 320 * 2;
  348. return packet_size;
  349. }
  350. static int amr_wb_decode_close(AVCodecContext *avctx)
  351. {
  352. AMRWBContext *s = avctx->priv_data;
  353. D_IF_exit(s->state);
  354. return 0;
  355. }
  356. AVCodec libamr_wb_decoder = {
  357. "libamr_wb",
  358. CODEC_TYPE_AUDIO,
  359. CODEC_ID_AMR_WB,
  360. sizeof(AMRWBContext),
  361. amr_wb_decode_init,
  362. NULL,
  363. amr_wb_decode_close,
  364. amr_wb_decode_frame,
  365. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  366. };
  367. #endif //CONFIG_LIBAMR_WB