libavcodec/libopencore-amr.c

Go to the documentation of this file.
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,
00111 const uint8_t *buf, int buf_size)
00112 {
00113 AMRContext *s = avctx->priv_data;
00114 const uint8_t *amrData = buf;
00115 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
00116 enum Mode dec_mode;
00117 int packet_size;
00118 
00119 /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
00120  buf, buf_size, s->frameCount); */
00121 
00122 dec_mode = (buf[0] >> 3) & 0x000F;
00123 packet_size = block_size[dec_mode] + 1;
00124 
00125 if (packet_size > buf_size) {
00126 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00127 buf_size, packet_size);
00128 return -1;
00129 }
00130 
00131 s->frameCount++;
00132 /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
00133  packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
00134 /* call decoder */
00135 Decoder_Interface_Decode(s->decState, amrData, data, 0);
00136 *data_size = 160 * 2;
00137 
00138 return packet_size;
00139 }
00140 
00141 AVCodec libopencore_amrnb_decoder = {
00142 "libopencore_amrnb",
00143 CODEC_TYPE_AUDIO,
00144 CODEC_ID_AMR_NB,
00145 sizeof(AMRContext),
00146 amr_nb_decode_init,
00147 NULL,
00148 amr_nb_decode_close,
00149 amr_nb_decode_frame,
00150 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
00151 };
00152 
00153 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
00154 {
00155 AMRContext *s = avctx->priv_data;
00156 
00157 s->frameCount = 0;
00158 
00159 if (avctx->sample_rate != 8000) {
00160 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
00161 return -1;
00162 }
00163 
00164 if (avctx->channels != 1) {
00165 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
00166 return -1;
00167 }
00168 
00169 avctx->frame_size = 160;
00170 avctx->coded_frame = avcodec_alloc_frame();
00171 
00172 s->enstate=Encoder_Interface_init(0);
00173 if (!s->enstate) {
00174 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
00175 return -1;
00176 }
00177 
00178 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
00179 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00180 return -1;
00181 }
00182 
00183 return 0;
00184 }
00185 
00186 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
00187 {
00188 AMRContext *s = avctx->priv_data;
00189 
00190 Encoder_Interface_exit(s->enstate);
00191 av_freep(&avctx->coded_frame);
00192 return 0;
00193 }
00194 
00195 static int amr_nb_encode_frame(AVCodecContext *avctx,
00196 unsigned char *frame/*out*/,
00197 int buf_size, void *data/*in*/)
00198 {
00199 AMRContext *s = avctx->priv_data;
00200 int written;
00201 
00202 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
00203 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
00204 return -1;
00205 }
00206 
00207 written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
00208 frame, 0);
00209 /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
00210  written, s->enc_bitrate, frame[0] ); */
00211 
00212 return written;
00213 }
00214 
00215 AVCodec libopencore_amrnb_encoder = {
00216 "libopencore_amrnb",
00217 CODEC_TYPE_AUDIO,
00218 CODEC_ID_AMR_NB,
00219 sizeof(AMRContext),
00220 amr_nb_encode_init,
00221 amr_nb_encode_frame,
00222 amr_nb_encode_close,
00223 NULL,
00224 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
00225 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Narrow-Band"),
00226 };
00227 
00228 #endif
00229 
00230 /* -----------AMR wideband ------------*/
00231 #if CONFIG_LIBOPENCORE_AMRWB
00232 
00233 #ifdef _TYPEDEF_H
00234 //To avoid duplicate typedefs from typedef in amr-nb
00235 #define typedef_h
00236 #endif
00237 
00238 #include <opencore-amrwb/dec_if.h>
00239 #include <opencore-amrwb/if_rom.h>
00240 
00241 static const char wb_bitrate_unsupported[] =
00242 "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";
00243 
00244 /* Common code for fixed and float version*/
00245 typedef struct AMRWB_bitrates {
00246 int rate;
00247 int mode;
00248 } AMRWB_bitrates;
00249 
00250 typedef struct AMRWBContext {
00251 int frameCount;
00252 void *state;
00253 int mode;
00254 Word16 allow_dtx;
00255 } AMRWBContext;
00256 
00257 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
00258 {
00259 AMRWBContext *s = avctx->priv_data;
00260 
00261 s->frameCount = 0;
00262 s->state = D_IF_init();
00263 
00264 amr_decode_fix_avctx(avctx);
00265 
00266 if (avctx->channels > 1) {
00267 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
00268 return -1;
00269 }
00270 
00271 return 0;
00272 }
00273 
00274 static int amr_wb_decode_frame(AVCodecContext *avctx,
00275 void *data, int *data_size,
00276 const uint8_t *buf, int buf_size)
00277 {
00278 AMRWBContext *s = avctx->priv_data;
00279 const uint8_t *amrData = buf;
00280 int mode;
00281 int packet_size;
00282 static const uint8_t block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
00283 
00284 if (!buf_size)
00285 /* nothing to do */
00286 return 0;
00287 
00288 mode = (amrData[0] >> 3) & 0x000F;
00289 packet_size = block_size[mode];
00290 
00291 if (packet_size > buf_size) {
00292 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
00293 buf_size, packet_size + 1);
00294 return -1;
00295 }
00296 
00297 s->frameCount++;
00298 D_IF_decode(s->state, amrData, data, _good_frame);
00299 *data_size = 320 * 2;
00300 return packet_size;
00301 }
00302 
00303 static int amr_wb_decode_close(AVCodecContext *avctx)
00304 {
00305 AMRWBContext *s = avctx->priv_data;
00306 
00307 D_IF_exit(s->state);
00308 return 0;
00309 }
00310 
00311 AVCodec libopencore_amrwb_decoder = {
00312 "libopencore_amrwb",
00313 CODEC_TYPE_AUDIO,
00314 CODEC_ID_AMR_WB,
00315 sizeof(AMRWBContext),
00316 amr_wb_decode_init,
00317 NULL,
00318 amr_wb_decode_close,
00319 amr_wb_decode_frame,
00320 .long_name = NULL_IF_CONFIG_SMALL("OpenCORE Adaptive Multi-Rate (AMR) Wide-Band"),
00321 };
00322 
00323 #endif /* CONFIG_LIBOPENCORE_AMRWB */

Generated on Fri Oct 26 02:35:37 2012 for FFmpeg by doxygen 1.5.8

AltStyle によって変換されたページ (->オリジナル) /