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.

716 lines
18KB

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