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.

692 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. /*
  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. if(buf_size==0) {
  354. /* nothing to do */
  355. return 0;
  356. }
  357. dec_mode = (buf[0] >> 3) & 0x000F;
  358. packet_size = block_size[dec_mode]+1;
  359. if(packet_size > buf_size) {
  360. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
  361. return -1;
  362. }
  363. s->frameCount++;
  364. /* 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]); */
  365. /* call decoder */
  366. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  367. *data_size=160*2;
  368. return packet_size;
  369. }
  370. static int amr_nb_encode_frame(AVCodecContext *avctx,
  371. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  372. {
  373. AMRContext *s = (AMRContext*)avctx->priv_data;
  374. int written;
  375. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  376. written = Encoder_Interface_Encode(s->enstate,
  377. s->enc_bitrate,
  378. data,
  379. frame,
  380. 0);
  381. /* 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] ); */
  382. return written;
  383. }
  384. #endif
  385. #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
  386. AVCodec amr_nb_decoder =
  387. {
  388. "amr_nb",
  389. CODEC_TYPE_AUDIO,
  390. CODEC_ID_AMR_NB,
  391. sizeof(AMRContext),
  392. amr_nb_decode_init,
  393. NULL,
  394. amr_nb_decode_close,
  395. amr_nb_decode_frame,
  396. };
  397. AVCodec amr_nb_encoder =
  398. {
  399. "amr_nb",
  400. CODEC_TYPE_AUDIO,
  401. CODEC_ID_AMR_NB,
  402. sizeof(AMRContext),
  403. amr_nb_encode_init,
  404. amr_nb_encode_frame,
  405. amr_nb_encode_close,
  406. NULL,
  407. };
  408. #endif
  409. /* -----------AMR wideband ------------*/
  410. #ifdef CONFIG_AMR_WB
  411. #ifdef _TYPEDEF_H
  412. //To avoid duplicate typedefs from typdef in amr-nb
  413. #define typedef_h
  414. #endif
  415. #include "amrwb_float/enc_if.h"
  416. #include "amrwb_float/dec_if.h"
  417. /* Common code for fixed and float version*/
  418. typedef struct AMRWB_bitrates
  419. {
  420. int startrate;
  421. int stoprate;
  422. int mode;
  423. } AMRWB_bitrates;
  424. static int getWBBitrateMode(int bitrate)
  425. {
  426. /* Adjusted so that all bitrates can be used from commandline where
  427. only a multiple of 1000 can be specified*/
  428. AMRWB_bitrates rates[]={ {0,7999,0}, //6.6kHz
  429. {8000,9999,1},//8.85
  430. {10000,13000,2},//12.65
  431. {13001,14999,3},//14.25
  432. {15000,17000,4},//15.85
  433. {17001,18000,5},//18.25
  434. {18001,22000,6},//19.85
  435. {22001,23000,7},//23.05
  436. {23001,24000,8},//23.85
  437. };
  438. int i;
  439. for(i=0;i<9;i++)
  440. {
  441. if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
  442. {
  443. return(rates[i].mode);
  444. }
  445. }
  446. /*Return highest possible*/
  447. return(8);
  448. }
  449. typedef struct AMRWBContext {
  450. int frameCount;
  451. void *state;
  452. int mode;
  453. Word16 allow_dtx;
  454. } AMRWBContext;
  455. static int amr_wb_encode_init(AVCodecContext * avctx)
  456. {
  457. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  458. s->frameCount=0;
  459. if(avctx->sample_rate!=16000)
  460. {
  461. av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
  462. return -1;
  463. }
  464. if(avctx->channels!=1)
  465. {
  466. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  467. return -1;
  468. }
  469. avctx->frame_size=320;
  470. avctx->coded_frame= avcodec_alloc_frame();
  471. s->state = E_IF_init();
  472. s->mode=getWBBitrateMode(avctx->bit_rate);
  473. s->allow_dtx=0;
  474. return 0;
  475. }
  476. static int amr_wb_encode_close(AVCodecContext * avctx)
  477. {
  478. AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
  479. E_IF_exit(s->state);
  480. av_freep(&avctx->coded_frame);
  481. s->frameCount++;
  482. return 0;
  483. }
  484. static int amr_wb_encode_frame(AVCodecContext *avctx,
  485. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  486. {
  487. AMRWBContext *s;
  488. int size;
  489. s = (AMRWBContext*) avctx->priv_data;
  490. s->mode=getWBBitrateMode(avctx->bit_rate);
  491. size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  492. return size;
  493. }
  494. static int amr_wb_decode_init(AVCodecContext * avctx)
  495. {
  496. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  497. s->frameCount=0;
  498. s->state = D_IF_init();
  499. amr_decode_fix_avctx(avctx);
  500. if(avctx->channels > 1)
  501. {
  502. av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
  503. return -1;
  504. }
  505. return 0;
  506. }
  507. extern const UWord8 block_size[];
  508. static int amr_wb_decode_frame(AVCodecContext * avctx,
  509. void *data, int *data_size,
  510. uint8_t * buf, int buf_size)
  511. {
  512. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  513. uint8_t*amrData=buf;
  514. int mode;
  515. int packet_size;
  516. if(buf_size==0) {
  517. /* nothing to do */
  518. return 0;
  519. }
  520. mode = (amrData[0] >> 3) & 0x000F;
  521. packet_size = block_size[mode];
  522. if(packet_size > buf_size) {
  523. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
  524. return -1;
  525. }
  526. s->frameCount++;
  527. D_IF_decode( s->state, amrData, data, _good_frame);
  528. *data_size=320*2;
  529. return packet_size;
  530. }
  531. static int amr_wb_decode_close(AVCodecContext * avctx)
  532. {
  533. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  534. D_IF_exit(s->state);
  535. return 0;
  536. }
  537. AVCodec amr_wb_decoder =
  538. {
  539. "amr_wb",
  540. CODEC_TYPE_AUDIO,
  541. CODEC_ID_AMR_WB,
  542. sizeof(AMRWBContext),
  543. amr_wb_decode_init,
  544. NULL,
  545. amr_wb_decode_close,
  546. amr_wb_decode_frame,
  547. };
  548. AVCodec amr_wb_encoder =
  549. {
  550. "amr_wb",
  551. CODEC_TYPE_AUDIO,
  552. CODEC_ID_AMR_WB,
  553. sizeof(AMRWBContext),
  554. amr_wb_encode_init,
  555. amr_wb_encode_frame,
  556. amr_wb_encode_close,
  557. NULL,
  558. };
  559. #endif //CONFIG_AMR_WB