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.

649 lines
17KB

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