annotate amr.c @ 1417:89dacc2b9bf0 libavcodec

avoid warning
author bellard
date Sun, 24 Aug 2003 22:15:37 +0000
parents 1db67cd7808a
children f22010affdce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
1 /*
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
2 * AMR Audio decoder stub
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
3 * Copyright (c) 2003 the ffmpeg project
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
4 *
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
9 *
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
14 *
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
18 */
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
19 /*
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
20 This code implements amr-nb audio encoder/decoder through external reference
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
21 code from www.3gpp.org. The licence of the code from 3gpp is unclear so you
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
22 have to download the code separately. Two versions exists: One fixed-point
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
23 and one with floats. For some reason the float-encoder is significant faster
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
24 atleast on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102).
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
25
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
26 The fixed-point (TS26.073) can be downloaded from:
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
27 http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26073-510.zip
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
28 Extract the soure into ffmpeg/libavcodec/amr
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
29 To use the float version run "./configure" with "--enable-amr-nb-fixed"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
30
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
31 The float version (default) can be downloaded from:
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
32 http://www.3gpp.org/ftp/Specs/latest/Rel-5/26_series/26104-510.zip
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
33 Extract the soure into ffmpeg/libavcodec/amr_float
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
34
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
35 The specification for amr-nb can be found in TS 26.071
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
36 (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
37 info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
38
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
39 In the future support for AMR-WB might also be included here.
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
40 Reference code exist in TS26.173 and TS 26.204.
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
41
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
42 */
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
43 #define DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
44 //#define AMR_NB_FIXED
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
45 #include "../config.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
46 #include "avcodec.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
47
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
48 #ifdef AMR_NB_FIXED
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
49
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
50 #define MMS_IO
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
51
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
52 #include "amr/sp_dec.h"
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
53 #include "amr/d_homing.h"
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
54 #include "amr/typedef.h"
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
55 #include "amr/sp_enc.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
56 #include "amr/sid_sync.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
57 #include "amr/e_homing.h"
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
58
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
59 #else
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
60 #include "amr_float/interf_dec.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
61 #include "amr_float/interf_enc.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
62 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
63
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
64 /* Common code for fixed and float version*/
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
65 typedef struct AMR_bitrates
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
66 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
67 int startrate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
68 int stoprate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
69 enum Mode mode;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
70
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
71 } AMR_bitrates;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
72
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
73 /* Match desired bitrate with closest one*/
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
74 static enum Mode getBitrateMode(int bitrate)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
75 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
76 /* Adjusted so that all bitrates can be used from commandline where
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
77 only a multiple of 1000 can be specified*/
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
78 AMR_bitrates rates[]={ {0,4999,MR475}, //4
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
79 {5000,5899,MR515},//5
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
80 {5900,6699,MR59},//6
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
81 {6700,7000,MR67},//7
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
82 {7001,7949,MR74},//8
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
83 {7950,9999,MR795},//9
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
84 {10000,11999,MR102},//10
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
85 {12000,64000,MR122},//12
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
86
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
87 };
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
88 int i;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
89 for(i=0;i<sizeof(rates);i++)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
90 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
91 if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
92 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
93 return(rates[i].mode);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
94 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
95 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
96 /*Return highest possible*/
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
97 return(MR122);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
98 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
99
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
100 #ifdef AMR_NB_FIXED
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
101 /* fixed point version*/
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
102 /* frame size in serial bitstream file (frame type + serial stream + flags) */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
103 #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
104
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
105 typedef struct AMRContext {
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
106 int frameCount;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
107 Speech_Decode_FrameState *speech_decoder_state;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
108 enum RXFrameType rx_type;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
109 enum Mode mode;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
110 Word16 reset_flag;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
111 Word16 reset_flag_old;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
112
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
113 enum Mode enc_bitrate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
114 Speech_Encode_FrameState *enstate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
115 sid_syncState *sidstate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
116 enum TXFrameType tx_frametype;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
117
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
118
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
119 } AMRContext;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
120
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
121 static int amr_nb_decode_init(AVCodecContext * avctx)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
122 {
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
123 AMRContext *s = avctx->priv_data;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
124 s->frameCount=0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
125 s->speech_decoder_state=NULL;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
126 s->rx_type = (enum RXFrameType)0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
127 s->mode= (enum Mode)0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
128 s->reset_flag=0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
129 s->reset_flag_old=1;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
130
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
131 if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
132 {
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
133 printf("Speech_Decode_Frame_init error\n");
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
134 return -1;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
135 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
136 return 0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
137 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
138
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
139 static int amr_nb_encode_init(AVCodecContext * avctx)
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
140 {
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
141 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
142 s->frameCount=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
143 s->speech_decoder_state=NULL;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
144 s->rx_type = (enum RXFrameType)0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
145 s->mode= (enum Mode)0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
146 s->reset_flag=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
147 s->reset_flag_old=1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
148
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
149 if(avctx->sample_rate!=8000)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
150 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
151 #ifdef DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
152 printf("Only 8000Hz sample rate supported\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
153 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
154 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
155 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
156
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
157 if(avctx->channels!=1)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
158 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
159 #ifdef DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
160 printf("Only mono supported\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
161 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
162 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
163 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
164
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
165 avctx->frame_size=160;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
166 avctx->coded_frame= avcodec_alloc_frame();
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
167
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
168 if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
169 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
170 #ifdef DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
171 printf("Speech_Encode_Frame_init error\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
172 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
173 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
174 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
175
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
176 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
177
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
178 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
179 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
180
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
181 static int amr_nb_encode_close(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
182 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
183 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
184 Speech_Encode_Frame_exit(&s->enstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
185 sid_sync_exit (&s->sidstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
186 av_freep(&avctx->coded_frame);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
187 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
188 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
189
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
190 static int amr_nb_decode_close(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
191 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
192 AMRContext *s = avctx->priv_data;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
193 Speech_Decode_Frame_exit(&s->speech_decoder_state);
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
194 return 0;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
195 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
196
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
197 static int amr_nb_decode_frame(AVCodecContext * avctx,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
198 void *data, int *data_size,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
199 uint8_t * buf, int buf_size)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
200 {
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
201 AMRContext *s = avctx->priv_data;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
202
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
203 uint8_t*amrData=buf;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
204 int offset=0;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
205
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
206 UWord8 toc, q, ft;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
207
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
208 Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
209 Word16 *synth;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
210 UWord8 *packed_bits;
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
211 *data_size=0;
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
212
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
213 static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
214 int i;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
215
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
216 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
217
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
218 synth=data;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
219
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
220 // while(offset<buf_size)
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
221 {
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
222 toc=amrData[offset];
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
223 /* read rest of the frame based on ToC byte */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
224 q = (toc >> 2) & 0x01;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
225 ft = (toc >> 3) & 0x0F;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
226
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
227 //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]);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
228
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
229 offset++;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
230
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
231 packed_bits=amrData+offset;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
232
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
233 offset+=packed_size[ft];
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
234
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
235 //Unsort and unpack bits
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
236 s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
237
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
238 //We have a new frame
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
239 s->frameCount++;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
240
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
241 if (s->rx_type == RX_NO_DATA)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
242 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
243 s->mode = s->speech_decoder_state->prev_mode;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
244 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
245 else {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
246 s->speech_decoder_state->prev_mode = s->mode;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
247 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
248
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
249 /* if homed: check if this frame is another homing frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
250 if (s->reset_flag_old == 1)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
251 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
252 /* only check until end of first subframe */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
253 s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
254 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
255 /* produce encoder homing frame if homed & input=decoder homing frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
256 if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
257 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
258 for (i = 0; i < L_FRAME; i++)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
259 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
260 synth[i] = EHF_MASK;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
261 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
262 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
263 else
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
264 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
265 /* decode frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
266 Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
267 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
268
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
269 //Each AMR-frame results in 160 16-bit samples
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
270 *data_size+=160*2;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
271 synth+=160;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
272
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
273 /* if not homed: check whether current frame is a homing frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
274 if (s->reset_flag_old == 0)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
275 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
276 /* check whole frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
277 s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
278 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
279 /* reset decoder if current frame is a homing frame */
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
280 if (s->reset_flag != 0)
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
281 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
282 Speech_Decode_Frame_reset(s->speech_decoder_state);
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
283 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
284 s->reset_flag_old = s->reset_flag;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
285
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
286 }
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
287 return offset;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
288 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
289
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
290
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
291 static int amr_nb_encode_frame(AVCodecContext *avctx,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
292 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
293 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
294 short serial_data[250] = {0};
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
295
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
296 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
297 int written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
298
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
299 s->reset_flag = encoder_homing_frame_test(data);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
300
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
301 Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
302
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
303 /* add frame type and mode */
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
304 sid_sync (s->sidstate, s->mode, &s->tx_frametype);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
305
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
306 written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
307
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
308 if (s->reset_flag != 0)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
309 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
310 Speech_Encode_Frame_reset(s->enstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
311 sid_sync_reset(s->sidstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
312 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
313 return written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
314 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
315
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
316
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
317 #else /* Float point version*/
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
318
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
319 typedef struct AMRContext {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
320 int frameCount;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
321 void * decState;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
322 int *enstate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
323 enum Mode enc_bitrate;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
324 } AMRContext;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
325
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
326 static int amr_nb_decode_init(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
327 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
328 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
329 s->frameCount=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
330 s->decState=Decoder_Interface_init();
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
331 if(!s->decState)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
332 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
333 printf("Decoder_Interface_init error\r\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
334 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
335 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
336 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
337 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
338
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
339 static int amr_nb_encode_init(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
340 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
341 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
342 s->frameCount=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
343
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
344 if(avctx->sample_rate!=8000)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
345 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
346 #ifdef DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
347 printf("Only 8000Hz sample rate supported\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
348 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
349 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
350 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
351
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
352 if(avctx->channels!=1)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
353 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
354 #ifdef DEBUG
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
355 printf("Only mono supported\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
356 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
357 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
358 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
359
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
360 avctx->frame_size=160;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
361 avctx->coded_frame= avcodec_alloc_frame();
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
362
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
363 s->enstate=Encoder_Interface_init(0);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
364 if(!s->enstate)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
365 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
366 printf("Encoder_Interface_init error\n");
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
367 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
368 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
369
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
370 s->enc_bitrate=getBitrateMode(avctx->bit_rate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
371
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
372 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
373 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
374
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
375 static int amr_nb_decode_close(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
376 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
377 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
378 Decoder_Interface_exit(s->decState);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
379 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
380 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
381
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
382 static int amr_nb_encode_close(AVCodecContext * avctx)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
383 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
384 AMRContext *s = avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
385 Encoder_Interface_exit(s->enstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
386 av_freep(&avctx->coded_frame);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
387 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
388 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
389
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
390 static int amr_nb_decode_frame(AVCodecContext * avctx,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
391 void *data, int *data_size,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
392 uint8_t * buf, int buf_size)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
393 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
394 AMRContext *s = (AMRContext*)avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
395
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
396 uint8_t*amrData=buf;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
397 int offset=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
398 static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
399 enum Mode dec_mode;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
400 int packet_size;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
401 *data_size=0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
402
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
403 //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
404
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
405 while(offset<buf_size)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
406 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
407 dec_mode = (amrData[offset] >> 3) & 0x000F;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
408 packet_size = block_size[dec_mode];
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
409
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
410 s->frameCount++;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
411 //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]);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
412 /* call decoder */
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
413 Decoder_Interface_Decode(s->decState, &amrData[offset], data+*data_size, 0);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
414 *data_size+=160*2;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
415
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
416 offset+=packet_size+1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
417 }
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
418 return buf_size;
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
419 }
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
420
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
421 static int amr_nb_encode_frame(AVCodecContext *avctx,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
422 unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
423 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
424 AMRContext *s = (AMRContext*)avctx->priv_data;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
425 int written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
426
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
427 written = Encoder_Interface_Encode(s->enstate,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
428 s->enc_bitrate,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
429 data,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
430 frame,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
431 0);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
432
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
433 return written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
434 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
435
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
436 #endif
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
437
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
438 AVCodec amr_nb_decoder =
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
439 {
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
440 "amr_nb",
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
441 CODEC_TYPE_AUDIO,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
442 CODEC_ID_AMR_NB,
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
443 sizeof(AMRContext),
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
444 amr_nb_decode_init,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
445 NULL,
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
446 amr_nb_decode_close,
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
447 amr_nb_decode_frame,
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
448 };
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
449
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
450 AVCodec amr_nb_encoder =
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
451 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
452 "amr_nb",
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
453 CODEC_TYPE_AUDIO,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
454 CODEC_ID_AMR_NB,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
455 sizeof(AMRContext),
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
456 amr_nb_encode_init,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
457 amr_nb_encode_frame,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
458 amr_nb_encode_close,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
459 NULL,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
460 };