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