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.

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