2 * AMR Audio decoder stub
3 * Copyright (c) 2003 the ffmpeg project
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.
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.
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
23 #include "amr/sp_dec.h"
24 #include "amr/d_homing.h"
25 #include "amr/typedef.h"
27 /* frame size in serial bitstream file (frame type + serial stream + flags) */
28 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
30 typedef struct AMRDecodeContext {
32 Speech_Decode_FrameState *speech_decoder_state;
33 enum RXFrameType rx_type;
36 Word16 reset_flag_old;
40 static int amr_nb_decode_init(AVCodecContext * avctx)
42 AMRDecodeContext *s = avctx->priv_data;
44 s->speech_decoder_state=NULL;
45 s->rx_type = (enum RXFrameType)0;
46 s->mode= (enum Mode)0;
50 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
58 static int amr_decode_close(AVCodecContext * avctx)
60 AMRDecodeContext *s = avctx->priv_data;
61 Speech_Decode_Frame_exit(&s->speech_decoder_state);
64 static int amr_nb_decode_frame(AVCodecContext * avctx,
65 void *data, int *data_size,
66 uint8_t * buf, int buf_size)
68 AMRDecodeContext *s = avctx->priv_data;
75 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
79 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
82 //printf("amr_decode_frame data=0x%X data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",data,&data_size,buf,buf_size,s->frameCount);
86 while(offset<buf_size)
88 toc=amrData[offset++];
89 /* read rest of the frame based on ToC byte */
90 q = (toc >> 2) & 0x01;
91 ft = (toc >> 3) & 0x0F;
93 packed_bits=amrData+offset;
95 offset+=packed_size[ft];
97 //Unsort and unpack bits
98 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
100 //We have a new frame
103 if (s->rx_type == RX_NO_DATA)
105 s->mode = s->speech_decoder_state->prev_mode;
108 s->speech_decoder_state->prev_mode = s->mode;
111 /* if homed: check if this frame is another homing frame */
112 if (s->reset_flag_old == 1)
114 /* only check until end of first subframe */
115 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
117 /* produce encoder homing frame if homed & input=decoder homing frame */
118 if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
120 for (i = 0; i < L_FRAME; i++)
128 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
131 //Each AMR-frame results in 160 16-bit samples
135 /* if not homed: check whether current frame is a homing frame */
136 if (s->reset_flag_old == 0)
138 /* check whole frame */
139 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
141 /* reset decoder if current frame is a homing frame */
142 if (s->reset_flag != 0)
144 Speech_Decode_Frame_reset(s->speech_decoder_state);
146 s->reset_flag_old = s->reset_flag;
152 AVCodec amr_nb_decoder =
157 sizeof(AMRDecodeContext),