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.

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