diff -urN ffmpeg-svn-r13104.orig/libavcodec/libamr.c ffmpeg-svn-r13104/libavcodec/libamr.c --- ffmpeg-svn-r13104.orig/libavcodec/libamr.c 2008-05-07 03:41:57 +0300 +++ ffmpeg-svn-r13104/libavcodec/libamr.c 2008-05-09 18:10:32 +0300 @@ -65,6 +65,25 @@ #include "avcodec.h" +#if defined(CONFIG_LIBAMRNBBIN) || defined(CONFIG_LIBAMRWBBIN) +#include +static void* dlsymm(void* handle, char type, const char* symbol) +{ + void* f = dlsym(handle, symbol); + if (!f) + av_log( NULL, AV_LOG_ERROR, "AMR-%cB Codec - function '%s' can't be resolved\n", type, symbol); + return f; +} +#endif + +static void amr_decode_fix_avctx(AVCodecContext* avctx, int sample_rate, int frame_size) +{ + if(avctx->sample_rate == 0) avctx->sample_rate = sample_rate; + if(avctx->channels == 0) avctx->channels = 1; + avctx->frame_size = frame_size; +} + +#if defined(CONFIG_LIBAMR_NB) || defined(CONFIG_LIBAMR_NB_FIXED) #ifdef CONFIG_LIBAMR_NB_FIXED #define MMS_IO @@ -77,9 +96,14 @@ #include "amr/e_homing.h" #else +#ifdef CONFIG_LIBAMRNBBIN +static const char* libamrnbnames[] = {"libamrnb.so.3", "libamrnb.so.2"}; +#else #include #include #endif +#define amrnb_decode_fix_avctx(avctx) amr_decode_fix_avctx(avctx, 8000, 160) +#endif static const char *nb_bitrate_unsupported = "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n"; @@ -87,16 +111,28 @@ "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"; /* Common code for fixed and float version*/ +#ifdef CONFIG_LIBAMRNBBIN +#define Decoder_Interface_init s->amrnb_dec_init +#define Encoder_Interface_init(S) s->amrnb_enc_init(S, 1) +#define Decoder_Interface_exit(S) s->amrnb_exit(S); dlclose(s->handle) +#define Encoder_Interface_exit(S) s->amrnb_exit(S); dlclose(s->handle) +#define Decoder_Interface_Decode s->amrnb_decode +#define Encoder_Interface_Encode(S1, S2, S3, S4, S5) s->amrnb_encode(S1, S2, S3, S4, S5, 0) +#else typedef struct AMR_bitrates { int rate; enum Mode mode; } AMR_bitrates; +#endif /* Match desired bitrate */ static int getBitrateMode(int bitrate) { /* make the correspondance between bitrate and mode */ +#ifdef CONFIG_LIBAMRNBBIN + int rates[]={4750, 5150, 5900, 6700, 7400, 7950, 10200, 12200}; +#else AMR_bitrates rates[]={ {4750,MR475}, {5150,MR515}, {5900,MR59}, @@ -106,36 +142,27 @@ {10200,MR102}, {12200,MR122}, }; +#endif int i; for(i=0;i<8;i++) { +#ifdef CONFIG_LIBAMRNBBIN + if(rates[i]==bitrate) + { + return i; +#else + if(rates[i].rate==bitrate) { return rates[i].mode; +#endif } } /* no bitrate matching, return an error */ return -1; } -static void amr_decode_fix_avctx(AVCodecContext * avctx) -{ - const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB); - - if(avctx->sample_rate == 0) - { - avctx->sample_rate = 8000 * is_amr_wb; - } - - if(avctx->channels == 0) - { - avctx->channels = 1; - } - - avctx->frame_size = 160 * is_amr_wb; -} - #ifdef CONFIG_LIBAMR_NB_FIXED /* fixed point version*/ /* frame size in serial bitstream file (frame type + serial stream + flags) */ @@ -172,7 +199,7 @@ return -1; } - amr_decode_fix_avctx(avctx); + amrnb_decode_fix_avctx(avctx); if(avctx->channels > 1) { @@ -359,13 +386,54 @@ void * decState; int *enstate; int enc_bitrate; +#ifdef CONFIG_LIBAMRNBBIN + void* handle; + union { + void* (*amrnb_dec_init)(void); + void* (*amrnb_enc_init)(int dtx, char vad2_code); + }; + union { + void (*amrnb_decode)(void *st, unsigned char *bits, short *synth, int bfi); + int (*amrnb_encode)(void *st, int mode, short *speech, unsigned char *serial, int forceSpeech, char vad2_code); + }; + void (*amrnb_exit)(void *state); +#endif } AMRContext; +#ifdef CONFIG_LIBAMRNBBIN +static void* dlsymn(void* handle, const char* symbol) +{ + return dlsymm(handle, 'N', symbol); +} +#endif + static int amr_nb_decode_init(AVCodecContext * avctx) { AMRContext *s = avctx->priv_data; +#ifdef CONFIG_LIBAMRNBBIN + int i; +#endif s->frameCount=0; +#ifdef CONFIG_LIBAMRNBBIN + for(i=0, s->handle=NULL; i<(sizeof(libamrnbnames)/sizeof(char*)) && s->handle==NULL; i++) + s->handle = dlopen(libamrnbnames[i], RTLD_LAZY); + if (!s->handle) + { + av_log(avctx, AV_LOG_ERROR, "AMR-NB library "); + for(i=0; i<(sizeof(libamrnbnames)/sizeof(char*)); i++) av_log(avctx, AV_LOG_ERROR, "%s ", libamrnbnames[i]); + av_log(avctx, AV_LOG_ERROR, "could not be opened! \n%s\n", dlerror()); + return -1; + } + s->amrnb_dec_init=(void (*)) dlsymn(s->handle, "Decoder_Interface_init"); + s->amrnb_exit=(void (*)(void*)) dlsymn(s->handle, "Decoder_Interface_exit"); + s->amrnb_decode=(void (*)(void *, unsigned char *, short *, int)) dlsymn(s->handle, "GP3Decoder_Interface_Decode"); + if (!s->amrnb_dec_init || !s->amrnb_exit || !s->amrnb_decode) + { + dlclose(s->handle); + return -1; + } +#endif s->decState=Decoder_Interface_init(); if(!s->decState) { @@ -373,7 +441,7 @@ return -1; } - amr_decode_fix_avctx(avctx); + amrnb_decode_fix_avctx(avctx); if(avctx->channels > 1) { @@ -387,6 +455,9 @@ static int amr_nb_encode_init(AVCodecContext * avctx) { AMRContext *s = avctx->priv_data; +#ifdef CONFIG_LIBAMRNBBIN + int i; +#endif s->frameCount=0; @@ -405,6 +476,25 @@ avctx->frame_size=160; avctx->coded_frame= avcodec_alloc_frame(); +#ifdef CONFIG_LIBAMRNBBIN + for(i=0, s->handle=NULL; i<(sizeof(libamrnbnames)/sizeof(char*)) && s->handle==NULL; i++) + s->handle = dlopen(libamrnbnames[i], RTLD_LAZY); + if (!s->handle) + { + av_log(avctx, AV_LOG_ERROR, "AMR-NB library "); + for(i=0; i<(sizeof(libamrnbnames)/sizeof(char*)); i++) av_log(avctx, AV_LOG_ERROR, "%s ", libamrnbnames[i]); + av_log(avctx, AV_LOG_ERROR, "could not be opened! \n%s\n", dlerror()); + return -1; + } + s->amrnb_enc_init=(void* (*)(int, char)) dlsymn(s->handle, "VADxEncoder_Interface_init"); + s->amrnb_exit=(void (*)(void*)) dlsymn(s->handle, "Encoder_Interface_exit"); + s->amrnb_encode=(int (*)(void*, int, short*, unsigned char*, int, char)) dlsymn(s->handle, "GP3VADxEncoder_Interface_Encode"); + if (!s->amrnb_enc_init || !s->amrnb_exit || !s->amrnb_encode) + { + dlclose(s->handle); + return -1; + } +#endif s->enstate=Encoder_Interface_init(0); if(!s->enstate) { @@ -445,7 +535,11 @@ AMRContext *s = avctx->priv_data; uint8_t*amrData=buf; static const uint8_t block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; +#ifdef CONFIG_LIBAMRNBBIN + int dec_mode; +#else enum Mode dec_mode; +#endif int packet_size; /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */ @@ -479,11 +573,7 @@ return -1; } - written = Encoder_Interface_Encode(s->enstate, - s->enc_bitrate, - data, - frame, - 0); + written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data, frame, 0); /* 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] ); */ return written; @@ -491,8 +581,6 @@ #endif -#if defined(CONFIG_LIBAMR_NB) || defined(CONFIG_LIBAMR_NB_FIXED) - AVCodec libamr_nb_decoder = { "libamr_nb", @@ -529,11 +617,26 @@ #define typedef_h #endif +#ifdef CONFIG_LIBAMRWBBIN +#include +static const char* libamrwbnames[] = {"libamrwb.so.3", "libamrwb.so.2"}; +#define Word16 int16_t +#define E_IF_init s->amrwb_init +#define E_IF_exit(S) s->amrwb_exit(S); dlclose(s->handle) +#define E_IF_encode s->amrwb_encode +#define D_IF_init s->amrwb_init +#define block_size s->amrwb_block_size +#define _good_frame 0 +#define D_IF_decode s->amrwb_decode +#define D_IF_exit(S) s->amrwb_exit(S); dlclose(s->handle) +#else #include #include #include +#endif + +#define amrwb_decode_fix_avctx(avctx) amr_decode_fix_avctx(avctx, 16000, 320) -/* Common code for fixed and float version*/ typedef struct AMRWB_bitrates { int rate; @@ -571,12 +674,32 @@ int frameCount; void *state; int mode; +#ifdef CONFIG_LIBAMRWBBIN + void* handle; + void* (*amrwb_init)(void); + union { + void (*amrwb_decode)(void *st, uint8_t *bits, int16_t *synth, int32_t bfi); + int (*amrwb_encode)(void *st, int16_t mode, int16_t *speech, uint8_t *serial, int16_t dtx); + }; + void (*amrwb_exit)(void *state); + uint8_t *amrwb_block_size; +#endif Word16 allow_dtx; } AMRWBContext; +#ifdef CONFIG_LIBAMRWBBIN +static void* dlsymw(void* handle, const char* symbol) +{ + return dlsymm(handle, 'W', symbol); +} +#endif + static int amr_wb_encode_init(AVCodecContext * avctx) { AMRWBContext *s = avctx->priv_data; +#ifdef CONFIG_LIBAMRNBBIN + int i; +#endif s->frameCount=0; @@ -601,6 +724,25 @@ avctx->frame_size=320; avctx->coded_frame= avcodec_alloc_frame(); +#ifdef CONFIG_LIBAMRWBBIN + for(i=0, s->handle=NULL; i<(sizeof(libamrwbnames)/sizeof(char*)) && s->handle==NULL; i++) + s->handle = dlopen(libamrwbnames[i], RTLD_LAZY); + if (!s->handle) + { + av_log(avctx, AV_LOG_ERROR, "AMR-WB library "); + for(i=0; i<(sizeof(libamrwbnames)/sizeof(char*)); i++) av_log(avctx, AV_LOG_ERROR, "%s ", libamrwbnames[i]); + av_log(avctx, AV_LOG_ERROR, "could not be opened! \n%s\n", dlerror()); + return -1; + } + s->amrwb_init=(void (*)) dlsymw(s->handle, "E_IF_init"); + s->amrwb_exit=(void (*)(void*)) dlsymw(s->handle, "E_IF_exit"); + s->amrwb_encode=(int (*)(void *, int16_t, int16_t *, uint8_t *, int16_t)) dlsymw(s->handle, "GP3E_IF_encode"); + if (!s->amrwb_init || !s->amrwb_exit || !s->amrwb_encode) + { + dlclose(s->handle); + return -1; + } +#endif s->state = E_IF_init(); s->allow_dtx=0; @@ -635,11 +777,34 @@ static int amr_wb_decode_init(AVCodecContext * avctx) { AMRWBContext *s = avctx->priv_data; +#ifdef CONFIG_LIBAMRNBBIN + int i; +#endif s->frameCount=0; +#ifdef CONFIG_LIBAMRWBBIN + for(i=0, s->handle=NULL; i<(sizeof(libamrwbnames)/sizeof(char*)) && s->handle==NULL; i++) + s->handle = dlopen(libamrwbnames[i], RTLD_LAZY); + if (!s->handle) + { + av_log(avctx, AV_LOG_ERROR, "AMR-WB library "); + for(i=0; i<(sizeof(libamrwbnames)/sizeof(char*)); i++) av_log(avctx, AV_LOG_ERROR, "%s ", libamrwbnames[i]); + av_log(avctx, AV_LOG_ERROR, "could not be opened! \n%s\n", dlerror()); + return -1; + } + s->amrwb_init=(void (*)) dlsymw(s->handle, "D_IF_init"); + s->amrwb_exit=(void (*)(void*)) dlsymw(s->handle, "D_IF_exit"); + s->amrwb_block_size=(uint8_t (*)) dlsymw(s->handle, "GP3block_size"); + s->amrwb_decode=(void (*)(void *, uint8_t *, int16_t *, int32_t)) dlsymw(s->handle, "GP3D_IF_decode"); + if (!s->amrwb_init || !s->amrwb_exit || !s->amrwb_block_size || !s->amrwb_decode) + { + dlclose(s->handle); + return -1; + } +#endif s->state = D_IF_init(); - amr_decode_fix_avctx(avctx); + amrwb_decode_fix_avctx(avctx); if(avctx->channels > 1) { @@ -658,7 +823,9 @@ uint8_t*amrData=buf; int mode; int packet_size; +#ifndef CONFIG_LIBAMRWBBIN static const uint8_t block_size[16] = {18, 23, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1}; +#endif if(buf_size==0) { /* nothing to do */