Mercurial > libavcodec.hg
comparison amr.c @ 2967:ef2149182f1c libavcodec
COSMETICS: Remove all trailing whitespace.
author | diego |
---|---|
date | Sat, 17 Dec 2005 18:14:38 +0000 |
parents | 8fe1dc88294a |
children | bfabfdf9ce55 |
comparison
equal
deleted
inserted
replaced
2966:564788471dd4 | 2967:ef2149182f1c |
---|---|
22 have to download the code separately. Two versions exists: One fixed-point | 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 | 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). | 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 | 25 Both float and fixed point is supported for amr-nb, but only float for |
26 amr-wb. | 26 amr-wb. |
27 | 27 |
28 --AMR-NB-- | 28 --AMR-NB-- |
29 The fixed-point (TS26.073) can be downloaded from: | 29 The fixed-point (TS26.073) can be downloaded from: |
30 http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip | 30 http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip |
31 Extract the soure into ffmpeg/libavcodec/amr | 31 Extract the soure into ffmpeg/libavcodec/amr |
32 To use the fixed version run "./configure" with "--enable-amr_nb-fixed" | 32 To use the fixed version run "./configure" with "--enable-amr_nb-fixed" |
33 | 33 |
34 The float version (default) can be downloaded from: | 34 The float version (default) can be downloaded from: |
35 http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip | 35 http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip |
36 Extract the soure into ffmpeg/libavcodec/amr_float | 36 Extract the soure into ffmpeg/libavcodec/amr_float |
37 | 37 |
38 The specification for amr-nb can be found in TS 26.071 | 38 The specification for amr-nb can be found in TS 26.071 |
39 (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other | 39 (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other |
40 info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm | 40 info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm |
41 | 41 |
42 --AMR-WB-- | 42 --AMR-WB-- |
43 The reference code can be downloaded from: | 43 The reference code can be downloaded from: |
44 http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip | 44 http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip |
45 It should be extracted to "libavcodec/amrwb_float". Enable it with | 45 It should be extracted to "libavcodec/amrwb_float". Enable it with |
46 "--enable-amr_wb". | 46 "--enable-amr_wb". |
47 | 47 |
48 The specification for amr-wb can be downloaded from: | 48 The specification for amr-wb can be downloaded from: |
49 http://www.3gpp.org/ftp/Specs/archive/26_series/26.171/26171-500.zip | 49 http://www.3gpp.org/ftp/Specs/archive/26_series/26.171/26171-500.zip |
50 | 50 |
51 If someone want to use the fixed point version it can be downloaded | 51 If someone want to use the fixed point version it can be downloaded |
52 from: http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip | 52 from: http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip |
53 | 53 |
54 */ | 54 */ |
55 | 55 |
56 #include "avcodec.h" | 56 #include "avcodec.h" |
57 | 57 |
58 #ifdef AMR_NB_FIXED | 58 #ifdef AMR_NB_FIXED |
75 typedef struct AMR_bitrates | 75 typedef struct AMR_bitrates |
76 { | 76 { |
77 int startrate; | 77 int startrate; |
78 int stoprate; | 78 int stoprate; |
79 enum Mode mode; | 79 enum Mode mode; |
80 | 80 |
81 } AMR_bitrates; | 81 } AMR_bitrates; |
82 | 82 |
83 /* Match desired bitrate with closest one*/ | 83 /* Match desired bitrate with closest one*/ |
84 static enum Mode getBitrateMode(int bitrate) | 84 static enum Mode getBitrateMode(int bitrate) |
85 { | 85 { |
91 {6700,7000,MR67},//7 | 91 {6700,7000,MR67},//7 |
92 {7001,7949,MR74},//8 | 92 {7001,7949,MR74},//8 |
93 {7950,9999,MR795},//9 | 93 {7950,9999,MR795},//9 |
94 {10000,11999,MR102},//10 | 94 {10000,11999,MR102},//10 |
95 {12000,64000,MR122},//12 | 95 {12000,64000,MR122},//12 |
96 | 96 |
97 }; | 97 }; |
98 int i; | 98 int i; |
99 for(i=0;i<8;i++) | 99 for(i=0;i<8;i++) |
100 { | 100 { |
101 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate) | 101 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate) |
122 | 122 |
123 enum Mode enc_bitrate; | 123 enum Mode enc_bitrate; |
124 Speech_Encode_FrameState *enstate; | 124 Speech_Encode_FrameState *enstate; |
125 sid_syncState *sidstate; | 125 sid_syncState *sidstate; |
126 enum TXFrameType tx_frametype; | 126 enum TXFrameType tx_frametype; |
127 | 127 |
128 | 128 |
129 } AMRContext; | 129 } AMRContext; |
130 | 130 |
131 static int amr_nb_decode_init(AVCodecContext * avctx) | 131 static int amr_nb_decode_init(AVCodecContext * avctx) |
132 { | 132 { |
135 s->speech_decoder_state=NULL; | 135 s->speech_decoder_state=NULL; |
136 s->rx_type = (enum RXFrameType)0; | 136 s->rx_type = (enum RXFrameType)0; |
137 s->mode= (enum Mode)0; | 137 s->mode= (enum Mode)0; |
138 s->reset_flag=0; | 138 s->reset_flag=0; |
139 s->reset_flag_old=1; | 139 s->reset_flag_old=1; |
140 | 140 |
141 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder")) | 141 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder")) |
142 { | 142 { |
143 av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n"); | 143 av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n"); |
144 return -1; | 144 return -1; |
145 } | 145 } |
153 s->speech_decoder_state=NULL; | 153 s->speech_decoder_state=NULL; |
154 s->rx_type = (enum RXFrameType)0; | 154 s->rx_type = (enum RXFrameType)0; |
155 s->mode= (enum Mode)0; | 155 s->mode= (enum Mode)0; |
156 s->reset_flag=0; | 156 s->reset_flag=0; |
157 s->reset_flag_old=1; | 157 s->reset_flag_old=1; |
158 | 158 |
159 if(avctx->sample_rate!=8000) | 159 if(avctx->sample_rate!=8000) |
160 { | 160 { |
161 if(avctx->debug) | 161 if(avctx->debug) |
162 { | 162 { |
163 av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n"); | 163 av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n"); |
215 | 215 |
216 uint8_t*amrData=buf; | 216 uint8_t*amrData=buf; |
217 int offset=0; | 217 int offset=0; |
218 | 218 |
219 UWord8 toc, q, ft; | 219 UWord8 toc, q, ft; |
220 | 220 |
221 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ | 221 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ |
222 Word16 *synth; | 222 Word16 *synth; |
223 UWord8 *packed_bits; | 223 UWord8 *packed_bits; |
224 | 224 |
225 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; | 225 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; |
248 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); | 248 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); |
249 | 249 |
250 //We have a new frame | 250 //We have a new frame |
251 s->frameCount++; | 251 s->frameCount++; |
252 | 252 |
253 if (s->rx_type == RX_NO_DATA) | 253 if (s->rx_type == RX_NO_DATA) |
254 { | 254 { |
255 s->mode = s->speech_decoder_state->prev_mode; | 255 s->mode = s->speech_decoder_state->prev_mode; |
256 } | 256 } |
257 else { | 257 else { |
258 s->speech_decoder_state->prev_mode = s->mode; | 258 s->speech_decoder_state->prev_mode = s->mode; |
259 } | 259 } |
260 | 260 |
261 /* if homed: check if this frame is another homing frame */ | 261 /* if homed: check if this frame is another homing frame */ |
262 if (s->reset_flag_old == 1) | 262 if (s->reset_flag_old == 1) |
263 { | 263 { |
264 /* only check until end of first subframe */ | 264 /* only check until end of first subframe */ |
265 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); | 265 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); |
271 { | 271 { |
272 synth[i] = EHF_MASK; | 272 synth[i] = EHF_MASK; |
273 } | 273 } |
274 } | 274 } |
275 else | 275 else |
276 { | 276 { |
277 /* decode frame */ | 277 /* decode frame */ |
278 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); | 278 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); |
279 } | 279 } |
280 | 280 |
281 //Each AMR-frame results in 160 16-bit samples | 281 //Each AMR-frame results in 160 16-bit samples |
282 *data_size+=160*2; | 282 *data_size+=160*2; |
283 synth+=160; | 283 synth+=160; |
284 | 284 |
285 /* if not homed: check whether current frame is a homing frame */ | 285 /* if not homed: check whether current frame is a homing frame */ |
286 if (s->reset_flag_old == 0) | 286 if (s->reset_flag_old == 0) |
287 { | 287 { |
288 /* check whole frame */ | 288 /* check whole frame */ |
289 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); | 289 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); |
292 if (s->reset_flag != 0) | 292 if (s->reset_flag != 0) |
293 { | 293 { |
294 Speech_Decode_Frame_reset(s->speech_decoder_state); | 294 Speech_Decode_Frame_reset(s->speech_decoder_state); |
295 } | 295 } |
296 s->reset_flag_old = s->reset_flag; | 296 s->reset_flag_old = s->reset_flag; |
297 | 297 |
298 } | 298 } |
299 return offset; | 299 return offset; |
300 } | 300 } |
301 | 301 |
302 | 302 |
305 { | 305 { |
306 short serial_data[250] = {0}; | 306 short serial_data[250] = {0}; |
307 | 307 |
308 AMRContext *s = avctx->priv_data; | 308 AMRContext *s = avctx->priv_data; |
309 int written; | 309 int written; |
310 | 310 |
311 s->reset_flag = encoder_homing_frame_test(data); | 311 s->reset_flag = encoder_homing_frame_test(data); |
312 | 312 |
313 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); | 313 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); |
314 | 314 |
315 /* add frame type and mode */ | 315 /* add frame type and mode */ |
316 sid_sync (s->sidstate, s->mode, &s->tx_frametype); | 316 sid_sync (s->sidstate, s->mode, &s->tx_frametype); |
317 | 317 |
318 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame); | 318 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame); |
319 | 319 |
320 if (s->reset_flag != 0) | 320 if (s->reset_flag != 0) |
321 { | 321 { |
322 Speech_Encode_Frame_reset(s->enstate); | 322 Speech_Encode_Frame_reset(s->enstate); |
323 sid_sync_reset(s->sidstate); | 323 sid_sync_reset(s->sidstate); |
324 } | 324 } |
350 | 350 |
351 static int amr_nb_encode_init(AVCodecContext * avctx) | 351 static int amr_nb_encode_init(AVCodecContext * avctx) |
352 { | 352 { |
353 AMRContext *s = avctx->priv_data; | 353 AMRContext *s = avctx->priv_data; |
354 s->frameCount=0; | 354 s->frameCount=0; |
355 | 355 |
356 if(avctx->sample_rate!=8000) | 356 if(avctx->sample_rate!=8000) |
357 { | 357 { |
358 if(avctx->debug) | 358 if(avctx->debug) |
359 { | 359 { |
360 av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n"); | 360 av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n"); |
414 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; | 414 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; |
415 enum Mode dec_mode; | 415 enum Mode dec_mode; |
416 int packet_size; | 416 int packet_size; |
417 | 417 |
418 /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */ | 418 /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */ |
419 | 419 |
420 if(buf_size==0) { | 420 if(buf_size==0) { |
421 /* nothing to do */ | 421 /* nothing to do */ |
422 return 0; | 422 return 0; |
423 } | 423 } |
424 | 424 |
427 | 427 |
428 if(packet_size > buf_size) { | 428 if(packet_size > buf_size) { |
429 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size); | 429 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size); |
430 return -1; | 430 return -1; |
431 } | 431 } |
432 | 432 |
433 s->frameCount++; | 433 s->frameCount++; |
434 /* 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]); */ | 434 /* 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]); */ |
435 /* call decoder */ | 435 /* call decoder */ |
436 Decoder_Interface_Decode(s->decState, amrData, data, 0); | 436 Decoder_Interface_Decode(s->decState, amrData, data, 0); |
437 *data_size=160*2; | 437 *data_size=160*2; |
438 | 438 |
439 return packet_size; | 439 return packet_size; |
440 } | 440 } |
441 | 441 |
442 static int amr_nb_encode_frame(AVCodecContext *avctx, | 442 static int amr_nb_encode_frame(AVCodecContext *avctx, |
443 unsigned char *frame/*out*/, int buf_size, void *data/*in*/) | 443 unsigned char *frame/*out*/, int buf_size, void *data/*in*/) |
444 { | 444 { |
445 AMRContext *s = (AMRContext*)avctx->priv_data; | 445 AMRContext *s = (AMRContext*)avctx->priv_data; |
446 int written; | 446 int written; |
447 | 447 |
448 written = Encoder_Interface_Encode(s->enstate, | 448 written = Encoder_Interface_Encode(s->enstate, |
449 s->enc_bitrate, | 449 s->enc_bitrate, |
450 data, | 450 data, |
451 frame, | 451 frame, |
452 0); | 452 0); |
453 /* 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] ); */ | 453 /* 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] ); */ |
454 | 454 |
455 return written; | 455 return written; |
456 } | 456 } |
500 typedef struct AMRWB_bitrates | 500 typedef struct AMRWB_bitrates |
501 { | 501 { |
502 int startrate; | 502 int startrate; |
503 int stoprate; | 503 int stoprate; |
504 int mode; | 504 int mode; |
505 | 505 |
506 } AMRWB_bitrates; | 506 } AMRWB_bitrates; |
507 | 507 |
508 static int getWBBitrateMode(int bitrate) | 508 static int getWBBitrateMode(int bitrate) |
509 { | 509 { |
510 /* Adjusted so that all bitrates can be used from commandline where | 510 /* Adjusted so that all bitrates can be used from commandline where |
516 {15000,17000,4},//15.85 | 516 {15000,17000,4},//15.85 |
517 {17001,18000,5},//18.25 | 517 {17001,18000,5},//18.25 |
518 {18001,22000,6},//19.85 | 518 {18001,22000,6},//19.85 |
519 {22001,23000,7},//23.05 | 519 {22001,23000,7},//23.05 |
520 {23001,24000,8},//23.85 | 520 {23001,24000,8},//23.85 |
521 | 521 |
522 }; | 522 }; |
523 int i; | 523 int i; |
524 | 524 |
525 for(i=0;i<9;i++) | 525 for(i=0;i<9;i++) |
526 { | 526 { |
543 | 543 |
544 static int amr_wb_encode_init(AVCodecContext * avctx) | 544 static int amr_wb_encode_init(AVCodecContext * avctx) |
545 { | 545 { |
546 AMRWBContext *s = (AMRWBContext*)avctx->priv_data; | 546 AMRWBContext *s = (AMRWBContext*)avctx->priv_data; |
547 s->frameCount=0; | 547 s->frameCount=0; |
548 | 548 |
549 if(avctx->sample_rate!=16000) | 549 if(avctx->sample_rate!=16000) |
550 { | 550 { |
551 if(avctx->debug) | 551 if(avctx->debug) |
552 { | 552 { |
553 av_log(avctx, AV_LOG_DEBUG, "Only 16000Hz sample rate supported\n"); | 553 av_log(avctx, AV_LOG_DEBUG, "Only 16000Hz sample rate supported\n"); |
621 | 621 |
622 if(packet_size > buf_size) { | 622 if(packet_size > buf_size) { |
623 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1); | 623 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1); |
624 return -1; | 624 return -1; |
625 } | 625 } |
626 | 626 |
627 s->frameCount++; | 627 s->frameCount++; |
628 D_IF_decode( s->state, amrData, data, _good_frame); | 628 D_IF_decode( s->state, amrData, data, _good_frame); |
629 *data_size=320*2; | 629 *data_size=320*2; |
630 return packet_size; | 630 return packet_size; |
631 } | 631 } |