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.

713 lines
18KB

  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 with floats. For some reason the float-encoder is significant faster
  29. * at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102).
  30. * Both float and fixed point are supported for AMR-NB, but only float for
  31. * 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. * Extract the source into \c "ffmpeg/libavcodec/amr_float".
  39. * Enable it by passing \c "--enable-amr-nb" to \c "./configure".
  40. *
  41. * \subsection Fixed-point
  42. * The fixed-point (TS26.073) can be downloaded from:
  43. * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip.
  44. * Extract the source into \c "ffmpeg/libavcodec/amr".
  45. * Enable it by passing \c "--enable-amr-nb-fixed" to \c "./configure".
  46. *
  47. * \subsection Specification
  48. * The specification for AMR-NB can be found in TS 26.071
  49. * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
  50. * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
  51. *
  52. * \section AMR-WB
  53. * \subsection Float
  54. * The reference code can be downloaded from:
  55. * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
  56. * It should be extracted to \c "ffmpeg/libavcodec/amrwb_float".
  57. * Enable it by passing \c "--enable-amr-wb" to \c "./configure".
  58. *
  59. * \subsection Fixed-point
  60. * If someone wants to use the fixed point version it can be downloaded from:
  61. * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip.
  62. *
  63. * \subsection Specification
  64. * The specification for AMR-WB can be found in TS 26.171
  65. * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other
  66. * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
  67. *
  68. */
  69. #include "avcodec.h"
  70. #ifdef CONFIG_AMR_NB_FIXED
  71. #define MMS_IO
  72. #include "amr/sp_dec.h"
  73. #include "amr/d_homing.h"
  74. #include "amr/typedef.h"
  75. #include "amr/sp_enc.h"
  76. #include "amr/sid_sync.h"
  77. #include "amr/e_homing.h"
  78. #else
  79. #include "amr_float/interf_dec.h"
  80. #include "amr_float/interf_enc.h"
  81. #endif
  82. /* Common code for fixed and float version*/
  83. typedef struct AMR_bitrates
  84. {
  85. int rate;
  86. enum Mode mode;
  87. } AMR_bitrates;
  88. /* Match desired bitrate */
  89. static int getBitrateMode(int bitrate)
  90. {
  91. /* make the correspondance between bitrate and mode */
  92. AMR_bitrates rates[]={ {4750,MR475},
  93. {5150,MR515},
  94. {5900,MR59},
  95. {6700,MR67},
  96. {7400,MR74},
  97. {7950,MR795},
  98. {10200,MR102},
  99. {12200,MR122},
  100. };
  101. int i;
  102. for(i=0;i<8;i++)
  103. {
  104. if(rates[i].rate==bitrate)
  105. {
  106. return(rates[i].mode);
  107. }
  108. }
  109. /* no bitrate matching, return an error */
  110. return -1;
  111. }
  112. static void amr_decode_fix_avctx(AVCodecContext * avctx)
  113. {
  114. const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
  115. if(avctx->sample_rate == 0)
  116. {
  117. avctx->sample_rate = 8000 * is_amr_wb;
  118. }
  119. if(avctx->channels == 0)
  120. {
  121. avctx->channels = 1;
  122. }
  123. avctx->frame_size = 160 * is_amr_wb;
  124. }
  125. #ifdef CONFIG_AMR_NB_FIXED
  126. /* fixed point version*/
  127. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  128. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  129. typedef struct AMRContext {
  130. int frameCount;
  131. Speech_Decode_FrameState *speech_decoder_state;
  132. enum RXFrameType rx_type;
  133. enum Mode mode;
  134. Word16 reset_flag;
  135. Word16 reset_flag_old;
  136. int enc_bitrate;
  137. Speech_Encode_FrameState *enstate;
  138. sid_syncState *sidstate;
  139. enum TXFrameType tx_frametype;
  140. } AMRContext;
  141. static int amr_nb_decode_init(AVCodecContext * avctx)
  142. {
  143. AMRContext *s = avctx->priv_data;
  144. s->frameCount=0;
  145. s->speech_decoder_state=NULL;
  146. s->rx_type = (enum RXFrameType)0;
  147. s->mode= (enum Mode)0;
  148. s->reset_flag=0;
  149. s->reset_flag_old=1;
  150. if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
  151. {
  152. av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
  153. return -1;
  154. }
  155. amr_decode_fix_avctx(avctx);
  156. if(avctx->channels > 1)
  157. {
  158. av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. static int amr_nb_encode_init(AVCodecContext * avctx)
  164. {
  165. AMRContext *s = avctx->priv_data;
  166. s->frameCount=0;
  167. s->speech_decoder_state=NULL;
  168. s->rx_type = (enum RXFrameType)0;
  169. s->mode= (enum Mode)0;
  170. s->reset_flag=0;
  171. s->reset_flag_old=1;
  172. if(avctx->sample_rate!=8000)
  173. {
  174. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  175. return -1;
  176. }
  177. if(avctx->channels!=1)
  178. {
  179. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  180. return -1;
  181. }
  182. avctx->frame_size=160;
  183. avctx->coded_frame= avcodec_alloc_frame();
  184. if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
  185. {
  186. av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n");
  187. return -1;
  188. }
  189. if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
  190. {
  191. av_log(avctx, AV_LOG_ERROR, "bitrate not supported\n");
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. static int amr_nb_encode_close(AVCodecContext * avctx)
  197. {
  198. AMRContext *s = avctx->priv_data;
  199. Speech_Encode_Frame_exit(&s->enstate);
  200. sid_sync_exit (&s->sidstate);
  201. av_freep(&avctx->coded_frame);
  202. return 0;
  203. }
  204. static int amr_nb_decode_close(AVCodecContext * avctx)
  205. {
  206. AMRContext *s = avctx->priv_data;
  207. Speech_Decode_Frame_exit(&s->speech_decoder_state);
  208. return 0;
  209. }
  210. static int amr_nb_decode_frame(AVCodecContext * avctx,
  211. void *data, int *data_size,
  212. uint8_t * buf, int buf_size)
  213. {
  214. AMRContext *s = avctx->priv_data;
  215. uint8_t*amrData=buf;
  216. int offset=0;
  217. UWord8 toc, q, ft;
  218. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  219. Word16 *synth;
  220. UWord8 *packed_bits;
  221. static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  222. int i;
  223. //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
  224. synth=data;
  225. toc=amrData[offset];
  226. /* read rest of the frame based on ToC byte */
  227. q = (toc >> 2) & 0x01;
  228. ft = (toc >> 3) & 0x0F;
  229. //printf("offset=%d, packet_size=%d amrData= 0x%X %X %X %X\n",offset,packed_size[ft],amrData[offset],amrData[offset+1],amrData[offset+2],amrData[offset+3]);
  230. offset++;
  231. packed_bits=amrData+offset;
  232. offset+=packed_size[ft];
  233. //Unsort and unpack bits
  234. s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
  235. //We have a new frame
  236. s->frameCount++;
  237. if (s->rx_type == RX_NO_DATA)
  238. {
  239. s->mode = s->speech_decoder_state->prev_mode;
  240. }
  241. else {
  242. s->speech_decoder_state->prev_mode = s->mode;
  243. }
  244. /* if homed: check if this frame is another homing frame */
  245. if (s->reset_flag_old == 1)
  246. {
  247. /* only check until end of first subframe */
  248. s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
  249. }
  250. /* produce encoder homing frame if homed & input=decoder homing frame */
  251. if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
  252. {
  253. for (i = 0; i < L_FRAME; i++)
  254. {
  255. synth[i] = EHF_MASK;
  256. }
  257. }
  258. else
  259. {
  260. /* decode frame */
  261. Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
  262. }
  263. //Each AMR-frame results in 160 16-bit samples
  264. *data_size+=160*2;
  265. /* if not homed: check whether current frame is a homing frame */
  266. if (s->reset_flag_old == 0)
  267. {
  268. /* check whole frame */
  269. s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
  270. }
  271. /* reset decoder if current frame is a homing frame */
  272. if (s->reset_flag != 0)
  273. {
  274. Speech_Decode_Frame_reset(s->speech_decoder_state);
  275. }
  276. s->reset_flag_old = s->reset_flag;
  277. return offset;
  278. }
  279. static int amr_nb_encode_frame(AVCodecContext *avctx,
  280. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  281. {
  282. short serial_data[250] = {0};
  283. AMRContext *s = avctx->priv_data;
  284. int written;
  285. s->reset_flag = encoder_homing_frame_test(data);
  286. Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
  287. /* add frame type and mode */
  288. sid_sync (s->sidstate, s->mode, &s->tx_frametype);
  289. written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
  290. if (s->reset_flag != 0)
  291. {
  292. Speech_Encode_Frame_reset(s->enstate);
  293. sid_sync_reset(s->sidstate);
  294. }
  295. return written;
  296. }
  297. #elif defined(CONFIG_AMR_NB) /* Float point version*/
  298. typedef struct AMRContext {
  299. int frameCount;
  300. void * decState;
  301. int *enstate;
  302. int enc_bitrate;
  303. } AMRContext;
  304. static int amr_nb_decode_init(AVCodecContext * avctx)
  305. {
  306. AMRContext *s = avctx->priv_data;
  307. s->frameCount=0;
  308. s->decState=Decoder_Interface_init();
  309. if(!s->decState)
  310. {
  311. av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
  312. return -1;
  313. }
  314. amr_decode_fix_avctx(avctx);
  315. if(avctx->channels > 1)
  316. {
  317. av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. static int amr_nb_encode_init(AVCodecContext * avctx)
  323. {
  324. AMRContext *s = avctx->priv_data;
  325. s->frameCount=0;
  326. if(avctx->sample_rate!=8000)
  327. {
  328. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  329. return -1;
  330. }
  331. if(avctx->channels!=1)
  332. {
  333. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  334. return -1;
  335. }
  336. avctx->frame_size=160;
  337. avctx->coded_frame= avcodec_alloc_frame();
  338. s->enstate=Encoder_Interface_init(0);
  339. if(!s->enstate)
  340. {
  341. av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
  342. return -1;
  343. }
  344. if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
  345. {
  346. av_log(avctx, AV_LOG_ERROR, "bitrate not supported\n");
  347. return -1;
  348. }
  349. return 0;
  350. }
  351. static int amr_nb_decode_close(AVCodecContext * avctx)
  352. {
  353. AMRContext *s = avctx->priv_data;
  354. Decoder_Interface_exit(s->decState);
  355. return 0;
  356. }
  357. static int amr_nb_encode_close(AVCodecContext * avctx)
  358. {
  359. AMRContext *s = avctx->priv_data;
  360. Encoder_Interface_exit(s->enstate);
  361. av_freep(&avctx->coded_frame);
  362. return 0;
  363. }
  364. static int amr_nb_decode_frame(AVCodecContext * avctx,
  365. void *data, int *data_size,
  366. uint8_t * buf, int buf_size)
  367. {
  368. AMRContext *s = avctx->priv_data;
  369. uint8_t*amrData=buf;
  370. static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  371. enum Mode dec_mode;
  372. int packet_size;
  373. /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */
  374. dec_mode = (buf[0] >> 3) & 0x000F;
  375. packet_size = block_size[dec_mode]+1;
  376. if(packet_size > buf_size) {
  377. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
  378. return -1;
  379. }
  380. s->frameCount++;
  381. /* 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]); */
  382. /* call decoder */
  383. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  384. *data_size=160*2;
  385. return packet_size;
  386. }
  387. static int amr_nb_encode_frame(AVCodecContext *avctx,
  388. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  389. {
  390. AMRContext *s = avctx->priv_data;
  391. int written;
  392. if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
  393. {
  394. av_log(avctx, AV_LOG_ERROR, "bitrate not supported\n");
  395. return -1;
  396. }
  397. written = Encoder_Interface_Encode(s->enstate,
  398. s->enc_bitrate,
  399. data,
  400. frame,
  401. 0);
  402. /* 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] ); */
  403. return written;
  404. }
  405. #endif
  406. #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
  407. AVCodec amr_nb_decoder =
  408. {
  409. "amr_nb",
  410. CODEC_TYPE_AUDIO,
  411. CODEC_ID_AMR_NB,
  412. sizeof(AMRContext),
  413. amr_nb_decode_init,
  414. NULL,
  415. amr_nb_decode_close,
  416. amr_nb_decode_frame,
  417. };
  418. AVCodec amr_nb_encoder =
  419. {
  420. "amr_nb",
  421. CODEC_TYPE_AUDIO,
  422. CODEC_ID_AMR_NB,
  423. sizeof(AMRContext),
  424. amr_nb_encode_init,
  425. amr_nb_encode_frame,
  426. amr_nb_encode_close,
  427. NULL,
  428. };
  429. #endif
  430. /* -----------AMR wideband ------------*/
  431. #ifdef CONFIG_AMR_WB
  432. #ifdef _TYPEDEF_H
  433. //To avoid duplicate typedefs from typdef in amr-nb
  434. #define typedef_h
  435. #endif
  436. #include "amrwb_float/enc_if.h"
  437. #include "amrwb_float/dec_if.h"
  438. /* Common code for fixed and float version*/
  439. typedef struct AMRWB_bitrates
  440. {
  441. int rate;
  442. int mode;
  443. } AMRWB_bitrates;
  444. static int getWBBitrateMode(int bitrate)
  445. {
  446. /* make the correspondance between bitrate and mode */
  447. AMRWB_bitrates rates[]={ {6600,0},
  448. {8850,1},
  449. {12650,2},
  450. {14250,3},
  451. {15850,4},
  452. {18250,5},
  453. {19850,6},
  454. {23050,7},
  455. {23850,8},
  456. };
  457. int i;
  458. for(i=0;i<9;i++)
  459. {
  460. if(rates[i].rate==bitrate)
  461. {
  462. return(rates[i].mode);
  463. }
  464. }
  465. /* no bitrate matching, return an error */
  466. return -1;
  467. }
  468. typedef struct AMRWBContext {
  469. int frameCount;
  470. void *state;
  471. int mode;
  472. Word16 allow_dtx;
  473. } AMRWBContext;
  474. static int amr_wb_encode_init(AVCodecContext * avctx)
  475. {
  476. AMRWBContext *s = avctx->priv_data;
  477. s->frameCount=0;
  478. if(avctx->sample_rate!=16000)
  479. {
  480. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  481. return -1;
  482. }
  483. if(avctx->channels!=1)
  484. {
  485. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  486. return -1;
  487. }
  488. if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
  489. {
  490. av_log(avctx, AV_LOG_ERROR, "bitrate not supported\n");
  491. return -1;
  492. }
  493. avctx->frame_size=320;
  494. avctx->coded_frame= avcodec_alloc_frame();
  495. s->state = E_IF_init();
  496. s->allow_dtx=0;
  497. return 0;
  498. }
  499. static int amr_wb_encode_close(AVCodecContext * avctx)
  500. {
  501. AMRWBContext *s = avctx->priv_data;
  502. E_IF_exit(s->state);
  503. av_freep(&avctx->coded_frame);
  504. s->frameCount++;
  505. return 0;
  506. }
  507. static int amr_wb_encode_frame(AVCodecContext *avctx,
  508. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  509. {
  510. AMRWBContext *s = avctx->priv_data;
  511. int size;
  512. if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
  513. {
  514. av_log(avctx, AV_LOG_ERROR, "bitrate not supported\n");
  515. return -1;
  516. }
  517. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  518. return size;
  519. }
  520. static int amr_wb_decode_init(AVCodecContext * avctx)
  521. {
  522. AMRWBContext *s = avctx->priv_data;
  523. s->frameCount=0;
  524. s->state = D_IF_init();
  525. amr_decode_fix_avctx(avctx);
  526. if(avctx->channels > 1)
  527. {
  528. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  529. return -1;
  530. }
  531. return 0;
  532. }
  533. extern const UWord8 block_size[];
  534. static int amr_wb_decode_frame(AVCodecContext * avctx,
  535. void *data, int *data_size,
  536. uint8_t * buf, int buf_size)
  537. {
  538. AMRWBContext *s = avctx->priv_data;
  539. uint8_t*amrData=buf;
  540. int mode;
  541. int packet_size;
  542. if(buf_size==0) {
  543. /* nothing to do */
  544. return 0;
  545. }
  546. mode = (amrData[0] >> 3) & 0x000F;
  547. packet_size = block_size[mode];
  548. if(packet_size > buf_size) {
  549. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
  550. return -1;
  551. }
  552. s->frameCount++;
  553. D_IF_decode( s->state, amrData, data, _good_frame);
  554. *data_size=320*2;
  555. return packet_size;
  556. }
  557. static int amr_wb_decode_close(AVCodecContext * avctx)
  558. {
  559. AMRWBContext *s = avctx->priv_data;
  560. D_IF_exit(s->state);
  561. return 0;
  562. }
  563. AVCodec amr_wb_decoder =
  564. {
  565. "amr_wb",
  566. CODEC_TYPE_AUDIO,
  567. CODEC_ID_AMR_WB,
  568. sizeof(AMRWBContext),
  569. amr_wb_decode_init,
  570. NULL,
  571. amr_wb_decode_close,
  572. amr_wb_decode_frame,
  573. };
  574. AVCodec amr_wb_encoder =
  575. {
  576. "amr_wb",
  577. CODEC_TYPE_AUDIO,
  578. CODEC_ID_AMR_WB,
  579. sizeof(AMRWBContext),
  580. amr_wb_encode_init,
  581. amr_wb_encode_frame,
  582. amr_wb_encode_close,
  583. NULL,
  584. };
  585. #endif //CONFIG_AMR_WB