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.

653 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 "../config.h"
  49. #include "avcodec.h"
  50. #ifdef AMR_NB_FIXED
  51. #define MMS_IO
  52. #include "amr/sp_dec.h"
  53. #include "amr/d_homing.h"
  54. #include "amr/typedef.h"
  55. #include "amr/sp_enc.h"
  56. #include "amr/sid_sync.h"
  57. #include "amr/e_homing.h"
  58. #else
  59. #include "amr_float/interf_dec.h"
  60. #include "amr_float/interf_enc.h"
  61. #endif
  62. /* Common code for fixed and float version*/
  63. typedef struct AMR_bitrates
  64. {
  65. int startrate;
  66. int stoprate;
  67. enum Mode mode;
  68. } AMR_bitrates;
  69. /* Match desired bitrate with closest one*/
  70. static enum Mode getBitrateMode(int bitrate)
  71. {
  72. /* Adjusted so that all bitrates can be used from commandline where
  73. only a multiple of 1000 can be specified*/
  74. AMR_bitrates rates[]={ {0,4999,MR475}, //4
  75. {5000,5899,MR515},//5
  76. {5900,6699,MR59},//6
  77. {6700,7000,MR67},//7
  78. {7001,7949,MR74},//8
  79. {7950,9999,MR795},//9
  80. {10000,11999,MR102},//10
  81. {12000,64000,MR122},//12
  82. };
  83. int i;
  84. for(i=0;i<8;i++)
  85. {
  86. if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
  87. {
  88. return(rates[i].mode);
  89. }
  90. }
  91. /*Return highest possible*/
  92. return(MR122);
  93. }
  94. #ifdef AMR_NB_FIXED
  95. /* fixed point version*/
  96. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  97. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  98. typedef struct AMRContext {
  99. int frameCount;
  100. Speech_Decode_FrameState *speech_decoder_state;
  101. enum RXFrameType rx_type;
  102. enum Mode mode;
  103. Word16 reset_flag;
  104. Word16 reset_flag_old;
  105. enum Mode enc_bitrate;
  106. Speech_Encode_FrameState *enstate;
  107. sid_syncState *sidstate;
  108. enum TXFrameType tx_frametype;
  109. } AMRContext;
  110. static int amr_nb_decode_init(AVCodecContext * avctx)
  111. {
  112. AMRContext *s = avctx->priv_data;
  113. s->frameCount=0;
  114. s->speech_decoder_state=NULL;
  115. s->rx_type = (enum RXFrameType)0;
  116. s->mode= (enum Mode)0;
  117. s->reset_flag=0;
  118. s->reset_flag_old=1;
  119. if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
  120. {
  121. printf("Speech_Decode_Frame_init error\n");
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. static int amr_nb_encode_init(AVCodecContext * avctx)
  127. {
  128. AMRContext *s = avctx->priv_data;
  129. s->frameCount=0;
  130. s->speech_decoder_state=NULL;
  131. s->rx_type = (enum RXFrameType)0;
  132. s->mode= (enum Mode)0;
  133. s->reset_flag=0;
  134. s->reset_flag_old=1;
  135. if(avctx->sample_rate!=8000)
  136. {
  137. if(avctx->debug)
  138. {
  139. fprintf(stderr, "Only 8000Hz sample rate supported\n");
  140. }
  141. return -1;
  142. }
  143. if(avctx->channels!=1)
  144. {
  145. if(avctx->debug)
  146. {
  147. fprintf(stderr, "Only mono supported\n");
  148. }
  149. return -1;
  150. }
  151. avctx->frame_size=160;
  152. avctx->coded_frame= avcodec_alloc_frame();
  153. if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
  154. {
  155. if(avctx->debug)
  156. {
  157. fprintf(stderr, "Speech_Encode_Frame_init error\n");
  158. }
  159. return -1;
  160. }
  161. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  162. return 0;
  163. }
  164. static int amr_nb_encode_close(AVCodecContext * avctx)
  165. {
  166. AMRContext *s = avctx->priv_data;
  167. Speech_Encode_Frame_exit(&s->enstate);
  168. sid_sync_exit (&s->sidstate);
  169. av_freep(&avctx->coded_frame);
  170. return 0;
  171. }
  172. static int amr_nb_decode_close(AVCodecContext * avctx)
  173. {
  174. AMRContext *s = avctx->priv_data;
  175. Speech_Decode_Frame_exit(&s->speech_decoder_state);
  176. return 0;
  177. }
  178. static int amr_nb_decode_frame(AVCodecContext * avctx,
  179. void *data, int *data_size,
  180. uint8_t * buf, int buf_size)
  181. {
  182. AMRContext *s = avctx->priv_data;
  183. uint8_t*amrData=buf;
  184. int offset=0;
  185. UWord8 toc, q, ft;
  186. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  187. Word16 *synth;
  188. UWord8 *packed_bits;
  189. *data_size=0;
  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. #else /* 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. printf("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. fprintf(stderr, "Only 8000Hz sample rate supported\n");
  298. }
  299. return -1;
  300. }
  301. if(avctx->channels!=1)
  302. {
  303. if(avctx->debug)
  304. {
  305. fprintf(stderr, "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. fprintf(stderr, "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. int offset=0;
  343. static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  344. enum Mode dec_mode;
  345. int packet_size;
  346. *data_size=0;
  347. //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
  348. while(offset<buf_size)
  349. {
  350. dec_mode = (amrData[offset] >> 3) & 0x000F;
  351. packet_size = block_size[dec_mode];
  352. s->frameCount++;
  353. //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]);
  354. /* call decoder */
  355. Decoder_Interface_Decode(s->decState, &amrData[offset], data+*data_size, 0);
  356. *data_size+=160*2;
  357. offset+=packet_size+1;
  358. }
  359. return buf_size;
  360. }
  361. static int amr_nb_encode_frame(AVCodecContext *avctx,
  362. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  363. {
  364. AMRContext *s = (AMRContext*)avctx->priv_data;
  365. int written;
  366. written = Encoder_Interface_Encode(s->enstate,
  367. s->enc_bitrate,
  368. data,
  369. frame,
  370. 0);
  371. return written;
  372. }
  373. #endif
  374. AVCodec amr_nb_decoder =
  375. {
  376. "amr_nb",
  377. CODEC_TYPE_AUDIO,
  378. CODEC_ID_AMR_NB,
  379. sizeof(AMRContext),
  380. amr_nb_decode_init,
  381. NULL,
  382. amr_nb_decode_close,
  383. amr_nb_decode_frame,
  384. };
  385. AVCodec amr_nb_encoder =
  386. {
  387. "amr_nb",
  388. CODEC_TYPE_AUDIO,
  389. CODEC_ID_AMR_NB,
  390. sizeof(AMRContext),
  391. amr_nb_encode_init,
  392. amr_nb_encode_frame,
  393. amr_nb_encode_close,
  394. NULL,
  395. };
  396. /* -----------AMR wideband ------------*/
  397. #ifdef AMR_WB
  398. #ifdef _TYPEDEF_H
  399. //To avoid duplicate typedefs from typdef in amr-nb
  400. #define typedef_h
  401. #endif
  402. #include "amrwb_float/enc_if.h"
  403. #include "amrwb_float/dec_if.h"
  404. /* Common code for fixed and float version*/
  405. typedef struct AMRWB_bitrates
  406. {
  407. int startrate;
  408. int stoprate;
  409. int mode;
  410. } AMRWB_bitrates;
  411. static int getWBBitrateMode(int bitrate)
  412. {
  413. /* Adjusted so that all bitrates can be used from commandline where
  414. only a multiple of 1000 can be specified*/
  415. AMRWB_bitrates rates[]={ {0,7999,0}, //6.6kHz
  416. {8000,9999,1},//8.85
  417. {10000,13000,2},//12.65
  418. {13001,14999,3},//14.25
  419. {15000,17000,4},//15.85
  420. {17001,18000,5},//18.25
  421. {18001,22000,6},//19.85
  422. {22001,23000,7},//23.05
  423. {23001,24000,8},//23.85
  424. };
  425. int i;
  426. for(i=0;i<9;i++)
  427. {
  428. if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
  429. {
  430. return(rates[i].mode);
  431. }
  432. }
  433. /*Return highest possible*/
  434. return(8);
  435. }
  436. typedef struct AMRWBContext {
  437. int frameCount;
  438. void *state;
  439. int mode;
  440. Word16 allow_dtx;
  441. } AMRWBContext;
  442. static int amr_wb_encode_init(AVCodecContext * avctx)
  443. {
  444. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  445. s->frameCount=0;
  446. if(avctx->sample_rate!=16000)
  447. {
  448. if(avctx->debug)
  449. {
  450. fprintf(stderr, "Only 16000Hz sample rate supported\n");
  451. }
  452. return -1;
  453. }
  454. if(avctx->channels!=1)
  455. {
  456. if(avctx->debug)
  457. {
  458. fprintf(stderr, "Only mono supported\n");
  459. }
  460. return -1;
  461. }
  462. avctx->frame_size=320;
  463. avctx->coded_frame= avcodec_alloc_frame();
  464. s->state = E_IF_init();
  465. s->mode=getWBBitrateMode(avctx->bit_rate);
  466. s->allow_dtx=0;
  467. return 0;
  468. }
  469. static int amr_wb_encode_close(AVCodecContext * avctx)
  470. {
  471. AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
  472. E_IF_exit(s->state);
  473. av_freep(&avctx->coded_frame);
  474. s->frameCount++;
  475. return 0;
  476. }
  477. static int amr_wb_encode_frame(AVCodecContext *avctx,
  478. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  479. {
  480. AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
  481. int size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
  482. return size;
  483. }
  484. static int amr_wb_decode_init(AVCodecContext * avctx)
  485. {
  486. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  487. s->frameCount=0;
  488. s->state = D_IF_init();
  489. return 0;
  490. }
  491. extern const UWord8 block_size[];
  492. static int amr_wb_decode_frame(AVCodecContext * avctx,
  493. void *data, int *data_size,
  494. uint8_t * buf, int buf_size)
  495. {
  496. AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
  497. uint8_t*amrData=buf;
  498. int offset=0;
  499. int mode;
  500. int packet_size;
  501. *data_size=0;
  502. while(offset<buf_size)
  503. {
  504. s->frameCount++;
  505. mode = (Word16)((amrData[offset] >> 3) & 0x0F);
  506. packet_size = block_size[mode];
  507. D_IF_decode( s->state, &amrData[offset], data+*data_size, _good_frame);
  508. *data_size+=320*2;
  509. offset+=packet_size;
  510. }
  511. return buf_size;
  512. }
  513. static int amr_wb_decode_close(AVCodecContext * avctx)
  514. {
  515. AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
  516. D_IF_exit(s->state);
  517. return 0;
  518. }
  519. AVCodec amr_wb_decoder =
  520. {
  521. "amr_wb",
  522. CODEC_TYPE_AUDIO,
  523. CODEC_ID_AMR_WB,
  524. sizeof(AMRWBContext),
  525. amr_wb_decode_init,
  526. NULL,
  527. amr_wb_decode_close,
  528. amr_wb_decode_frame,
  529. };
  530. AVCodec amr_wb_encoder =
  531. {
  532. "amr_wb",
  533. CODEC_TYPE_AUDIO,
  534. CODEC_ID_AMR_WB,
  535. sizeof(AMRWBContext),
  536. amr_wb_encode_init,
  537. amr_wb_encode_frame,
  538. amr_wb_encode_close,
  539. NULL,
  540. };
  541. #endif //AMR_WB