Statistics
| Branch: | Revision:

ffmpeg / libavcodec / amr.c @ ee4c54a3

History | View | Annotate | Download (18.1 KB)

1 891f64b3 joca@rixmail.se
/*
2
 * AMR Audio decoder stub
3
 * Copyright (c) 2003 the ffmpeg project
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 891f64b3 joca@rixmail.se
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 891f64b3 joca@rixmail.se
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 891f64b3 joca@rixmail.se
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 891f64b3 joca@rixmail.se
 */
21 115329f1 Diego Biurrun
22 e0b67c76 Panagiotis Issaris
 /** @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 ee4c54a3 Diego Biurrun
 * and one floating-point. For some reason the float encoder is significantly
30
 * faster at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip
31
 * at MR102). Both float and fixed point are supported for AMR-NB, but only
32
 * float for AMR-WB.
33 e0b67c76 Panagiotis Issaris
 *
34
 * \section AMR-NB
35
 *
36
 * \subsection Float
37
 * The float version (default) can be downloaded from:
38 2c48a21c Robert Swain
 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip
39 e0b67c76 Panagiotis Issaris
 * Extract the source into \c "ffmpeg/libavcodec/amr_float".
40 7038fa61 Diego Biurrun
 * Enable it by passing \c "--enable-amr-nb" to \c "./configure".
41 79f20349 Diego Biurrun
 * If you try this on Alpha, you may need to change \c "Word32" to
42
 * \c "int" in \c "amr/typedef.h".
43 e0b67c76 Panagiotis Issaris
 *
44
 * \subsection Fixed-point
45
 * The fixed-point (TS26.073) can be downloaded from:
46 0c8351b8 Diego Biurrun
 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-600.zip
47 79f20349 Diego Biurrun
 * Extract the source into \c "ffmpeg/libavcodec/amr" and add \c "-DMMS_IO" to
48
 * \c "CFLAGS" in \c "libavcodec/amr/makefile", i.e.
49
 * \c "CFLAGS = -Wall -pedantic-errors -I. $(CFLAGS_$(MODE)) -D$(VAD) -DMMS_IO".
50 7038fa61 Diego Biurrun
 * Enable it by passing \c "--enable-amr-nb-fixed" to \c "./configure".
51 e0b67c76 Panagiotis Issaris
 *
52
 * \subsection Specification
53
 * The specification for AMR-NB can be found in TS 26.071
54
 * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
55
 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
56
 *
57
 * \section AMR-WB
58
 * \subsection Float
59
 * The reference code can be downloaded from:
60 2c48a21c Robert Swain
 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
61 7038fa61 Diego Biurrun
 * It should be extracted to \c "ffmpeg/libavcodec/amrwb_float".
62
 * Enable it by passing \c "--enable-amr-wb" to \c "./configure".
63 e0b67c76 Panagiotis Issaris
 *
64
 * \subsection Fixed-point
65
 * If someone wants to use the fixed point version it can be downloaded from:
66
 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip.
67
 *
68
 * \subsection Specification
69 2c48a21c Robert Swain
 * The specification for AMR-WB can be found in TS 26.171
70
 * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other
71
 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
72 e0b67c76 Panagiotis Issaris
 *
73 bc634f6f Zdenek Kabelac
 */
74 d663a1fd Michael Niedermayer
75 891f64b3 joca@rixmail.se
#include "avcodec.h"
76
77 6636b7e8 Måns Rullgård
#ifdef CONFIG_AMR_NB_FIXED
78 bc634f6f Zdenek Kabelac
79
#define MMS_IO
80
81 891f64b3 joca@rixmail.se
#include "amr/sp_dec.h"
82
#include "amr/d_homing.h"
83
#include "amr/typedef.h"
84 bc634f6f Zdenek Kabelac
#include "amr/sp_enc.h"
85
#include "amr/sid_sync.h"
86
#include "amr/e_homing.h"
87 891f64b3 joca@rixmail.se
88 bc634f6f Zdenek Kabelac
#else
89 91024272 Stanislav Brabec
#include <amrnb/interf_dec.h>
90
#include <amrnb/interf_enc.h>
91 bc634f6f Zdenek Kabelac
#endif
92
93 414425e0 Víctor Paesa
static const char *nb_bitrate_unsupported =
94
    "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
95
static const char *wb_bitrate_unsupported =
96
    "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k\n";
97
98 bc634f6f Zdenek Kabelac
/* Common code for fixed and float version*/
99
typedef struct AMR_bitrates
100
{
101 db56acd4 Benoit Fouet
    int rate;
102 bc634f6f Zdenek Kabelac
    enum Mode mode;
103
} AMR_bitrates;
104
105 db56acd4 Benoit Fouet
/* Match desired bitrate */
106
static int getBitrateMode(int bitrate)
107 bc634f6f Zdenek Kabelac
{
108 db56acd4 Benoit Fouet
    /* make the correspondance between bitrate and mode */
109
    AMR_bitrates rates[]={ {4750,MR475},
110
                           {5150,MR515},
111
                           {5900,MR59},
112
                           {6700,MR67},
113
                           {7400,MR74},
114
                           {7950,MR795},
115
                           {10200,MR102},
116
                           {12200,MR122},
117 bc634f6f Zdenek Kabelac
                         };
118
    int i;
119 be6753c0 Benoit Fouet
120 d663a1fd Michael Niedermayer
    for(i=0;i<8;i++)
121 bc634f6f Zdenek Kabelac
    {
122 db56acd4 Benoit Fouet
        if(rates[i].rate==bitrate)
123 bc634f6f Zdenek Kabelac
        {
124
            return(rates[i].mode);
125
        }
126
    }
127 db56acd4 Benoit Fouet
    /* no bitrate matching, return an error */
128
    return -1;
129 bc634f6f Zdenek Kabelac
}
130
131 b500cc2a Andrew Voznytsa
static void amr_decode_fix_avctx(AVCodecContext * avctx)
132
{
133
    const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
134
135
    if(avctx->sample_rate == 0)
136
    {
137
        avctx->sample_rate = 8000 * is_amr_wb;
138
    }
139
140
    if(avctx->channels == 0)
141
    {
142
        avctx->channels = 1;
143
    }
144
145
    avctx->frame_size = 160 * is_amr_wb;
146
}
147
148 6636b7e8 Måns Rullgård
#ifdef CONFIG_AMR_NB_FIXED
149 bc634f6f Zdenek Kabelac
/* fixed point version*/
150 891f64b3 joca@rixmail.se
/* frame size in serial bitstream file (frame type + serial stream + flags) */
151
#define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
152
153 bc634f6f Zdenek Kabelac
typedef struct AMRContext {
154 891f64b3 joca@rixmail.se
    int frameCount;
155
    Speech_Decode_FrameState *speech_decoder_state;
156
    enum RXFrameType rx_type;
157
    enum Mode mode;
158
    Word16 reset_flag;
159
    Word16 reset_flag_old;
160
161 db56acd4 Benoit Fouet
    int enc_bitrate;
162 bc634f6f Zdenek Kabelac
    Speech_Encode_FrameState *enstate;
163
    sid_syncState *sidstate;
164
    enum TXFrameType tx_frametype;
165
} AMRContext;
166 891f64b3 joca@rixmail.se
167
static int amr_nb_decode_init(AVCodecContext * avctx)
168
{
169 bc634f6f Zdenek Kabelac
    AMRContext *s = avctx->priv_data;
170 be6753c0 Benoit Fouet
171 891f64b3 joca@rixmail.se
    s->frameCount=0;
172
    s->speech_decoder_state=NULL;
173
    s->rx_type = (enum RXFrameType)0;
174
    s->mode= (enum Mode)0;
175
    s->reset_flag=0;
176
    s->reset_flag_old=1;
177 115329f1 Diego Biurrun
178 891f64b3 joca@rixmail.se
    if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
179
    {
180 560ff809 Michael Niedermayer
        av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
181 891f64b3 joca@rixmail.se
        return -1;
182
    }
183 b500cc2a Andrew Voznytsa
184
    amr_decode_fix_avctx(avctx);
185
186
    if(avctx->channels > 1)
187
    {
188
        av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
189
        return -1;
190
    }
191
192 891f64b3 joca@rixmail.se
    return 0;
193
}
194
195 bc634f6f Zdenek Kabelac
static int amr_nb_encode_init(AVCodecContext * avctx)
196 891f64b3 joca@rixmail.se
{
197 bc634f6f Zdenek Kabelac
    AMRContext *s = avctx->priv_data;
198 be6753c0 Benoit Fouet
199 bc634f6f Zdenek Kabelac
    s->frameCount=0;
200
    s->speech_decoder_state=NULL;
201
    s->rx_type = (enum RXFrameType)0;
202
    s->mode= (enum Mode)0;
203
    s->reset_flag=0;
204
    s->reset_flag_old=1;
205 115329f1 Diego Biurrun
206 bc634f6f Zdenek Kabelac
    if(avctx->sample_rate!=8000)
207
    {
208 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
209 bc634f6f Zdenek Kabelac
        return -1;
210
    }
211
212
    if(avctx->channels!=1)
213
    {
214 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
215 bc634f6f Zdenek Kabelac
        return -1;
216
    }
217
218
    avctx->frame_size=160;
219
    avctx->coded_frame= avcodec_alloc_frame();
220
221
    if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
222
    {
223 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n");
224 bc634f6f Zdenek Kabelac
        return -1;
225
    }
226
227 db56acd4 Benoit Fouet
    if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
228
    {
229 414425e0 Víctor Paesa
        av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
230 db56acd4 Benoit Fouet
        return -1;
231
    }
232 bc634f6f Zdenek Kabelac
233
    return 0;
234
}
235
236
static int amr_nb_encode_close(AVCodecContext * avctx)
237
{
238
    AMRContext *s = avctx->priv_data;
239 be6753c0 Benoit Fouet
240 bc634f6f Zdenek Kabelac
    Speech_Encode_Frame_exit(&s->enstate);
241
    sid_sync_exit (&s->sidstate);
242
    av_freep(&avctx->coded_frame);
243
    return 0;
244
}
245
246
static int amr_nb_decode_close(AVCodecContext * avctx)
247
{
248
    AMRContext *s = avctx->priv_data;
249 be6753c0 Benoit Fouet
250 891f64b3 joca@rixmail.se
    Speech_Decode_Frame_exit(&s->speech_decoder_state);
251 bc634f6f Zdenek Kabelac
    return 0;
252 891f64b3 joca@rixmail.se
}
253
254
static int amr_nb_decode_frame(AVCodecContext * avctx,
255
            void *data, int *data_size,
256
            uint8_t * buf, int buf_size)
257
{
258 bc634f6f Zdenek Kabelac
    AMRContext *s = avctx->priv_data;
259 891f64b3 joca@rixmail.se
    uint8_t*amrData=buf;
260
    int offset=0;
261
    UWord8 toc, q, ft;
262
    Word16 serial[SERIAL_FRAMESIZE];   /* coded bits */
263
    Word16 *synth;
264
    UWord8 *packed_bits;
265
    static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
266
    int i;
267
268 bc634f6f Zdenek Kabelac
    //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
269 891f64b3 joca@rixmail.se
270
    synth=data;
271
272 e7b85a15 Diego Biurrun
    toc=amrData[offset];
273
    /* read rest of the frame based on ToC byte */
274
    q  = (toc >> 2) & 0x01;
275
    ft = (toc >> 3) & 0x0F;
276 891f64b3 joca@rixmail.se
277 e7b85a15 Diego Biurrun
    //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]);
278 bc634f6f Zdenek Kabelac
279 e7b85a15 Diego Biurrun
    offset++;
280 bc634f6f Zdenek Kabelac
281 e7b85a15 Diego Biurrun
    packed_bits=amrData+offset;
282 891f64b3 joca@rixmail.se
283 e7b85a15 Diego Biurrun
    offset+=packed_size[ft];
284 891f64b3 joca@rixmail.se
285 e7b85a15 Diego Biurrun
    //Unsort and unpack bits
286
    s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
287 891f64b3 joca@rixmail.se
288 e7b85a15 Diego Biurrun
    //We have a new frame
289
    s->frameCount++;
290 891f64b3 joca@rixmail.se
291 e7b85a15 Diego Biurrun
    if (s->rx_type == RX_NO_DATA)
292
    {
293
        s->mode = s->speech_decoder_state->prev_mode;
294
    }
295
    else {
296
        s->speech_decoder_state->prev_mode = s->mode;
297
    }
298 115329f1 Diego Biurrun
299 e7b85a15 Diego Biurrun
    /* if homed: check if this frame is another homing frame */
300
    if (s->reset_flag_old == 1)
301
    {
302
        /* only check until end of first subframe */
303
        s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
304
    }
305
    /* produce encoder homing frame if homed & input=decoder homing frame */
306
    if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
307
    {
308
        for (i = 0; i < L_FRAME; i++)
309 115329f1 Diego Biurrun
        {
310 e7b85a15 Diego Biurrun
            synth[i] = EHF_MASK;
311 891f64b3 joca@rixmail.se
        }
312 e7b85a15 Diego Biurrun
    }
313
    else
314
    {
315
        /* decode frame */
316
        Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
317
    }
318 891f64b3 joca@rixmail.se
319 e7b85a15 Diego Biurrun
    //Each AMR-frame results in 160 16-bit samples
320 5558bda8 Benoit Fouet
    *data_size=160*2;
321 115329f1 Diego Biurrun
322 e7b85a15 Diego Biurrun
    /* if not homed: check whether current frame is a homing frame */
323
    if (s->reset_flag_old == 0)
324
    {
325
        /* check whole frame */
326
        s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
327
    }
328
    /* reset decoder if current frame is a homing frame */
329
    if (s->reset_flag != 0)
330
    {
331
        Speech_Decode_Frame_reset(s->speech_decoder_state);
332
    }
333
    s->reset_flag_old = s->reset_flag;
334 115329f1 Diego Biurrun
335 bc634f6f Zdenek Kabelac
    return offset;
336
}
337
338
339
static int amr_nb_encode_frame(AVCodecContext *avctx,
340 bb270c08 Diego Biurrun
                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
341 bc634f6f Zdenek Kabelac
{
342
    short serial_data[250] = {0};
343
    AMRContext *s = avctx->priv_data;
344
    int written;
345 115329f1 Diego Biurrun
346 bc634f6f Zdenek Kabelac
    s->reset_flag = encoder_homing_frame_test(data);
347 115329f1 Diego Biurrun
348
    Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
349
350 bc634f6f Zdenek Kabelac
    /* add frame type and mode */
351
    sid_sync (s->sidstate, s->mode, &s->tx_frametype);
352 115329f1 Diego Biurrun
353 bc634f6f Zdenek Kabelac
    written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
354 115329f1 Diego Biurrun
355 bc634f6f Zdenek Kabelac
    if (s->reset_flag != 0)
356
    {
357
        Speech_Encode_Frame_reset(s->enstate);
358
        sid_sync_reset(s->sidstate);
359
    }
360
    return written;
361
}
362
363
364 6636b7e8 Måns Rullgård
#elif defined(CONFIG_AMR_NB) /* Float point version*/
365 bc634f6f Zdenek Kabelac
366
typedef struct AMRContext {
367
    int frameCount;
368
    void * decState;
369
    int *enstate;
370 db56acd4 Benoit Fouet
    int enc_bitrate;
371 bc634f6f Zdenek Kabelac
} AMRContext;
372
373
static int amr_nb_decode_init(AVCodecContext * avctx)
374
{
375
    AMRContext *s = avctx->priv_data;
376 be6753c0 Benoit Fouet
377 bc634f6f Zdenek Kabelac
    s->frameCount=0;
378
    s->decState=Decoder_Interface_init();
379
    if(!s->decState)
380
    {
381 560ff809 Michael Niedermayer
        av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
382 bc634f6f Zdenek Kabelac
        return -1;
383
    }
384 b500cc2a Andrew Voznytsa
385
    amr_decode_fix_avctx(avctx);
386
387
    if(avctx->channels > 1)
388
    {
389
        av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
390
        return -1;
391
    }
392
393 bc634f6f Zdenek Kabelac
    return 0;
394
}
395
396
static int amr_nb_encode_init(AVCodecContext * avctx)
397
{
398
    AMRContext *s = avctx->priv_data;
399 be6753c0 Benoit Fouet
400 bc634f6f Zdenek Kabelac
    s->frameCount=0;
401 115329f1 Diego Biurrun
402 bc634f6f Zdenek Kabelac
    if(avctx->sample_rate!=8000)
403
    {
404 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
405 bc634f6f Zdenek Kabelac
        return -1;
406
    }
407
408
    if(avctx->channels!=1)
409
    {
410 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
411 bc634f6f Zdenek Kabelac
        return -1;
412
    }
413
414
    avctx->frame_size=160;
415
    avctx->coded_frame= avcodec_alloc_frame();
416
417
    s->enstate=Encoder_Interface_init(0);
418
    if(!s->enstate)
419
    {
420 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
421 bc634f6f Zdenek Kabelac
        return -1;
422
    }
423
424 db56acd4 Benoit Fouet
    if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
425
    {
426 414425e0 Víctor Paesa
        av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
427 db56acd4 Benoit Fouet
        return -1;
428
    }
429 bc634f6f Zdenek Kabelac
430
    return 0;
431
}
432
433
static int amr_nb_decode_close(AVCodecContext * avctx)
434
{
435
    AMRContext *s = avctx->priv_data;
436 be6753c0 Benoit Fouet
437 bc634f6f Zdenek Kabelac
    Decoder_Interface_exit(s->decState);
438
    return 0;
439
}
440
441
static int amr_nb_encode_close(AVCodecContext * avctx)
442
{
443
    AMRContext *s = avctx->priv_data;
444 be6753c0 Benoit Fouet
445 bc634f6f Zdenek Kabelac
    Encoder_Interface_exit(s->enstate);
446
    av_freep(&avctx->coded_frame);
447
    return 0;
448
}
449
450
static int amr_nb_decode_frame(AVCodecContext * avctx,
451
            void *data, int *data_size,
452
            uint8_t * buf, int buf_size)
453
{
454 c4f452fd Benoit Fouet
    AMRContext *s = avctx->priv_data;
455 bc634f6f Zdenek Kabelac
    uint8_t*amrData=buf;
456
    static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
457
    enum Mode dec_mode;
458
    int packet_size;
459
460 a319bbac Richard van der Hoff
    /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */
461 115329f1 Diego Biurrun
462 a319bbac Richard van der Hoff
    dec_mode = (buf[0] >> 3) & 0x000F;
463
    packet_size = block_size[dec_mode]+1;
464
465
    if(packet_size > buf_size) {
466
        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
467
        return -1;
468
    }
469 115329f1 Diego Biurrun
470 a319bbac Richard van der Hoff
    s->frameCount++;
471
    /* 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]); */
472
    /* call decoder */
473
    Decoder_Interface_Decode(s->decState, amrData, data, 0);
474
    *data_size=160*2;
475 115329f1 Diego Biurrun
476 a319bbac Richard van der Hoff
    return packet_size;
477 891f64b3 joca@rixmail.se
}
478
479 bc634f6f Zdenek Kabelac
static int amr_nb_encode_frame(AVCodecContext *avctx,
480 bb270c08 Diego Biurrun
                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
481 bc634f6f Zdenek Kabelac
{
482 c4f452fd Benoit Fouet
    AMRContext *s = avctx->priv_data;
483 bc634f6f Zdenek Kabelac
    int written;
484
485 db56acd4 Benoit Fouet
    if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0)
486
    {
487 414425e0 Víctor Paesa
        av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
488 db56acd4 Benoit Fouet
        return -1;
489
    }
490 4ec0ccb1 Martin
491 115329f1 Diego Biurrun
    written = Encoder_Interface_Encode(s->enstate,
492
        s->enc_bitrate,
493
        data,
494
        frame,
495 bc634f6f Zdenek Kabelac
        0);
496 1ba02582 Calcium
    /* 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] ); */
497 bc634f6f Zdenek Kabelac
498
    return written;
499
}
500
501
#endif
502
503 6636b7e8 Måns Rullgård
#if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
504 399d8401 Michel Bardiaux
505 891f64b3 joca@rixmail.se
AVCodec amr_nb_decoder =
506
{
507
    "amr_nb",
508
    CODEC_TYPE_AUDIO,
509
    CODEC_ID_AMR_NB,
510 bc634f6f Zdenek Kabelac
    sizeof(AMRContext),
511 891f64b3 joca@rixmail.se
    amr_nb_decode_init,
512
    NULL,
513 bc634f6f Zdenek Kabelac
    amr_nb_decode_close,
514 891f64b3 joca@rixmail.se
    amr_nb_decode_frame,
515
};
516
517 bc634f6f Zdenek Kabelac
AVCodec amr_nb_encoder =
518
{
519
    "amr_nb",
520
    CODEC_TYPE_AUDIO,
521
    CODEC_ID_AMR_NB,
522
    sizeof(AMRContext),
523
    amr_nb_encode_init,
524
    amr_nb_encode_frame,
525
    amr_nb_encode_close,
526
    NULL,
527
};
528 d663a1fd Michael Niedermayer
529 399d8401 Michel Bardiaux
#endif
530
531 d663a1fd Michael Niedermayer
/* -----------AMR wideband ------------*/
532 6636b7e8 Måns Rullgård
#ifdef CONFIG_AMR_WB
533 d663a1fd Michael Niedermayer
534
#ifdef _TYPEDEF_H
535
//To avoid duplicate typedefs from typdef in amr-nb
536
#define typedef_h
537
#endif
538
539 91024272 Stanislav Brabec
#include <amrwb/enc_if.h>
540
#include <amrwb/dec_if.h>
541
#include <amrwb/if_rom.h>
542 d663a1fd Michael Niedermayer
543
/* Common code for fixed and float version*/
544
typedef struct AMRWB_bitrates
545
{
546 db56acd4 Benoit Fouet
    int rate;
547 d663a1fd Michael Niedermayer
    int mode;
548
} AMRWB_bitrates;
549
550
static int getWBBitrateMode(int bitrate)
551
{
552 db56acd4 Benoit Fouet
    /* make the correspondance between bitrate and mode */
553
    AMRWB_bitrates rates[]={ {6600,0},
554
                           {8850,1},
555
                           {12650,2},
556
                           {14250,3},
557
                           {15850,4},
558
                           {18250,5},
559
                           {19850,6},
560
                           {23050,7},
561
                           {23850,8},
562 d663a1fd Michael Niedermayer
                         };
563
    int i;
564
565
    for(i=0;i<9;i++)
566
    {
567 db56acd4 Benoit Fouet
        if(rates[i].rate==bitrate)
568 d663a1fd Michael Niedermayer
        {
569
            return(rates[i].mode);
570
        }
571
    }
572 db56acd4 Benoit Fouet
    /* no bitrate matching, return an error */
573
    return -1;
574 d663a1fd Michael Niedermayer
}
575
576
577
typedef struct AMRWBContext {
578
    int frameCount;
579
    void *state;
580
    int mode;
581
    Word16 allow_dtx;
582
} AMRWBContext;
583
584
static int amr_wb_encode_init(AVCodecContext * avctx)
585
{
586 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
587 be6753c0 Benoit Fouet
588 d663a1fd Michael Niedermayer
    s->frameCount=0;
589 115329f1 Diego Biurrun
590 d663a1fd Michael Niedermayer
    if(avctx->sample_rate!=16000)
591
    {
592 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
593 d663a1fd Michael Niedermayer
        return -1;
594
    }
595
596
    if(avctx->channels!=1)
597
    {
598 7910d134 Baptiste Coudurier
        av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
599 d663a1fd Michael Niedermayer
        return -1;
600
    }
601
602 db56acd4 Benoit Fouet
    if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
603
    {
604 414425e0 Víctor Paesa
        av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
605 db56acd4 Benoit Fouet
        return -1;
606
    }
607
608 d663a1fd Michael Niedermayer
    avctx->frame_size=320;
609
    avctx->coded_frame= avcodec_alloc_frame();
610
611
    s->state = E_IF_init();
612
    s->allow_dtx=0;
613
614
    return 0;
615
}
616
617
static int amr_wb_encode_close(AVCodecContext * avctx)
618
{
619 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
620 be6753c0 Benoit Fouet
621 d663a1fd Michael Niedermayer
    E_IF_exit(s->state);
622
    av_freep(&avctx->coded_frame);
623
    s->frameCount++;
624
    return 0;
625
}
626
627
static int amr_wb_encode_frame(AVCodecContext *avctx,
628 bb270c08 Diego Biurrun
                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
629 d663a1fd Michael Niedermayer
{
630 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
631 4ec0ccb1 Martin
    int size;
632 e7a5854d Benoit Fouet
633 db56acd4 Benoit Fouet
    if((s->mode=getWBBitrateMode(avctx->bit_rate))<0)
634
    {
635 414425e0 Víctor Paesa
        av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
636 db56acd4 Benoit Fouet
        return -1;
637
    }
638 4ec0ccb1 Martin
    size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
639 d663a1fd Michael Niedermayer
    return size;
640
}
641
642
static int amr_wb_decode_init(AVCodecContext * avctx)
643
{
644 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
645 be6753c0 Benoit Fouet
646 d663a1fd Michael Niedermayer
    s->frameCount=0;
647
    s->state = D_IF_init();
648 b500cc2a Andrew Voznytsa
649
    amr_decode_fix_avctx(avctx);
650
651
    if(avctx->channels > 1)
652
    {
653
        av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
654
        return -1;
655
    }
656
657 d663a1fd Michael Niedermayer
    return 0;
658
}
659
660
static int amr_wb_decode_frame(AVCodecContext * avctx,
661
            void *data, int *data_size,
662
            uint8_t * buf, int buf_size)
663
{
664 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
665 d663a1fd Michael Niedermayer
    uint8_t*amrData=buf;
666
    int mode;
667
    int packet_size;
668
669 a319bbac Richard van der Hoff
    if(buf_size==0) {
670
        /* nothing to do */
671
        return 0;
672
    }
673
674
    mode = (amrData[0] >> 3) & 0x000F;
675
    packet_size = block_size[mode];
676
677
    if(packet_size > buf_size) {
678
        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
679
        return -1;
680 d663a1fd Michael Niedermayer
    }
681 115329f1 Diego Biurrun
682 a319bbac Richard van der Hoff
    s->frameCount++;
683
    D_IF_decode( s->state, amrData, data, _good_frame);
684
    *data_size=320*2;
685
    return packet_size;
686 d663a1fd Michael Niedermayer
}
687
688
static int amr_wb_decode_close(AVCodecContext * avctx)
689
{
690 c4f452fd Benoit Fouet
    AMRWBContext *s = avctx->priv_data;
691 be6753c0 Benoit Fouet
692 d663a1fd Michael Niedermayer
    D_IF_exit(s->state);
693
    return 0;
694
}
695
696
AVCodec amr_wb_decoder =
697
{
698
    "amr_wb",
699
    CODEC_TYPE_AUDIO,
700
    CODEC_ID_AMR_WB,
701
    sizeof(AMRWBContext),
702
    amr_wb_decode_init,
703
    NULL,
704
    amr_wb_decode_close,
705
    amr_wb_decode_frame,
706
};
707
708
AVCodec amr_wb_encoder =
709
{
710
    "amr_wb",
711
    CODEC_TYPE_AUDIO,
712
    CODEC_ID_AMR_WB,
713
    sizeof(AMRWBContext),
714
    amr_wb_encode_init,
715
    amr_wb_encode_frame,
716
    amr_wb_encode_close,
717
    NULL,
718
};
719
720 6636b7e8 Måns Rullgård
#endif //CONFIG_AMR_WB