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.

667 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. #ifdef CONFIG_AMR_NB_FIXED
  96. /* fixed point version*/
  97. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  98. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  99. typedef struct AMRContext {
  100. int frameCount;
  101. Speech_Decode_FrameState *speech_decoder_state;
  102. enum RXFrameType rx_type;
  103. enum Mode mode;
  104. Word16 reset_flag;
  105. Word16 reset_flag_old;
  106. enum Mode enc_bitrate;
  107. Speech_Encode_FrameState *enstate;
  108. sid_syncState *sidstate;
  109. enum TXFrameType tx_frametype;
  110. } AMRContext;
  111. static int amr_nb_decode_init(AVCodecContext * avctx)
  112. {
  113. AMRContext *s = avctx->priv_data;
  114. s->frameCount=0;
  115. s->speech_decoder_state=NULL;
  116. s->rx_type = (enum RXFrameType)0;
  117. s->mode= (enum Mode)0;
  118. s->reset_flag=0;
  119. s->reset_flag_old=1;
  120. if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
  121. {
  122. av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. static int amr_nb_encode_init(AVCodecContext * avctx)
  128. {
  129. AMRContext *s = avctx->priv_data;
  130. s->frameCount=0;
  131. s->speech_decoder_state=NULL;
  132. s->rx_type = (enum RXFrameType)0;
  133. s->mode= (enum Mode)0;
  134. s->reset_flag=0;
  135. s->reset_flag_old=1;
  136. if(avctx->sample_rate!=8000)
  137. {
  138. if(avctx->debug)
  139. {
  140. av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n");
  141. }
  142. return -1;
  143. }
  144. if(avctx->channels!=1)
  145. {
  146. if(avctx->debug)
  147. {
  148. av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
  149. }
  150. return -1;
  151. }
  152. avctx->frame_size=160;
  153. avctx->coded_frame= avcodec_alloc_frame();
  154. if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
  155. {
  156. if(avctx->debug)
  157. {
  158. av_log(avctx, AV_LOG_DEBUG, "Speech_Encode_Frame_init error\n");
  159. }
  160. return -1;
  161. }
  162. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  163. return 0;
  164. }
  165. static int amr_nb_encode_close(AVCodecContext * avctx)
  166. {
  167. AMRContext *s = avctx->priv_data;
  168. Speech_Encode_Frame_exit(&s->enstate);
  169. sid_sync_exit (&s->sidstate);
  170. av_freep(&avctx->coded_frame);
  171. return 0;
  172. }
  173. static int amr_nb_decode_close(AVCodecContext * avctx)
  174. {
  175. AMRContext *s = avctx->priv_data;
  176. Speech_Decode_Frame_exit(&s->speech_decoder_state);
  177. return 0;
  178. }
  179. static int amr_nb_decode_frame(AVCodecContext * avctx,
  180. void *data, int *data_size,
  181. uint8_t * buf, int buf_size)
  182. {
  183. AMRContext *s = avctx->priv_data;
  184. uint8_t*amrData=buf;
  185. int offset=0;
  186. UWord8 toc, q, ft;
  187. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  188. Word16 *synth;
  189. UWord8 *packed_bits;
  190. static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  191. int i;
  192. //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
  193. synth=data;
  194. // while(offset<buf_size)
  195. {
  196. toc=amrData[offset];
  197. /* read rest of the frame based on ToC byte */
  198. q = (toc >> 2) & 0x01;
  199. ft = (toc >> 3) & 0x0F;
  200. //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]);
  201. offset++;
  202. packed_bits=amrData+offset;
  203. offset+=packed_size[ft];
  204. //Unsort and unpack bits
  205. s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
  206. //We have a new frame
  207. s->frameCount++;
  208. if (s->rx_type == RX_NO_DATA)
  209. {
  210. s->mode = s->speech_decoder_state->prev_mode;
  211. }
  212. else {
  213. s->speech_decoder_state->prev_mode = s->mode;
  214. }
  215. /* if homed: check if this frame is another homing frame */
  216. if (s->reset_flag_old == 1)
  217. {
  218. /* only check until end of first subframe */
  219. s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
  220. }
  221. /* produce encoder homing frame if homed & input=decoder homing frame */
  222. if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
  223. {
  224. for (i = 0; i < L_FRAME; i++)
  225. {
  226. synth[i] = EHF_MASK;
  227. }
  228. }
  229. else
  230. {
  231. /* decode frame */
  232. Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
  233. }
  234. //Each AMR-frame results in 160 16-bit samples
  235. *data_size+=160*2;
  236. synth+=160;
  237. /* if not homed: check whether current frame is a homing frame */
  238. if (s->reset_flag_old == 0)
  239. {
  240. /* check whole frame */
  241. s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
  242. }
  243. /* reset decoder if current frame is a homing frame */
  244. if (s->reset_flag != 0)
  245. {
  246. Speech_Decode_Frame_reset(s->speech_decoder_state);
  247. }
  248. s->reset_flag_old = s->reset_flag;
  249. }
  250. return offset;
  251. }
  252. static int amr_nb_encode_frame(AVCodecContext *avctx,
  253. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  254. {
  255. short serial_data[250] = {0};
  256. AMRContext *s = avctx->priv_data;
  257. int written;
  258. s->reset_flag = encoder_homing_frame_test(data);
  259. Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
  260. /* add frame type and mode */
  261. sid_sync (s->sidstate, s->mode, &s->tx_frametype);
  262. written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
  263. if (s->reset_flag != 0)
  264. {
  265. Speech_Encode_Frame_reset(s->enstate);
  266. sid_sync_reset(s->sidstate);
  267. }
  268. return written;
  269. }
  270. #elif defined(CONFIG_AMR_NB) /* Float point version*/
  271. typedef struct AMRContext {
  272. int frameCount;
  273. void * decState;
  274. int *enstate;
  275. enum Mode enc_bitrate;
  276. } AMRContext;
  277. static int amr_nb_decode_init(AVCodecContext * avctx)
  278. {
  279. AMRContext *s = avctx->priv_data;
  280. s->frameCount=0;
  281. s->decState=Decoder_Interface_init();
  282. if(!s->decState)
  283. {
  284. av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
  285. return -1;
  286. }
  287. return 0;
  288. }
  289. static int amr_nb_encode_init(AVCodecContext * avctx)
  290. {
  291. AMRContext *s = avctx->priv_data;
  292. s->frameCount=0;
  293. if(avctx->sample_rate!=8000)
  294. {
  295. if(avctx->debug)
  296. {
  297. av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n");
  298. }
  299. return -1;
  300. }
  301. if(avctx->channels!=1)
  302. {
  303. if(avctx->debug)
  304. {
  305. av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
  306. }
  307. return -1;
  308. }
  309. avctx->frame_size=160;
  310. avctx->coded_frame= avcodec_alloc_frame();
  311. s->enstate=Encoder_Interface_init(0);
  312. if(!s->enstate)
  313. {
  314. if(avctx->debug)
  315. {
  316. av_log(avctx, AV_LOG_DEBUG, "Encoder_Interface_init error\n");
  317. }
  318. return -1;
  319. }
  320. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  321. return 0;
  322. }
  323. static int amr_nb_decode_close(AVCodecContext * avctx)
  324. {
  325. AMRContext *s = avctx->priv_data;
  326. Decoder_Interface_exit(s->decState);
  327. return 0;
  328. }
  329. static int amr_nb_encode_close(AVCodecContext * avctx)
  330. {
  331. AMRContext *s = avctx->priv_data;
  332. Encoder_Interface_exit(s->enstate);
  333. av_freep(&avctx->coded_frame);
  334. return 0;
  335. }
  336. static int amr_nb_decode_frame(AVCodecContext * avctx,
  337. void *data, int *data_size,
  338. uint8_t * buf, int buf_size)
  339. {
  340. AMRContext *s = (AMRContext*)avctx->priv_data;
  341. uint8_t*amrData=buf;
  342. static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  343. enum Mode dec_mode;
  344. int packet_size;
  345. /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */
  346. if(buf_size==0) {
  347. /* nothing to do */
  348. return 0;
  349. }
  350. dec_mode = (buf[0] >> 3) & 0x000F;
  351. packet_size = block_size[dec_mode]+1;
  352. if(packet_size > buf_size) {
  353. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
  354. return -1;
  355. }
  356. s->frameCount++;
  357. /* 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]); */
  358. /* call decoder */
  359. Decoder_Interface_Decode(s->decState, amrData, data, 0);
  360. *data_size=160*2;
  361. return packet_size;
  362. }
  363. static int amr_nb_encode_frame(AVCodecContext *avctx,
  364. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  365. {
  366. AMRContext *s = (AMRContext*)avctx->priv_data;
  367. int written;
  368. written = Encoder_Interface_Encode(s->enstate,
  369. s->enc_bitrate,
  370. data,
  371. frame,
  372. 0);
  373. /* 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] ); */
  374. return written;
  375. }
  376. #endif
  377. #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
  378. AVCodec amr_nb_decoder =
  379. {
  380. "amr_nb",
  381. CODEC_TYPE_AUDIO,
  382. CODEC_ID_AMR_NB,
  383. sizeof(AMRContext),
  384. amr_nb_decode_init,
  385. NULL,
  386. amr_nb_decode_close,
  387. amr_nb_decode_frame,
  388. };
  389. AVCodec amr_nb_encoder =
  390. {
  391. "amr_nb",
  392. CODEC_TYPE_AUDIO,
  393. CODEC_ID_AMR_NB,
  394. sizeof(AMRContext),
  395. amr_nb_encode_init,
  396. amr_nb_encode_frame,
  397. amr_nb_encode_close,
  398. NULL,
  399. };
  400. #endif
  401. /* -----------AMR wideband ------------*/
  402. #ifdef CONFIG_AMR_WB
  403. #ifdef _TYPEDEF_H
  404. //To avoid duplicate typedefs from typdef in amr-nb
  405. #define typedef_h
  406. #endif
  407. #include "amrwb_float/enc_if.h"
  408. #include "amrwb_float/dec_if.h"
  409. /* Common code for fixed and float version*/
  410. typedef struct AMRWB_bitrates
  411. {
  412. int startrate;
  413. int stoprate;
  414. int mode;
  415. } AMRWB_bitrates;
  416. static int getWBBitrateMode(int bitrate)
  417. {
  418. /* Adjusted so that all bitrates can be used from commandline where
  419. only a multiple of 1000 can be specified*/
  420. AMRWB_bitrates rates[]={ {0,7999,0}, //6.6kHz
  421. {8000,9999,1},//8.85
  422. {10000,13000,2},//12.65
  423. {13001,14999,3},//14.25
  424. {15000,17000,4},//15.85
  425. {17001,18000,5},//18.25
  426. {18001,22000,6},//19.85
  427. {22001,23000,7},//23.05
  428. {23001,24000,8},//23.85
  429. };
  430. int i;
  431. for(i=0;i<9;i++)
  432. {
  433. if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
  434. {
  435. return(rates[i].mode);
  436. }
  437. }
  438. /*Return highest possible*/
  439. return(8);
  440. }
  441. typedef struct AMRWBContext {
  442. int frameCount;
  443. void *state;
  444. int mode;
  445. Word16 allow_dtx;
  446. } AMRWBContext;
  447. static int amr_wb_encode_init(AVCodecContext * avctx)
  448. {
  449. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  450. s->frameCount=0;
  451. if(avctx->sample_rate!=16000)
  452. {
  453. if(avctx->debug)
  454. {
  455. av_log(avctx, AV_LOG_DEBUG, "Only 16000Hz sample rate supported\n");
  456. }
  457. return -1;
  458. }
  459. if(avctx->channels!=1)
  460. {
  461. if(avctx->debug)
  462. {
  463. av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
  464. }
  465. return -1;
  466. }
  467. avctx->frame_size=320;
  468. avctx->coded_frame= avcodec_alloc_frame();
  469. s->state = E_IF_init();
  470. s->mode=getWBBitrateMode(avctx->bit_rate);
  471. s->allow_dtx=0;
  472. return 0;
  473. }
  474. static int amr_wb_encode_close(AVCodecContext * avctx)
  475. {
  476. AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
  477. E_IF_exit(s->state);
  478. av_freep(&avctx->coded_frame);
  479. s->frameCount++;
  480. return 0;
  481. }
  482. static int amr_wb_encode_frame(AVCodecContext *avctx,
  483. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  484. {
  485. AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
  486. int size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  487. return size;
  488. }
  489. static int amr_wb_decode_init(AVCodecContext * avctx)
  490. {
  491. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  492. s->frameCount=0;
  493. s->state = D_IF_init();
  494. return 0;
  495. }
  496. extern const UWord8 block_size[];
  497. static int amr_wb_decode_frame(AVCodecContext * avctx,
  498. void *data, int *data_size,
  499. uint8_t * buf, int buf_size)
  500. {
  501. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  502. uint8_t*amrData=buf;
  503. int mode;
  504. int packet_size;
  505. if(buf_size==0) {
  506. /* nothing to do */
  507. return 0;
  508. }
  509. mode = (amrData[0] >> 3) & 0x000F;
  510. packet_size = block_size[mode];
  511. if(packet_size > buf_size) {
  512. av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
  513. return -1;
  514. }
  515. s->frameCount++;
  516. D_IF_decode( s->state, amrData, data, _good_frame);
  517. *data_size=320*2;
  518. return packet_size;
  519. }
  520. static int amr_wb_decode_close(AVCodecContext * avctx)
  521. {
  522. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  523. D_IF_exit(s->state);
  524. return 0;
  525. }
  526. AVCodec amr_wb_decoder =
  527. {
  528. "amr_wb",
  529. CODEC_TYPE_AUDIO,
  530. CODEC_ID_AMR_WB,
  531. sizeof(AMRWBContext),
  532. amr_wb_decode_init,
  533. NULL,
  534. amr_wb_decode_close,
  535. amr_wb_decode_frame,
  536. };
  537. AVCodec amr_wb_encoder =
  538. {
  539. "amr_wb",
  540. CODEC_TYPE_AUDIO,
  541. CODEC_ID_AMR_WB,
  542. sizeof(AMRWBContext),
  543. amr_wb_encode_init,
  544. amr_wb_encode_frame,
  545. amr_wb_encode_close,
  546. NULL,
  547. };
  548. #endif //CONFIG_AMR_WB