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.

461 lines
12KB

  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 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. The fixed-point (TS26.073) can be downloaded from:
  26. http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26073-510.zip
  27. Extract the soure into ffmpeg/libavcodec/amr
  28. To use the float version run "./configure" with "--enable-amr-nb-fixed"
  29. The float version (default) can be downloaded from:
  30. http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26104-510.zip
  31. Extract the soure into ffmpeg/libavcodec/amr_float
  32. The specification for amr-nb can be found in TS 26.071
  33. (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
  34. info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm
  35. In the future support for AMR-WB might also be included here.
  36. Reference code exist in TS26.173 and TS 26.204.
  37. */
  38. #define DEBUG
  39. //#define AMR_NB_FIXED
  40. #include "../config.h"
  41. #include "avcodec.h"
  42. #ifdef AMR_NB_FIXED
  43. #define MMS_IO
  44. #include "amr/sp_dec.h"
  45. #include "amr/d_homing.h"
  46. #include "amr/typedef.h"
  47. #include "amr/sp_enc.h"
  48. #include "amr/sid_sync.h"
  49. #include "amr/e_homing.h"
  50. #else
  51. #include "amr_float/interf_dec.h"
  52. #include "amr_float/interf_enc.h"
  53. #endif
  54. /* Common code for fixed and float version*/
  55. typedef struct AMR_bitrates
  56. {
  57. int startrate;
  58. int stoprate;
  59. enum Mode mode;
  60. } AMR_bitrates;
  61. /* Match desired bitrate with closest one*/
  62. static enum Mode getBitrateMode(int bitrate)
  63. {
  64. /* Adjusted so that all bitrates can be used from commandline where
  65. only a multiple of 1000 can be specified*/
  66. AMR_bitrates rates[]={ {0,4999,MR475}, //4
  67. {5000,5899,MR515},//5
  68. {5900,6699,MR59},//6
  69. {6700,7000,MR67},//7
  70. {7001,7949,MR74},//8
  71. {7950,9999,MR795},//9
  72. {10000,11999,MR102},//10
  73. {12000,64000,MR122},//12
  74. };
  75. int i;
  76. for(i=0;i<sizeof(rates);i++)
  77. {
  78. if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
  79. {
  80. return(rates[i].mode);
  81. }
  82. }
  83. /*Return highest possible*/
  84. return(MR122);
  85. }
  86. #ifdef AMR_NB_FIXED
  87. /* fixed point version*/
  88. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  89. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  90. typedef struct AMRContext {
  91. int frameCount;
  92. Speech_Decode_FrameState *speech_decoder_state;
  93. enum RXFrameType rx_type;
  94. enum Mode mode;
  95. Word16 reset_flag;
  96. Word16 reset_flag_old;
  97. enum Mode enc_bitrate;
  98. Speech_Encode_FrameState *enstate;
  99. sid_syncState *sidstate;
  100. enum TXFrameType tx_frametype;
  101. } AMRContext;
  102. static int amr_nb_decode_init(AVCodecContext * avctx)
  103. {
  104. AMRContext *s = avctx->priv_data;
  105. s->frameCount=0;
  106. s->speech_decoder_state=NULL;
  107. s->rx_type = (enum RXFrameType)0;
  108. s->mode= (enum Mode)0;
  109. s->reset_flag=0;
  110. s->reset_flag_old=1;
  111. if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
  112. {
  113. printf("Speech_Decode_Frame_init error\n");
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. static int amr_nb_encode_init(AVCodecContext * avctx)
  119. {
  120. AMRContext *s = avctx->priv_data;
  121. s->frameCount=0;
  122. s->speech_decoder_state=NULL;
  123. s->rx_type = (enum RXFrameType)0;
  124. s->mode= (enum Mode)0;
  125. s->reset_flag=0;
  126. s->reset_flag_old=1;
  127. if(avctx->sample_rate!=8000)
  128. {
  129. #ifdef DEBUG
  130. printf("Only 8000Hz sample rate supported\n");
  131. #endif
  132. return -1;
  133. }
  134. if(avctx->channels!=1)
  135. {
  136. #ifdef DEBUG
  137. printf("Only mono supported\n");
  138. #endif
  139. return -1;
  140. }
  141. avctx->frame_size=160;
  142. avctx->coded_frame= avcodec_alloc_frame();
  143. if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
  144. {
  145. #ifdef DEBUG
  146. printf("Speech_Encode_Frame_init error\n");
  147. #endif
  148. return -1;
  149. }
  150. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  151. return 0;
  152. }
  153. static int amr_nb_encode_close(AVCodecContext * avctx)
  154. {
  155. AMRContext *s = avctx->priv_data;
  156. Speech_Encode_Frame_exit(&s->enstate);
  157. sid_sync_exit (&s->sidstate);
  158. av_freep(&avctx->coded_frame);
  159. return 0;
  160. }
  161. static int amr_nb_decode_close(AVCodecContext * avctx)
  162. {
  163. AMRContext *s = avctx->priv_data;
  164. Speech_Decode_Frame_exit(&s->speech_decoder_state);
  165. return 0;
  166. }
  167. static int amr_nb_decode_frame(AVCodecContext * avctx,
  168. void *data, int *data_size,
  169. uint8_t * buf, int buf_size)
  170. {
  171. AMRContext *s = avctx->priv_data;
  172. uint8_t*amrData=buf;
  173. int offset=0;
  174. UWord8 toc, q, ft;
  175. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  176. Word16 *synth;
  177. UWord8 *packed_bits;
  178. *data_size=0;
  179. static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  180. int i;
  181. //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
  182. synth=data;
  183. // while(offset<buf_size)
  184. {
  185. toc=amrData[offset];
  186. /* read rest of the frame based on ToC byte */
  187. q = (toc >> 2) & 0x01;
  188. ft = (toc >> 3) & 0x0F;
  189. //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]);
  190. offset++;
  191. packed_bits=amrData+offset;
  192. offset+=packed_size[ft];
  193. //Unsort and unpack bits
  194. s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
  195. //We have a new frame
  196. s->frameCount++;
  197. if (s->rx_type == RX_NO_DATA)
  198. {
  199. s->mode = s->speech_decoder_state->prev_mode;
  200. }
  201. else {
  202. s->speech_decoder_state->prev_mode = s->mode;
  203. }
  204. /* if homed: check if this frame is another homing frame */
  205. if (s->reset_flag_old == 1)
  206. {
  207. /* only check until end of first subframe */
  208. s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
  209. }
  210. /* produce encoder homing frame if homed & input=decoder homing frame */
  211. if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
  212. {
  213. for (i = 0; i < L_FRAME; i++)
  214. {
  215. synth[i] = EHF_MASK;
  216. }
  217. }
  218. else
  219. {
  220. /* decode frame */
  221. Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
  222. }
  223. //Each AMR-frame results in 160 16-bit samples
  224. *data_size+=160*2;
  225. synth+=160;
  226. /* if not homed: check whether current frame is a homing frame */
  227. if (s->reset_flag_old == 0)
  228. {
  229. /* check whole frame */
  230. s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
  231. }
  232. /* reset decoder if current frame is a homing frame */
  233. if (s->reset_flag != 0)
  234. {
  235. Speech_Decode_Frame_reset(s->speech_decoder_state);
  236. }
  237. s->reset_flag_old = s->reset_flag;
  238. }
  239. return offset;
  240. }
  241. static int amr_nb_encode_frame(AVCodecContext *avctx,
  242. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  243. {
  244. short serial_data[250] = {0};
  245. AMRContext *s = avctx->priv_data;
  246. int written;
  247. s->reset_flag = encoder_homing_frame_test(data);
  248. Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
  249. /* add frame type and mode */
  250. sid_sync (s->sidstate, s->mode, &s->tx_frametype);
  251. written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
  252. if (s->reset_flag != 0)
  253. {
  254. Speech_Encode_Frame_reset(s->enstate);
  255. sid_sync_reset(s->sidstate);
  256. }
  257. return written;
  258. }
  259. #else /* Float point version*/
  260. typedef struct AMRContext {
  261. int frameCount;
  262. void * decState;
  263. int *enstate;
  264. enum Mode enc_bitrate;
  265. } AMRContext;
  266. static int amr_nb_decode_init(AVCodecContext * avctx)
  267. {
  268. AMRContext *s = avctx->priv_data;
  269. s->frameCount=0;
  270. s->decState=Decoder_Interface_init();
  271. if(!s->decState)
  272. {
  273. printf("Decoder_Interface_init error\r\n");
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. static int amr_nb_encode_init(AVCodecContext * avctx)
  279. {
  280. AMRContext *s = avctx->priv_data;
  281. s->frameCount=0;
  282. if(avctx->sample_rate!=8000)
  283. {
  284. #ifdef DEBUG
  285. printf("Only 8000Hz sample rate supported\n");
  286. #endif
  287. return -1;
  288. }
  289. if(avctx->channels!=1)
  290. {
  291. #ifdef DEBUG
  292. printf("Only mono supported\n");
  293. #endif
  294. return -1;
  295. }
  296. avctx->frame_size=160;
  297. avctx->coded_frame= avcodec_alloc_frame();
  298. s->enstate=Encoder_Interface_init(0);
  299. if(!s->enstate)
  300. {
  301. printf("Encoder_Interface_init error\n");
  302. return -1;
  303. }
  304. s->enc_bitrate=getBitrateMode(avctx->bit_rate);
  305. return 0;
  306. }
  307. static int amr_nb_decode_close(AVCodecContext * avctx)
  308. {
  309. AMRContext *s = avctx->priv_data;
  310. Decoder_Interface_exit(s->decState);
  311. return 0;
  312. }
  313. static int amr_nb_encode_close(AVCodecContext * avctx)
  314. {
  315. AMRContext *s = avctx->priv_data;
  316. Encoder_Interface_exit(s->enstate);
  317. av_freep(&avctx->coded_frame);
  318. return 0;
  319. }
  320. static int amr_nb_decode_frame(AVCodecContext * avctx,
  321. void *data, int *data_size,
  322. uint8_t * buf, int buf_size)
  323. {
  324. AMRContext *s = (AMRContext*)avctx->priv_data;
  325. uint8_t*amrData=buf;
  326. int offset=0;
  327. static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
  328. enum Mode dec_mode;
  329. int packet_size;
  330. *data_size=0;
  331. //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
  332. while(offset<buf_size)
  333. {
  334. dec_mode = (amrData[offset] >> 3) & 0x000F;
  335. packet_size = block_size[dec_mode];
  336. s->frameCount++;
  337. //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]);
  338. /* call decoder */
  339. Decoder_Interface_Decode(s->decState, &amrData[offset], data+*data_size, 0);
  340. *data_size+=160*2;
  341. offset+=packet_size+1;
  342. }
  343. return buf_size;
  344. }
  345. static int amr_nb_encode_frame(AVCodecContext *avctx,
  346. unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
  347. {
  348. AMRContext *s = (AMRContext*)avctx->priv_data;
  349. int written;
  350. written = Encoder_Interface_Encode(s->enstate,
  351. s->enc_bitrate,
  352. data,
  353. frame,
  354. 0);
  355. return written;
  356. }
  357. #endif
  358. AVCodec amr_nb_decoder =
  359. {
  360. "amr_nb",
  361. CODEC_TYPE_AUDIO,
  362. CODEC_ID_AMR_NB,
  363. sizeof(AMRContext),
  364. amr_nb_decode_init,
  365. NULL,
  366. amr_nb_decode_close,
  367. amr_nb_decode_frame,
  368. };
  369. AVCodec amr_nb_encoder =
  370. {
  371. "amr_nb",
  372. CODEC_TYPE_AUDIO,
  373. CODEC_ID_AMR_NB,
  374. sizeof(AMRContext),
  375. amr_nb_encode_init,
  376. amr_nb_encode_frame,
  377. amr_nb_encode_close,
  378. NULL,
  379. };