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.

483 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. #include <amrwb/enc_if.h>
  264. static int getWBBitrateMode(int bitrate)
  265. {
  266. /* make the correspondance between bitrate and mode */
  267. AMRWB_bitrates rates[]={ {6600,0},
  268. {8850,1},
  269. {12650,2},
  270. {14250,3},
  271. {15850,4},
  272. {18250,5},
  273. {19850,6},
  274. {23050,7},
  275. {23850,8},
  276. };
  277. int i;
  278. for(i=0;i<9;i++)
  279. {
  280. if(rates[i].rate==bitrate)
  281. {
  282. return rates[i].mode;
  283. }
  284. }
  285. /* no bitrate matching, return an error */
  286. return -1;
  287. }
  288. static av_cold int amr_wb_encode_init(AVCodecContext * avctx)
  289. {
  290. AMRWBContext *s = avctx->priv_data;
  291. s->frameCount=0;
  292. if(avctx->sample_rate!=16000)
  293. {
  294. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  295. return -1;
  296. }
  297. if(avctx->channels!=1)
  298. {
  299. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  300. return -1;
  301. }
  302. if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
  303. {
  304. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  305. return -1;
  306. }
  307. avctx->frame_size=320;
  308. avctx->coded_frame= avcodec_alloc_frame();
  309. s->state = E_IF_init();
  310. s->allow_dtx=0;
  311. return 0;
  312. }
  313. static int amr_wb_encode_close(AVCodecContext * avctx)
  314. {
  315. AMRWBContext *s = avctx->priv_data;
  316. E_IF_exit(s->state);
  317. av_freep(&avctx->coded_frame);
  318. s->frameCount++;
  319. return 0;
  320. }
  321. static int amr_wb_encode_frame(AVCodecContext *avctx,
  322. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  323. {
  324. AMRWBContext *s = avctx->priv_data;
  325. int size;
  326. if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
  327. {
  328. av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
  329. return -1;
  330. }
  331. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  332. return size;
  333. }
  334. AVCodec libamr_wb_encoder =
  335. {
  336. "libamr_wb",
  337. CODEC_TYPE_AUDIO,
  338. CODEC_ID_AMR_WB,
  339. sizeof(AMRWBContext),
  340. amr_wb_encode_init,
  341. amr_wb_encode_frame,
  342. amr_wb_encode_close,
  343. NULL,
  344. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  345. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  346. };
  347. static av_cold int amr_wb_decode_init(AVCodecContext * avctx)
  348. {
  349. AMRWBContext *s = avctx->priv_data;
  350. s->frameCount=0;
  351. s->state = D_IF_init();
  352. amr_decode_fix_avctx(avctx);
  353. if(avctx->channels > 1)
  354. {
  355. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. static int amr_wb_decode_frame(AVCodecContext * avctx,
  361. void *data, int *data_size,
  362. AVPacket *avpkt)
  363. {
  364. const uint8_t *buf = avpkt->data;
  365. int buf_size = avpkt->size;
  366. AMRWBContext *s = avctx->priv_data;
  367. const uint8_t*amrData=buf;
  368. int mode;
  369. int packet_size;
  370. static const uint8_t block_size[16] = {18, 23, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  371. if(buf_size==0) {
  372. /* nothing to do */
  373. return 0;
  374. }
  375. mode = (amrData[0] >> 3) & 0x000F;
  376. packet_size = block_size[mode];
  377. if(packet_size > buf_size) {
  378. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
  379. return -1;
  380. }
  381. s->frameCount++;
  382. D_IF_decode( s->state, amrData, data, _good_frame);
  383. *data_size=320*2;
  384. return packet_size;
  385. }
  386. static int amr_wb_decode_close(AVCodecContext * avctx)
  387. {
  388. AMRWBContext *s = avctx->priv_data;
  389. D_IF_exit(s->state);
  390. return 0;
  391. }
  392. AVCodec libamr_wb_decoder =
  393. {
  394. "libamr_wb",
  395. CODEC_TYPE_AUDIO,
  396. CODEC_ID_AMR_WB,
  397. sizeof(AMRWBContext),
  398. amr_wb_decode_init,
  399. NULL,
  400. amr_wb_decode_close,
  401. amr_wb_decode_frame,
  402. .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
  403. };
  404. #endif //CONFIG_LIBAMR_WB