Libav
|
00001 /* 00002 * AMR Audio decoder stub 00003 * Copyright (c) 2003 the ffmpeg project 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #include "avcodec.h" 00023 00024 static void amr_decode_fix_avctx(AVCodecContext *avctx) 00025 { 00026 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB); 00027 00028 if (!avctx->sample_rate) 00029 avctx->sample_rate = 8000 * is_amr_wb; 00030 00031 if (!avctx->channels) 00032 avctx->channels = 1; 00033 00034 avctx->frame_size = 160 * is_amr_wb; 00035 avctx->sample_fmt = SAMPLE_FMT_S16; 00036 } 00037 00038 #if CONFIG_LIBOPENCORE_AMRNB 00039 00040 #include <opencore-amrnb/interf_dec.h> 00041 #include <opencore-amrnb/interf_enc.h> 00042 00043 static const char nb_bitrate_unsupported[] = 00044 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n"; 00045 00046 /* Common code for fixed and float version*/ 00047 typedef struct AMR_bitrates { 00048 int rate; 00049 enum Mode mode; 00050 } AMR_bitrates; 00051 00052 /* Match desired bitrate */ 00053 static int getBitrateMode(int bitrate) 00054 { 00055 /* make the correspondance between bitrate and mode */ 00056 AMR_bitrates rates[] = { { 4750, MR475}, 00057 { 5150, MR515}, 00058 { 5900, MR59}, 00059 { 6700, MR67}, 00060 { 7400, MR74}, 00061 { 7950, MR795}, 00062 {10200, MR102}, 00063 {12200, MR122}, }; 00064 int i; 00065 00066 for (i = 0; i < 8; i++) 00067 if (rates[i].rate == bitrate) 00068 return rates[i].mode; 00069 /* no bitrate matching, return an error */ 00070 return -1; 00071 } 00072 00073 typedef struct AMRContext { 00074 int frameCount; 00075 void *decState; 00076 int *enstate; 00077 int enc_bitrate; 00078 } AMRContext; 00079 00080 static av_cold int amr_nb_decode_init(AVCodecContext *avctx) 00081 { 00082 AMRContext *s = avctx->priv_data; 00083 00084 s->frameCount = 0; 00085 s->decState = Decoder_Interface_init(); 00086 if (!s->decState) { 00087 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n"); 00088 return -1; 00089 } 00090 00091 amr_decode_fix_avctx(avctx); 00092 00093 if (avctx->channels > 1) { 00094 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); 00095 return -1; 00096 } 00097 00098 return 0; 00099 } 00100 00101 static av_cold int amr_nb_decode_close(AVCodecContext *avctx) 00102 { 00103 AMRContext *s = avctx->priv_data; 00104 00105 Decoder_Interface_exit(s->decState); 00106 return 0; 00107 } 00108 00109 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data, 00110 int *data_size, AVPacket *avpkt) 00111 { 00112 const uint8_t *buf = avpkt->data; 00113 int buf_size = avpkt->size; 00114 AMRContext *s = avctx->priv_data; 00115 const uint8_t *amrData = buf; 00116 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; 00117 enum Mode dec_mode; 00118 int packet_size; 00119 00120 /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n", 00121 buf, buf_size, s->frameCount); */ 00122 00123 dec_mode = (buf[0] >> 3) & 0x000F; 00124 packet_size = block_size[dec_mode] + 1; 00125 00126 if (packet_size > buf_size) { 00127 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", 00128 buf_size, packet_size); 00129 return -1; 00130 } 00131 00132 s->frameCount++; 00133 /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n", 00134 packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */ 00135 /* call decoder */ 00136 Decoder_Interface_Decode(s->decState, amrData, data, 0); 00137 *data_size = 160 * 2; 00138 00139 return packet_size; 00140 } 00141 00142 AVCodec libopencore_amrnb_decoder = { 00143 "libopencore_amrnb", 00144 AVMEDIA_TYPE_AUDIO, 00145 CODEC_ID_AMR_NB, 00146 sizeof(AMRContext), 00147 amr_nb_decode_init, 00148 NULL, 00149 amr_nb_decode_close, 00150 amr_nb_decode_frame, 00151 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"), 00152 }; 00153 00154 static av_cold int amr_nb_encode_init(AVCodecContext *avctx) 00155 { 00156 AMRContext *s = avctx->priv_data; 00157 00158 s->frameCount = 0; 00159 00160 if (avctx->sample_rate != 8000) { 00161 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); 00162 return -1; 00163 } 00164 00165 if (avctx->channels != 1) { 00166 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); 00167 return -1; 00168 } 00169 00170 avctx->frame_size = 160; 00171 avctx->coded_frame = avcodec_alloc_frame(); 00172 00173 s->enstate=Encoder_Interface_init(0); 00174 if (!s->enstate) { 00175 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n"); 00176 return -1; 00177 } 00178 00179 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) { 00180 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); 00181 return -1; 00182 } 00183 00184 return 0; 00185 } 00186 00187 static av_cold int amr_nb_encode_close(AVCodecContext *avctx) 00188 { 00189 AMRContext *s = avctx->priv_data; 00190 00191 Encoder_Interface_exit(s->enstate); 00192 av_freep(&avctx->coded_frame); 00193 return 0; 00194 } 00195 00196 static int amr_nb_encode_frame(AVCodecContext *avctx, 00197 unsigned char *frame/*out*/, 00198 int buf_size, void *data/*in*/) 00199 { 00200 AMRContext *s = avctx->priv_data; 00201 int written; 00202 00203 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) { 00204 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); 00205 return -1; 00206 } 00207 00208 written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data, 00209 frame, 0); 00210 /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n", 00211 written, s->enc_bitrate, frame[0] ); */ 00212 00213 return written; 00214 } 00215 00216 AVCodec libopencore_amrnb_encoder = { 00217 "libopencore_amrnb", 00218 AVMEDIA_TYPE_AUDIO, 00219 CODEC_ID_AMR_NB, 00220 sizeof(AMRContext), 00221 amr_nb_encode_init, 00222 amr_nb_encode_frame, 00223 amr_nb_encode_close, 00224 NULL, 00225 .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, 00226 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"), 00227 }; 00228 00229 #endif 00230 00231 /* -----------AMR wideband ------------*/ 00232 #if CONFIG_LIBOPENCORE_AMRWB 00233 00234 #ifdef _TYPEDEF_H 00235 //To avoid duplicate typedefs from typedef in amr-nb 00236 #define typedef_h 00237 #endif 00238 00239 #include <opencore-amrwb/dec_if.h> 00240 #include <opencore-amrwb/if_rom.h> 00241 00242 static const char wb_bitrate_unsupported[] = 00243 "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"; 00244 00245 /* Common code for fixed and float version*/ 00246 typedef struct AMRWB_bitrates { 00247 int rate; 00248 int mode; 00249 } AMRWB_bitrates; 00250 00251 typedef struct AMRWBContext { 00252 int frameCount; 00253 void *state; 00254 int mode; 00255 Word16 allow_dtx; 00256 } AMRWBContext; 00257 00258 static av_cold int amr_wb_decode_init(AVCodecContext *avctx) 00259 { 00260 AMRWBContext *s = avctx->priv_data; 00261 00262 s->frameCount = 0; 00263 s->state = D_IF_init(); 00264 00265 amr_decode_fix_avctx(avctx); 00266 00267 if (avctx->channels > 1) { 00268 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n"); 00269 return -1; 00270 } 00271 00272 return 0; 00273 } 00274 00275 static int amr_wb_decode_frame(AVCodecContext *avctx, void *data, 00276 int *data_size, AVPacket *avpkt) 00277 { 00278 const uint8_t *buf = avpkt->data; 00279 int buf_size = avpkt->size; 00280 AMRWBContext *s = avctx->priv_data; 00281 const uint8_t *amrData = buf; 00282 int mode; 00283 int packet_size; 00284 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1}; 00285 00286 if (!buf_size) 00287 /* nothing to do */ 00288 return 0; 00289 00290 mode = (amrData[0] >> 3) & 0x000F; 00291 packet_size = block_size[mode]; 00292 00293 if (packet_size > buf_size) { 00294 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", 00295 buf_size, packet_size + 1); 00296 return -1; 00297 } 00298 00299 s->frameCount++; 00300 D_IF_decode(s->state, amrData, data, _good_frame); 00301 *data_size = 320 * 2; 00302 return packet_size; 00303 } 00304 00305 static int amr_wb_decode_close(AVCodecContext *avctx) 00306 { 00307 AMRWBContext *s = avctx->priv_data; 00308 00309 D_IF_exit(s->state); 00310 return 0; 00311 } 00312 00313 AVCodec libopencore_amrwb_decoder = { 00314 "libopencore_amrwb", 00315 AVMEDIA_TYPE_AUDIO, 00316 CODEC_ID_AMR_WB, 00317 sizeof(AMRWBContext), 00318 amr_wb_decode_init, 00319 NULL, 00320 amr_wb_decode_close, 00321 amr_wb_decode_frame, 00322 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"), 00323 }; 00324 00325 #endif /* CONFIG_LIBOPENCORE_AMRWB */