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.

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