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.

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