e3164b30082a003eb2b6f6b2a72047c99bc37586
[libav.git] / libavcodec / amr.c
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 /** @file
23 * Adaptive Multi-Rate (AMR) Audio decoder stub.
24 *
25 * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand
26 * (AMR-WB) audio encoder/decoder through external reference code from
27 * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you
28 * have to download the code separately. Two versions exists: One fixed-point
29 * and one with floats. For some reason the float-encoder is significant faster
30 * at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102).
31 * Both float and fixed point are supported for AMR-NB, but only float for
32 * AMR-WB.
33 *
34 * \section AMR-NB
35 *
36 * \subsection Float
37 * The float version (default) can be downloaded from:
38 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip
39 * Extract the source into \c "ffmpeg/libavcodec/amr_float".
40 *
41 * \subsection Fixed-point
42 * The fixed-point (TS26.073) can be downloaded from:
43 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip.
44 * Extract the source into \c "ffmpeg/libavcodec/amr".
45 * To use the fixed version run \c "./configure" with \c "--enable-amr_nb-fixed".
46 *
47 * \subsection Specification
48 * The specification for AMR-NB can be found in TS 26.071
49 * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
50 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
51 *
52 * \section AMR-WB
53 * \subsection Float
54 * The reference code can be downloaded from:
55 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip
56 * It should be extracted to \c "ffmpeg/libavcodec/amrwb_float". Enable it with
57 * \c "--enable-amr_wb".
58 *
59 * \subsection Fixed-point
60 * If someone wants to use the fixed point version it can be downloaded from:
61 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip.
62 *
63 * \subsection Specification
64 * The specification for AMR-WB can be downloaded from:
65 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.171/26171-500.zip.
66 *
67 */
68
69 #include "avcodec.h"
70
71 #ifdef CONFIG_AMR_NB_FIXED
72
73 #define MMS_IO
74
75 #include "amr/sp_dec.h"
76 #include "amr/d_homing.h"
77 #include "amr/typedef.h"
78 #include "amr/sp_enc.h"
79 #include "amr/sid_sync.h"
80 #include "amr/e_homing.h"
81
82 #else
83 #include "amr_float/interf_dec.h"
84 #include "amr_float/interf_enc.h"
85 #endif
86
87 /* Common code for fixed and float version*/
88 typedef struct AMR_bitrates
89 {
90 int startrate;
91 int stoprate;
92 enum Mode mode;
93 } AMR_bitrates;
94
95 /* Match desired bitrate with closest one*/
96 static enum Mode getBitrateMode(int bitrate)
97 {
98 /* Adjusted so that all bitrates can be used from commandline where
99 only a multiple of 1000 can be specified*/
100 AMR_bitrates rates[]={ {0,4999,MR475}, //4
101 {5000,5899,MR515},//5
102 {5900,6699,MR59},//6
103 {6700,7000,MR67},//7
104 {7001,7949,MR74},//8
105 {7950,9999,MR795},//9
106 {10000,11999,MR102},//10
107 {12000,64000,MR122},//12
108 };
109 int i;
110
111 for(i=0;i<8;i++)
112 {
113 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
114 {
115 return(rates[i].mode);
116 }
117 }
118 /*Return highest possible*/
119 return(MR122);
120 }
121
122 static void amr_decode_fix_avctx(AVCodecContext * avctx)
123 {
124 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
125
126 if(avctx->sample_rate == 0)
127 {
128 avctx->sample_rate = 8000 * is_amr_wb;
129 }
130
131 if(avctx->channels == 0)
132 {
133 avctx->channels = 1;
134 }
135
136 avctx->frame_size = 160 * is_amr_wb;
137 }
138
139 #ifdef CONFIG_AMR_NB_FIXED
140 /* fixed point version*/
141 /* frame size in serial bitstream file (frame type + serial stream + flags) */
142 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
143
144 typedef struct AMRContext {
145 int frameCount;
146 Speech_Decode_FrameState *speech_decoder_state;
147 enum RXFrameType rx_type;
148 enum Mode mode;
149 Word16 reset_flag;
150 Word16 reset_flag_old;
151
152 enum Mode enc_bitrate;
153 Speech_Encode_FrameState *enstate;
154 sid_syncState *sidstate;
155 enum TXFrameType tx_frametype;
156 } AMRContext;
157
158 static int amr_nb_decode_init(AVCodecContext * avctx)
159 {
160 AMRContext *s = avctx->priv_data;
161
162 s->frameCount=0;
163 s->speech_decoder_state=NULL;
164 s->rx_type = (enum RXFrameType)0;
165 s->mode= (enum Mode)0;
166 s->reset_flag=0;
167 s->reset_flag_old=1;
168
169 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
170 {
171 av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
172 return -1;
173 }
174
175 amr_decode_fix_avctx(avctx);
176
177 if(avctx->channels > 1)
178 {
179 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
180 return -1;
181 }
182
183 return 0;
184 }
185
186 static int amr_nb_encode_init(AVCodecContext * avctx)
187 {
188 AMRContext *s = avctx->priv_data;
189
190 s->frameCount=0;
191 s->speech_decoder_state=NULL;
192 s->rx_type = (enum RXFrameType)0;
193 s->mode= (enum Mode)0;
194 s->reset_flag=0;
195 s->reset_flag_old=1;
196
197 if(avctx->sample_rate!=8000)
198 {
199 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
200 return -1;
201 }
202
203 if(avctx->channels!=1)
204 {
205 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
206 return -1;
207 }
208
209 avctx->frame_size=160;
210 avctx->coded_frame= avcodec_alloc_frame();
211
212 if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
213 {
214 av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n");
215 return -1;
216 }
217
218 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
219
220 return 0;
221 }
222
223 static int amr_nb_encode_close(AVCodecContext * avctx)
224 {
225 AMRContext *s = avctx->priv_data;
226
227 Speech_Encode_Frame_exit(&s->enstate);
228 sid_sync_exit (&s->sidstate);
229 av_freep(&avctx->coded_frame);
230 return 0;
231 }
232
233 static int amr_nb_decode_close(AVCodecContext * avctx)
234 {
235 AMRContext *s = avctx->priv_data;
236
237 Speech_Decode_Frame_exit(&s->speech_decoder_state);
238 return 0;
239 }
240
241 static int amr_nb_decode_frame(AVCodecContext * avctx,
242 void *data, int *data_size,
243 uint8_t * buf, int buf_size)
244 {
245 AMRContext *s = avctx->priv_data;
246 uint8_t*amrData=buf;
247 int offset=0;
248 UWord8 toc, q, ft;
249 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
250 Word16 *synth;
251 UWord8 *packed_bits;
252 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
253 int i;
254
255 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
256
257 synth=data;
258
259 // while(offset<buf_size)
260 {
261 toc=amrData[offset];
262 /* read rest of the frame based on ToC byte */
263 q = (toc >> 2) & 0x01;
264 ft = (toc >> 3) & 0x0F;
265
266 //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]);
267
268 offset++;
269
270 packed_bits=amrData+offset;
271
272 offset+=packed_size[ft];
273
274 //Unsort and unpack bits
275 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
276
277 //We have a new frame
278 s->frameCount++;
279
280 if (s->rx_type == RX_NO_DATA)
281 {
282 s->mode = s->speech_decoder_state->prev_mode;
283 }
284 else {
285 s->speech_decoder_state->prev_mode = s->mode;
286 }
287
288 /* if homed: check if this frame is another homing frame */
289 if (s->reset_flag_old == 1)
290 {
291 /* only check until end of first subframe */
292 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
293 }
294 /* produce encoder homing frame if homed & input=decoder homing frame */
295 if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
296 {
297 for (i = 0; i < L_FRAME; i++)
298 {
299 synth[i] = EHF_MASK;
300 }
301 }
302 else
303 {
304 /* decode frame */
305 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
306 }
307
308 //Each AMR-frame results in 160 16-bit samples
309 *data_size+=160*2;
310 synth+=160;
311
312 /* if not homed: check whether current frame is a homing frame */
313 if (s->reset_flag_old == 0)
314 {
315 /* check whole frame */
316 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
317 }
318 /* reset decoder if current frame is a homing frame */
319 if (s->reset_flag != 0)
320 {
321 Speech_Decode_Frame_reset(s->speech_decoder_state);
322 }
323 s->reset_flag_old = s->reset_flag;
324
325 }
326 return offset;
327 }
328
329
330 static int amr_nb_encode_frame(AVCodecContext *avctx,
331 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
332 {
333 short serial_data[250] = {0};
334 AMRContext *s = avctx->priv_data;
335 int written;
336
337 s->reset_flag = encoder_homing_frame_test(data);
338
339 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
340
341 /* add frame type and mode */
342 sid_sync (s->sidstate, s->mode, &s->tx_frametype);
343
344 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
345
346 if (s->reset_flag != 0)
347 {
348 Speech_Encode_Frame_reset(s->enstate);
349 sid_sync_reset(s->sidstate);
350 }
351 return written;
352 }
353
354
355 #elif defined(CONFIG_AMR_NB) /* Float point version*/
356
357 typedef struct AMRContext {
358 int frameCount;
359 void * decState;
360 int *enstate;
361 enum Mode enc_bitrate;
362 } AMRContext;
363
364 static int amr_nb_decode_init(AVCodecContext * avctx)
365 {
366 AMRContext *s = avctx->priv_data;
367
368 s->frameCount=0;
369 s->decState=Decoder_Interface_init();
370 if(!s->decState)
371 {
372 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
373 return -1;
374 }
375
376 amr_decode_fix_avctx(avctx);
377
378 if(avctx->channels > 1)
379 {
380 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
381 return -1;
382 }
383
384 return 0;
385 }
386
387 static int amr_nb_encode_init(AVCodecContext * avctx)
388 {
389 AMRContext *s = avctx->priv_data;
390
391 s->frameCount=0;
392
393 if(avctx->sample_rate!=8000)
394 {
395 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
396 return -1;
397 }
398
399 if(avctx->channels!=1)
400 {
401 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
402 return -1;
403 }
404
405 avctx->frame_size=160;
406 avctx->coded_frame= avcodec_alloc_frame();
407
408 s->enstate=Encoder_Interface_init(0);
409 if(!s->enstate)
410 {
411 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
412 return -1;
413 }
414
415 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
416
417 return 0;
418 }
419
420 static int amr_nb_decode_close(AVCodecContext * avctx)
421 {
422 AMRContext *s = avctx->priv_data;
423
424 Decoder_Interface_exit(s->decState);
425 return 0;
426 }
427
428 static int amr_nb_encode_close(AVCodecContext * avctx)
429 {
430 AMRContext *s = avctx->priv_data;
431
432 Encoder_Interface_exit(s->enstate);
433 av_freep(&avctx->coded_frame);
434 return 0;
435 }
436
437 static int amr_nb_decode_frame(AVCodecContext * avctx,
438 void *data, int *data_size,
439 uint8_t * buf, int buf_size)
440 {
441 AMRContext *s = avctx->priv_data;
442 uint8_t*amrData=buf;
443 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
444 enum Mode dec_mode;
445 int packet_size;
446
447 /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */
448
449 dec_mode = (buf[0] >> 3) & 0x000F;
450 packet_size = block_size[dec_mode]+1;
451
452 if(packet_size > buf_size) {
453 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
454 return -1;
455 }
456
457 s->frameCount++;
458 /* 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]); */
459 /* call decoder */
460 Decoder_Interface_Decode(s->decState, amrData, data, 0);
461 *data_size=160*2;
462
463 return packet_size;
464 }
465
466 static int amr_nb_encode_frame(AVCodecContext *avctx,
467 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
468 {
469 AMRContext *s = avctx->priv_data;
470 int written;
471
472 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
473
474 written = Encoder_Interface_Encode(s->enstate,
475 s->enc_bitrate,
476 data,
477 frame,
478 0);
479 /* 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] ); */
480
481 return written;
482 }
483
484 #endif
485
486 #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
487
488 AVCodec amr_nb_decoder =
489 {
490 "amr_nb",
491 CODEC_TYPE_AUDIO,
492 CODEC_ID_AMR_NB,
493 sizeof(AMRContext),
494 amr_nb_decode_init,
495 NULL,
496 amr_nb_decode_close,
497 amr_nb_decode_frame,
498 };
499
500 AVCodec amr_nb_encoder =
501 {
502 "amr_nb",
503 CODEC_TYPE_AUDIO,
504 CODEC_ID_AMR_NB,
505 sizeof(AMRContext),
506 amr_nb_encode_init,
507 amr_nb_encode_frame,
508 amr_nb_encode_close,
509 NULL,
510 };
511
512 #endif
513
514 /* -----------AMR wideband ------------*/
515 #ifdef CONFIG_AMR_WB
516
517 #ifdef _TYPEDEF_H
518 //To avoid duplicate typedefs from typdef in amr-nb
519 #define typedef_h
520 #endif
521
522 #include "amrwb_float/enc_if.h"
523 #include "amrwb_float/dec_if.h"
524
525 /* Common code for fixed and float version*/
526 typedef struct AMRWB_bitrates
527 {
528 int startrate;
529 int stoprate;
530 int mode;
531 } AMRWB_bitrates;
532
533 static int getWBBitrateMode(int bitrate)
534 {
535 /* Adjusted so that all bitrates can be used from commandline where
536 only a multiple of 1000 can be specified*/
537 AMRWB_bitrates rates[]={ {0,7999,0}, //6.6kHz
538 {8000,9999,1},//8.85
539 {10000,13000,2},//12.65
540 {13001,14999,3},//14.25
541 {15000,17000,4},//15.85
542 {17001,18000,5},//18.25
543 {18001,22000,6},//19.85
544 {22001,23000,7},//23.05
545 {23001,24000,8},//23.85
546 };
547 int i;
548
549 for(i=0;i<9;i++)
550 {
551 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
552 {
553 return(rates[i].mode);
554 }
555 }
556 /*Return highest possible*/
557 return(8);
558 }
559
560
561 typedef struct AMRWBContext {
562 int frameCount;
563 void *state;
564 int mode;
565 Word16 allow_dtx;
566 } AMRWBContext;
567
568 static int amr_wb_encode_init(AVCodecContext * avctx)
569 {
570 AMRWBContext *s = avctx->priv_data;
571
572 s->frameCount=0;
573
574 if(avctx->sample_rate!=16000)
575 {
576 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
577 return -1;
578 }
579
580 if(avctx->channels!=1)
581 {
582 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
583 return -1;
584 }
585
586 avctx->frame_size=320;
587 avctx->coded_frame= avcodec_alloc_frame();
588
589 s->state = E_IF_init();
590 s->mode=getWBBitrateMode(avctx->bit_rate);
591 s->allow_dtx=0;
592
593 return 0;
594 }
595
596 static int amr_wb_encode_close(AVCodecContext * avctx)
597 {
598 AMRWBContext *s = avctx->priv_data;
599
600 E_IF_exit(s->state);
601 av_freep(&avctx->coded_frame);
602 s->frameCount++;
603 return 0;
604 }
605
606 static int amr_wb_encode_frame(AVCodecContext *avctx,
607 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
608 {
609 AMRWBContext *s = avctx->priv_data;
610 int size;
611
612 s->mode=getWBBitrateMode(avctx->bit_rate);
613 size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
614 return size;
615 }
616
617 static int amr_wb_decode_init(AVCodecContext * avctx)
618 {
619 AMRWBContext *s = avctx->priv_data;
620
621 s->frameCount=0;
622 s->state = D_IF_init();
623
624 amr_decode_fix_avctx(avctx);
625
626 if(avctx->channels > 1)
627 {
628 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
629 return -1;
630 }
631
632 return 0;
633 }
634
635 extern const UWord8 block_size[];
636
637 static int amr_wb_decode_frame(AVCodecContext * avctx,
638 void *data, int *data_size,
639 uint8_t * buf, int buf_size)
640 {
641 AMRWBContext *s = avctx->priv_data;
642 uint8_t*amrData=buf;
643 int mode;
644 int packet_size;
645
646 if(buf_size==0) {
647 /* nothing to do */
648 return 0;
649 }
650
651 mode = (amrData[0] >> 3) & 0x000F;
652 packet_size = block_size[mode];
653
654 if(packet_size > buf_size) {
655 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
656 return -1;
657 }
658
659 s->frameCount++;
660 D_IF_decode( s->state, amrData, data, _good_frame);
661 *data_size=320*2;
662 return packet_size;
663 }
664
665 static int amr_wb_decode_close(AVCodecContext * avctx)
666 {
667 AMRWBContext *s = avctx->priv_data;
668
669 D_IF_exit(s->state);
670 return 0;
671 }
672
673 AVCodec amr_wb_decoder =
674 {
675 "amr_wb",
676 CODEC_TYPE_AUDIO,
677 CODEC_ID_AMR_WB,
678 sizeof(AMRWBContext),
679 amr_wb_decode_init,
680 NULL,
681 amr_wb_decode_close,
682 amr_wb_decode_frame,
683 };
684
685 AVCodec amr_wb_encoder =
686 {
687 "amr_wb",
688 CODEC_TYPE_AUDIO,
689 CODEC_ID_AMR_WB,
690 sizeof(AMRWBContext),
691 amr_wb_encode_init,
692 amr_wb_encode_frame,
693 amr_wb_encode_close,
694 NULL,
695 };
696
697 #endif //CONFIG_AMR_WB