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.

687 lines
17KB

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