# HG changeset patch # User diego # Date 1181088858 0 # Node ID c3f2379b80db450a3585c1881253b26253b028a1 # Parent daff9ebd2e0bb9a1235393c20335bc172fce3e46 Give all wrappers for external libraries names starting with lib. diff -r daff9ebd2e0b -r c3f2379b80db Makefile --- a/Makefile Tue Jun 05 14:18:13 2007 +0000 +++ b/Makefile Wed Jun 06 00:14:18 2007 +0000 @@ -277,16 +277,16 @@ OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER) += adpcm.o # external codec libraries -OBJS-$(CONFIG_LIBAMR) += amr.o -OBJS-$(CONFIG_LIBA52) += a52dec.o -OBJS-$(CONFIG_LIBFAAC) += faac.o -OBJS-$(CONFIG_LIBFAAD) += faad.o +OBJS-$(CONFIG_LIBA52) += liba52.o +OBJS-$(CONFIG_LIBAMR) += libamr.o +OBJS-$(CONFIG_LIBFAAC) += libfaac.o +OBJS-$(CONFIG_LIBFAAD) += libfaad.o OBJS-$(CONFIG_LIBGSM) += libgsm.o -OBJS-$(CONFIG_LIBMP3LAME) += mp3lameaudio.o +OBJS-$(CONFIG_LIBMP3LAME) += libmp3lame.o OBJS-$(CONFIG_LIBTHEORA) += libtheoraenc.o -OBJS-$(CONFIG_LIBVORBIS) += oggvorbis.o -OBJS-$(CONFIG_X264) += x264.o -OBJS-$(CONFIG_XVID) += xvidff.o xvid_rc.o +OBJS-$(CONFIG_LIBVORBIS) += libvorbis.o +OBJS-$(CONFIG_X264) += libx264.o +OBJS-$(CONFIG_XVID) += libxvidff.o libxvid_rc.o OBJS-$(CONFIG_AAC_PARSER) += aac_parser.o aac_ac3_parser.o diff -r daff9ebd2e0b -r c3f2379b80db a52dec.c --- a/a52dec.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,259 +0,0 @@ -/* - * A52 decoder using liba52 - * Copyright (c) 2001 Fabrice Bellard. - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file a52dec.c - * A52 decoder using liba52 - */ - -#include "avcodec.h" -#include - -#ifdef CONFIG_LIBA52BIN -#include -static const char* liba52name = "liba52.so.0"; -#endif - -/** - * liba52 - Copyright (C) Aaron Holtzman - * released under the GPL license. - */ -typedef struct AC3DecodeState { - uint8_t inbuf[4096]; /* input buffer */ - uint8_t *inbuf_ptr; - int frame_size; - int flags; - int channels; - a52_state_t* state; - sample_t* samples; - - /* - * virtual method table - * - * using this function table so the liba52 doesn't - * have to be really linked together with ffmpeg - * and might be linked in runtime - this allows binary - * distribution of ffmpeg library which doens't depend - * on liba52 library - but if user has it installed - * it will be used - user might install such library - * separately - */ - void* handle; - a52_state_t* (*a52_init)(uint32_t mm_accel); - sample_t* (*a52_samples)(a52_state_t * state); - int (*a52_syncinfo)(uint8_t * buf, int * flags, - int * sample_rate, int * bit_rate); - int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags, - sample_t * level, sample_t bias); - void (*a52_dynrng)(a52_state_t * state, - sample_t (* call) (sample_t, void *), void * data); - int (*a52_block)(a52_state_t * state); - void (*a52_free)(a52_state_t * state); - -} AC3DecodeState; - -#ifdef CONFIG_LIBA52BIN -static void* dlsymm(void* handle, const char* symbol) -{ - void* f = dlsym(handle, symbol); - if (!f) - av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); - return f; -} -#endif - -static int a52_decode_init(AVCodecContext *avctx) -{ - AC3DecodeState *s = avctx->priv_data; - -#ifdef CONFIG_LIBA52BIN - s->handle = dlopen(liba52name, RTLD_LAZY); - if (!s->handle) - { - av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); - return -1; - } - s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); - s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); - s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); - s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); - s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); - s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); - if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo - || !s->a52_frame || !s->a52_block || !s->a52_free) - { - dlclose(s->handle); - return -1; - } -#else - s->handle = 0; - s->a52_init = a52_init; - s->a52_samples = a52_samples; - s->a52_syncinfo = a52_syncinfo; - s->a52_frame = a52_frame; - s->a52_block = a52_block; - s->a52_free = a52_free; -#endif - s->state = s->a52_init(0); /* later use CPU flags */ - s->samples = s->a52_samples(s->state); - s->inbuf_ptr = s->inbuf; - s->frame_size = 0; - - return 0; -} - -/**** the following two functions comes from a52dec */ -static inline int blah (int32_t i) -{ - if (i > 0x43c07fff) - return 32767; - else if (i < 0x43bf8000) - return -32768; - return i - 0x43c00000; -} - -static inline void float_to_int (float * _f, int16_t * s16, int nchannels) -{ - int i, j, c; - int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format - - j = 0; - nchannels *= 256; - for (i = 0; i < 256; i++) { - for (c = 0; c < nchannels; c += 256) - s16[j++] = blah (f[i + c]); - } -} - -/**** end */ - -#define HEADER_SIZE 7 - -static int a52_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ - AC3DecodeState *s = avctx->priv_data; - uint8_t *buf_ptr; - int flags, i, len; - int sample_rate, bit_rate; - short *out_samples = data; - float level; - static const int ac3_channels[8] = { - 2, 1, 2, 3, 3, 4, 4, 5 - }; - - *data_size= 0; - - buf_ptr = buf; - while (buf_size > 0) { - len = s->inbuf_ptr - s->inbuf; - if (s->frame_size == 0) { - /* no header seen : find one. We need at least 7 bytes to parse it */ - len = HEADER_SIZE - len; - if (len > buf_size) - len = buf_size; - memcpy(s->inbuf_ptr, buf_ptr, len); - buf_ptr += len; - s->inbuf_ptr += len; - buf_size -= len; - if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { - len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); - if (len == 0) { - /* no sync found : move by one byte (inefficient, but simple!) */ - memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); - s->inbuf_ptr--; - } else { - s->frame_size = len; - /* update codec info */ - avctx->sample_rate = sample_rate; - s->channels = ac3_channels[s->flags & 7]; - if (s->flags & A52_LFE) - s->channels++; - if (avctx->channels == 0) - /* No specific number of channel requested */ - avctx->channels = s->channels; - else if (s->channels < avctx->channels) { - av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); - avctx->channels = s->channels; - } - avctx->bit_rate = bit_rate; - } - } - } else if (len < s->frame_size) { - len = s->frame_size - len; - if (len > buf_size) - len = buf_size; - - memcpy(s->inbuf_ptr, buf_ptr, len); - buf_ptr += len; - s->inbuf_ptr += len; - buf_size -= len; - } else { - flags = s->flags; - if (avctx->channels == 1) - flags = A52_MONO; - else if (avctx->channels == 2) - flags = A52_STEREO; - else - flags |= A52_ADJUST_LEVEL; - level = 1; - if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { - fail: - av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); - s->inbuf_ptr = s->inbuf; - s->frame_size = 0; - continue; - } - for (i = 0; i < 6; i++) { - if (s->a52_block(s->state)) - goto fail; - float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); - } - s->inbuf_ptr = s->inbuf; - s->frame_size = 0; - *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); - break; - } - } - return buf_ptr - buf; -} - -static int a52_decode_end(AVCodecContext *avctx) -{ - AC3DecodeState *s = avctx->priv_data; - s->a52_free(s->state); -#ifdef CONFIG_LIBA52BIN - dlclose(s->handle); -#endif - return 0; -} - -AVCodec liba52_decoder = { - "ac3", - CODEC_TYPE_AUDIO, - CODEC_ID_AC3, - sizeof(AC3DecodeState), - a52_decode_init, - NULL, - a52_decode_end, - a52_decode_frame, -}; diff -r daff9ebd2e0b -r c3f2379b80db amr.c --- a/amr.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,711 +0,0 @@ -/* - * AMR Audio decoder stub - * Copyright (c) 2003 the ffmpeg project - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - - /** @file - * Adaptive Multi-Rate (AMR) Audio decoder stub. - * - * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand - * (AMR-WB) audio encoder/decoder through external reference code from - * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you - * have to download the code separately. Two versions exists: One fixed-point - * and one floating-point. For some reason the float encoder is significantly - * faster at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip - * at MR102). Both float and fixed point are supported for AMR-NB, but only - * float for AMR-WB. - * - * \section AMR-NB - * - * \subsection Float - * The float version (default) can be downloaded from: - * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip - * - * \subsection Fixed-point - * The fixed-point (TS26.073) can be downloaded from: - * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-600.zip - * - * \subsection Specification - * The specification for AMR-NB can be found in TS 26.071 - * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other - * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. - * - * \section AMR-WB - * - * \subsection Float - * The reference code can be downloaded from: - * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip - * - * \subsection Fixed-point - * If someone wants to use the fixed point version it can be downloaded from: - * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip. - * - * \subsection Specification - * The specification for AMR-WB can be found in TS 26.171 - * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other - * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. - * - */ - -#include "avcodec.h" - -#ifdef CONFIG_LIBAMR_NB_FIXED - -#define MMS_IO - -#include "amr/sp_dec.h" -#include "amr/d_homing.h" -#include "amr/typedef.h" -#include "amr/sp_enc.h" -#include "amr/sid_sync.h" -#include "amr/e_homing.h" - -#else -#include -#include -#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"; -static const char *wb_bitrate_unsupported = - "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*/ -typedef struct AMR_bitrates -{ - int rate; - enum Mode mode; -} AMR_bitrates; - -/* Match desired bitrate */ -static int getBitrateMode(int bitrate) -{ - /* make the correspondance between bitrate and mode */ - AMR_bitrates rates[]={ {4750,MR475}, - {5150,MR515}, - {5900,MR59}, - {6700,MR67}, - {7400,MR74}, - {7950,MR795}, - {10200,MR102}, - {12200,MR122}, - }; - int i; - - for(i=0;i<8;i++) - { - if(rates[i].rate==bitrate) - { - return(rates[i].mode); - } - } - /* 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) */ -#define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5) - -typedef struct AMRContext { - int frameCount; - Speech_Decode_FrameState *speech_decoder_state; - enum RXFrameType rx_type; - enum Mode mode; - Word16 reset_flag; - Word16 reset_flag_old; - - int enc_bitrate; - Speech_Encode_FrameState *enstate; - sid_syncState *sidstate; - enum TXFrameType tx_frametype; -} AMRContext; - -static int amr_nb_decode_init(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - s->frameCount=0; - s->speech_decoder_state=NULL; - s->rx_type = (enum RXFrameType)0; - s->mode= (enum Mode)0; - s->reset_flag=0; - s->reset_flag_old=1; - - if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder")) - { - av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n"); - return -1; - } - - amr_decode_fix_avctx(avctx); - - if(avctx->channels > 1) - { - av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); - return -1; - } - - return 0; -} - -static int amr_nb_encode_init(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - s->frameCount=0; - s->speech_decoder_state=NULL; - s->rx_type = (enum RXFrameType)0; - s->mode= (enum Mode)0; - s->reset_flag=0; - s->reset_flag_old=1; - - if(avctx->sample_rate!=8000) - { - av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); - return -1; - } - - if(avctx->channels!=1) - { - av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); - return -1; - } - - avctx->frame_size=160; - avctx->coded_frame= avcodec_alloc_frame(); - - if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate)) - { - av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n"); - return -1; - } - - if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) - { - av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); - return -1; - } - - return 0; -} - -static int amr_nb_encode_close(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - Speech_Encode_Frame_exit(&s->enstate); - sid_sync_exit (&s->sidstate); - av_freep(&avctx->coded_frame); - return 0; -} - -static int amr_nb_decode_close(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - Speech_Decode_Frame_exit(&s->speech_decoder_state); - return 0; -} - -static int amr_nb_decode_frame(AVCodecContext * avctx, - void *data, int *data_size, - uint8_t * buf, int buf_size) -{ - AMRContext *s = avctx->priv_data; - uint8_t*amrData=buf; - int offset=0; - UWord8 toc, q, ft; - Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ - Word16 *synth; - UWord8 *packed_bits; - static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; - int i; - - //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount); - - synth=data; - - toc=amrData[offset]; - /* read rest of the frame based on ToC byte */ - q = (toc >> 2) & 0x01; - ft = (toc >> 3) & 0x0F; - - //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]); - - offset++; - - packed_bits=amrData+offset; - - offset+=packed_size[ft]; - - //Unsort and unpack bits - s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); - - //We have a new frame - s->frameCount++; - - if (s->rx_type == RX_NO_DATA) - { - s->mode = s->speech_decoder_state->prev_mode; - } - else { - s->speech_decoder_state->prev_mode = s->mode; - } - - /* if homed: check if this frame is another homing frame */ - if (s->reset_flag_old == 1) - { - /* only check until end of first subframe */ - s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); - } - /* produce encoder homing frame if homed & input=decoder homing frame */ - if ((s->reset_flag != 0) && (s->reset_flag_old != 0)) - { - for (i = 0; i < L_FRAME; i++) - { - synth[i] = EHF_MASK; - } - } - else - { - /* decode frame */ - Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); - } - - //Each AMR-frame results in 160 16-bit samples - *data_size=160*2; - - /* if not homed: check whether current frame is a homing frame */ - if (s->reset_flag_old == 0) - { - /* check whole frame */ - s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); - } - /* reset decoder if current frame is a homing frame */ - if (s->reset_flag != 0) - { - Speech_Decode_Frame_reset(s->speech_decoder_state); - } - s->reset_flag_old = s->reset_flag; - - return offset; -} - - -static int amr_nb_encode_frame(AVCodecContext *avctx, - unsigned char *frame/*out*/, int buf_size, void *data/*in*/) -{ - short serial_data[250] = {0}; - AMRContext *s = avctx->priv_data; - int written; - - s->reset_flag = encoder_homing_frame_test(data); - - Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); - - /* add frame type and mode */ - sid_sync (s->sidstate, s->mode, &s->tx_frametype); - - written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame); - - if (s->reset_flag != 0) - { - Speech_Encode_Frame_reset(s->enstate); - sid_sync_reset(s->sidstate); - } - return written; -} - - -#elif defined(CONFIG_LIBAMR_NB) /* Float point version*/ - -typedef struct AMRContext { - int frameCount; - void * decState; - int *enstate; - int enc_bitrate; -} AMRContext; - -static int amr_nb_decode_init(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - s->frameCount=0; - s->decState=Decoder_Interface_init(); - if(!s->decState) - { - av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n"); - return -1; - } - - amr_decode_fix_avctx(avctx); - - if(avctx->channels > 1) - { - av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); - return -1; - } - - return 0; -} - -static int amr_nb_encode_init(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - s->frameCount=0; - - if(avctx->sample_rate!=8000) - { - av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); - return -1; - } - - if(avctx->channels!=1) - { - av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); - return -1; - } - - avctx->frame_size=160; - avctx->coded_frame= avcodec_alloc_frame(); - - s->enstate=Encoder_Interface_init(0); - if(!s->enstate) - { - av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n"); - return -1; - } - - if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) - { - av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); - return -1; - } - - return 0; -} - -static int amr_nb_decode_close(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - Decoder_Interface_exit(s->decState); - return 0; -} - -static int amr_nb_encode_close(AVCodecContext * avctx) -{ - AMRContext *s = avctx->priv_data; - - Encoder_Interface_exit(s->enstate); - av_freep(&avctx->coded_frame); - return 0; -} - -static int amr_nb_decode_frame(AVCodecContext * avctx, - void *data, int *data_size, - uint8_t * buf, int buf_size) -{ - AMRContext *s = avctx->priv_data; - uint8_t*amrData=buf; - static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; - enum Mode dec_mode; - 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); */ - - dec_mode = (buf[0] >> 3) & 0x000F; - packet_size = block_size[dec_mode]+1; - - if(packet_size > buf_size) { - av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size); - return -1; - } - - s->frameCount++; - /* 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]); */ - /* call decoder */ - Decoder_Interface_Decode(s->decState, amrData, data, 0); - *data_size=160*2; - - return packet_size; -} - -static int amr_nb_encode_frame(AVCodecContext *avctx, - unsigned char *frame/*out*/, int buf_size, void *data/*in*/) -{ - AMRContext *s = avctx->priv_data; - int written; - - if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) - { - av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); - return -1; - } - - 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; -} - -#endif - -#if defined(CONFIG_LIBAMR_NB) || defined(CONFIG_LIBAMR_NB_FIXED) - -AVCodec libamr_nb_decoder = -{ - "libamr_nb", - CODEC_TYPE_AUDIO, - CODEC_ID_AMR_NB, - sizeof(AMRContext), - amr_nb_decode_init, - NULL, - amr_nb_decode_close, - amr_nb_decode_frame, -}; - -AVCodec libamr_nb_encoder = -{ - "libamr_nb", - CODEC_TYPE_AUDIO, - CODEC_ID_AMR_NB, - sizeof(AMRContext), - amr_nb_encode_init, - amr_nb_encode_frame, - amr_nb_encode_close, - NULL, -}; - -#endif - -/* -----------AMR wideband ------------*/ -#ifdef CONFIG_LIBAMR_WB - -#ifdef _TYPEDEF_H -//To avoid duplicate typedefs from typdef in amr-nb -#define typedef_h -#endif - -#include -#include -#include - -/* Common code for fixed and float version*/ -typedef struct AMRWB_bitrates -{ - int rate; - int mode; -} AMRWB_bitrates; - -static int getWBBitrateMode(int bitrate) -{ - /* make the correspondance between bitrate and mode */ - AMRWB_bitrates rates[]={ {6600,0}, - {8850,1}, - {12650,2}, - {14250,3}, - {15850,4}, - {18250,5}, - {19850,6}, - {23050,7}, - {23850,8}, - }; - int i; - - for(i=0;i<9;i++) - { - if(rates[i].rate==bitrate) - { - return(rates[i].mode); - } - } - /* no bitrate matching, return an error */ - return -1; -} - - -typedef struct AMRWBContext { - int frameCount; - void *state; - int mode; - Word16 allow_dtx; -} AMRWBContext; - -static int amr_wb_encode_init(AVCodecContext * avctx) -{ - AMRWBContext *s = avctx->priv_data; - - s->frameCount=0; - - if(avctx->sample_rate!=16000) - { - av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n"); - return -1; - } - - if(avctx->channels!=1) - { - av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); - return -1; - } - - if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) - { - av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); - return -1; - } - - avctx->frame_size=320; - avctx->coded_frame= avcodec_alloc_frame(); - - s->state = E_IF_init(); - s->allow_dtx=0; - - return 0; -} - -static int amr_wb_encode_close(AVCodecContext * avctx) -{ - AMRWBContext *s = avctx->priv_data; - - E_IF_exit(s->state); - av_freep(&avctx->coded_frame); - s->frameCount++; - return 0; -} - -static int amr_wb_encode_frame(AVCodecContext *avctx, - unsigned char *frame/*out*/, int buf_size, void *data/*in*/) -{ - AMRWBContext *s = avctx->priv_data; - int size; - - if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) - { - av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); - return -1; - } - size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx); - return size; -} - -static int amr_wb_decode_init(AVCodecContext * avctx) -{ - AMRWBContext *s = avctx->priv_data; - - s->frameCount=0; - s->state = D_IF_init(); - - amr_decode_fix_avctx(avctx); - - if(avctx->channels > 1) - { - av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n"); - return -1; - } - - return 0; -} - -static int amr_wb_decode_frame(AVCodecContext * avctx, - void *data, int *data_size, - uint8_t * buf, int buf_size) -{ - AMRWBContext *s = avctx->priv_data; - uint8_t*amrData=buf; - int mode; - int packet_size; - - if(buf_size==0) { - /* nothing to do */ - return 0; - } - - mode = (amrData[0] >> 3) & 0x000F; - packet_size = block_size[mode]; - - if(packet_size > buf_size) { - av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1); - return -1; - } - - s->frameCount++; - D_IF_decode( s->state, amrData, data, _good_frame); - *data_size=320*2; - return packet_size; -} - -static int amr_wb_decode_close(AVCodecContext * avctx) -{ - AMRWBContext *s = avctx->priv_data; - - D_IF_exit(s->state); - return 0; -} - -AVCodec libamr_wb_decoder = -{ - "libamr_wb", - CODEC_TYPE_AUDIO, - CODEC_ID_AMR_WB, - sizeof(AMRWBContext), - amr_wb_decode_init, - NULL, - amr_wb_decode_close, - amr_wb_decode_frame, -}; - -AVCodec libamr_wb_encoder = -{ - "libamr_wb", - CODEC_TYPE_AUDIO, - CODEC_ID_AMR_WB, - sizeof(AMRWBContext), - amr_wb_encode_init, - amr_wb_encode_frame, - amr_wb_encode_close, - NULL, -}; - -#endif //CONFIG_LIBAMR_WB diff -r daff9ebd2e0b -r c3f2379b80db faac.c --- a/faac.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -/* - * Interface to libfaac for aac encoding - * Copyright (c) 2002 Gildas Bazin - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file faacaudio.c - * Interface to libfaac for aac encoding. - */ - -#include "avcodec.h" -#include - -typedef struct FaacAudioContext { - faacEncHandle faac_handle; -} FaacAudioContext; - -static int Faac_encode_init(AVCodecContext *avctx) -{ - FaacAudioContext *s = avctx->priv_data; - faacEncConfigurationPtr faac_cfg; - unsigned long samples_input, max_bytes_output; - - /* number of channels */ - if (avctx->channels < 1 || avctx->channels > 6) - return -1; - - s->faac_handle = faacEncOpen(avctx->sample_rate, - avctx->channels, - &samples_input, &max_bytes_output); - - /* check faac version */ - faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle); - if (faac_cfg->version != FAAC_CFG_VERSION) { - av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version); - faacEncClose(s->faac_handle); - return -1; - } - - /* put the options in the configuration struct */ - switch(avctx->profile) { - case FF_PROFILE_AAC_MAIN: - faac_cfg->aacObjectType = MAIN; - break; - case FF_PROFILE_UNKNOWN: - case FF_PROFILE_AAC_LOW: - faac_cfg->aacObjectType = LOW; - break; - case FF_PROFILE_AAC_SSR: - faac_cfg->aacObjectType = SSR; - break; - case FF_PROFILE_AAC_LTP: - faac_cfg->aacObjectType = LTP; - break; - default: - av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n"); - faacEncClose(s->faac_handle); - return -1; - } - faac_cfg->mpegVersion = MPEG4; - faac_cfg->useTns = 0; - faac_cfg->allowMidside = 1; - faac_cfg->bitRate = avctx->bit_rate / avctx->channels; - faac_cfg->bandWidth = avctx->cutoff; - if(avctx->flags & CODEC_FLAG_QSCALE) { - faac_cfg->bitRate = 0; - faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA; - } - faac_cfg->outputFormat = 1; - faac_cfg->inputFormat = FAAC_INPUT_16BIT; - - avctx->frame_size = samples_input / avctx->channels; - - avctx->coded_frame= avcodec_alloc_frame(); - avctx->coded_frame->key_frame= 1; - - /* Set decoder specific info */ - avctx->extradata_size = 0; - if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { - - unsigned char *buffer = NULL; - unsigned long decoder_specific_info_size; - - if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer, - &decoder_specific_info_size)) { - avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE); - avctx->extradata_size = decoder_specific_info_size; - memcpy(avctx->extradata, buffer, avctx->extradata_size); - faac_cfg->outputFormat = 0; - } -#undef free - free(buffer); -#define free please_use_av_free - } - - if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) { - av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n"); - return -1; - } - - return 0; -} - -static int Faac_encode_frame(AVCodecContext *avctx, - unsigned char *frame, int buf_size, void *data) -{ - FaacAudioContext *s = avctx->priv_data; - int bytes_written; - - bytes_written = faacEncEncode(s->faac_handle, - data, - avctx->frame_size * avctx->channels, - frame, - buf_size); - - return bytes_written; -} - -static int Faac_encode_close(AVCodecContext *avctx) -{ - FaacAudioContext *s = avctx->priv_data; - - av_freep(&avctx->coded_frame); - av_freep(&avctx->extradata); - - faacEncClose(s->faac_handle); - return 0; -} - -AVCodec libfaac_encoder = { - "libfaac", - CODEC_TYPE_AUDIO, - CODEC_ID_AAC, - sizeof(FaacAudioContext), - Faac_encode_init, - Faac_encode_frame, - Faac_encode_close -}; diff -r daff9ebd2e0b -r c3f2379b80db faad.c --- a/faad.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,333 +0,0 @@ -/* - * Faad decoder - * Copyright (c) 2003 Zdenek Kabelac. - * Copyright (c) 2004 Thomas Raivio. - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file faad.c - * AAC decoder. - * - * still a bit unfinished - but it plays something - */ - -#include "avcodec.h" -#include "faad.h" - -#ifndef FAADAPI -#define FAADAPI -#endif - -/* - * when CONFIG_LIBFAADBIN is defined the libfaad will be opened at runtime - */ -//#undef CONFIG_LIBFAADBIN -//#define CONFIG_LIBFAADBIN - -#ifdef CONFIG_LIBFAADBIN -#include -static const char* libfaadname = "libfaad.so.0"; -#else -#define dlopen(a) -#define dlclose(a) -#endif - -typedef struct { - void* handle; /* dlopen handle */ - void* faac_handle; /* FAAD library handle */ - int sample_size; - int init; - - /* faad calls */ - faacDecHandle FAADAPI (*faacDecOpen)(void); - faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder); -#ifndef FAAD2_VERSION - int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, - faacDecConfigurationPtr config); - int FAADAPI (*faacDecInit)(faacDecHandle hDecoder, - unsigned char *buffer, - unsigned long *samplerate, - unsigned long *channels); - int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer, - unsigned long SizeOfDecoderSpecificInfo, - unsigned long *samplerate, unsigned long *channels); - int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder, - unsigned char *buffer, - unsigned long *bytesconsumed, - short *sample_buffer, - unsigned long *samples); -#else - unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, - faacDecConfigurationPtr config); - long FAADAPI (*faacDecInit)(faacDecHandle hDecoder, - unsigned char *buffer, - unsigned long buffer_size, - unsigned long *samplerate, - unsigned char *channels); - char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer, - unsigned long SizeOfDecoderSpecificInfo, - unsigned long *samplerate, unsigned char *channels); - void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder, - faacDecFrameInfo *hInfo, - unsigned char *buffer, - unsigned long buffer_size); - char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode); -#endif - - void FAADAPI (*faacDecClose)(faacDecHandle hDecoder); - - -} FAACContext; - -static const unsigned long faac_srates[] = -{ - 96000, 88200, 64000, 48000, 44100, 32000, - 24000, 22050, 16000, 12000, 11025, 8000 -}; - -static int faac_init_mp4(AVCodecContext *avctx) -{ - FAACContext *s = avctx->priv_data; - unsigned long samplerate; -#ifndef FAAD2_VERSION - unsigned long channels; -#else - unsigned char channels; -#endif - int r = 0; - - if (avctx->extradata){ - r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata, - avctx->extradata_size, - &samplerate, &channels); - if (r < 0){ - av_log(avctx, AV_LOG_ERROR, - "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n", - r, samplerate, (long)channels, avctx->extradata_size); - } else { - avctx->sample_rate = samplerate; - avctx->channels = channels; - s->init = 1; - } - } - - return r; -} - -static int faac_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ - FAACContext *s = avctx->priv_data; -#ifndef FAAD2_VERSION - unsigned long bytesconsumed; - short *sample_buffer = NULL; - unsigned long samples; - int out; -#else - faacDecFrameInfo frame_info; - void *out; -#endif - if(buf_size == 0) - return 0; -#ifndef FAAD2_VERSION - out = s->faacDecDecode(s->faac_handle, - (unsigned char*)buf, - &bytesconsumed, - data, - &samples); - samples *= s->sample_size; - if (data_size) - *data_size = samples; - return (buf_size < (int)bytesconsumed) - ? buf_size : (int)bytesconsumed; -#else - - if(!s->init){ - unsigned long srate; - unsigned char channels; - int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels); - if(r < 0){ - av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n", - s->faacDecGetErrorMessage(frame_info.error)); - return -1; - } - avctx->sample_rate = srate; - avctx->channels = channels; - s->init = 1; - } - - out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size); - - if (frame_info.error > 0) { - av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n", - s->faacDecGetErrorMessage(frame_info.error)); - return -1; - } - - frame_info.samples *= s->sample_size; - memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one - - if (data_size) - *data_size = frame_info.samples; - - return (buf_size < (int)frame_info.bytesconsumed) - ? buf_size : (int)frame_info.bytesconsumed; -#endif -} - -static int faac_decode_end(AVCodecContext *avctx) -{ - FAACContext *s = avctx->priv_data; - - s->faacDecClose(s->faac_handle); - - dlclose(s->handle); - return 0; -} - -static int faac_decode_init(AVCodecContext *avctx) -{ - FAACContext *s = avctx->priv_data; - faacDecConfigurationPtr faac_cfg; - -#ifdef CONFIG_LIBFAADBIN - const char* err = 0; - - s->handle = dlopen(libfaadname, RTLD_LAZY); - if (!s->handle) - { - av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n", - libfaadname, dlerror()); - return -1; - } -#define dfaac(a, b) \ - do { static const char* n = "faacDec" #a; \ - if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0) - for(;;) { -#else /* !CONFIG_LIBFAADBIN */ -#define dfaac(a, b) s->faacDec ## a = faacDec ## a -#endif /* CONFIG_LIBFAADBIN */ - - // resolve all needed function calls - dfaac(Open, (faacDecHandle FAADAPI (*)(void))); - dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder))); - dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr - FAADAPI (*)(faacDecHandle))); -#ifndef FAAD2_VERSION - dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle, - faacDecConfigurationPtr))); - - dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*, - unsigned long*, unsigned long*))); - dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*, - unsigned long, unsigned long*, - unsigned long*))); - dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*, - unsigned long*, short*, unsigned long*))); -#else - dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle, - faacDecConfigurationPtr))); - dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*, - unsigned long, unsigned long*, unsigned char*))); - dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*, - unsigned long, unsigned long*, - unsigned char*))); - dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*, - unsigned char*, unsigned long))); - dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char))); -#endif -#undef dfacc - -#ifdef CONFIG_LIBFAADBIN - break; - } - if (err) { - dlclose(s->handle); - av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n", - err, libfaadname); - return -1; - } -#endif - - s->faac_handle = s->faacDecOpen(); - if (!s->faac_handle) { - av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n"); - faac_decode_end(avctx); - return -1; - } - - - faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle); - - if (faac_cfg) { - switch (avctx->bits_per_sample) { - case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break; - default: - case 16: -#ifdef FAAD2_VERSION - faac_cfg->outputFormat = FAAD_FMT_16BIT; -#endif - s->sample_size = 2; - break; - case 24: -#ifdef FAAD2_VERSION - faac_cfg->outputFormat = FAAD_FMT_24BIT; -#endif - s->sample_size = 3; - break; - case 32: -#ifdef FAAD2_VERSION - faac_cfg->outputFormat = FAAD_FMT_32BIT; -#endif - s->sample_size = 4; - break; - } - - faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate; - faac_cfg->defObjectType = LC; - } - - s->faacDecSetConfiguration(s->faac_handle, faac_cfg); - - faac_init_mp4(avctx); - - return 0; -} - -#define AAC_CODEC(id, name) \ -AVCodec name ## _decoder = { \ - #name, \ - CODEC_TYPE_AUDIO, \ - id, \ - sizeof(FAACContext), \ - faac_decode_init, \ - NULL, \ - faac_decode_end, \ - faac_decode_frame, \ -} - -// FIXME - raw AAC files - maybe just one entry will be enough -AAC_CODEC(CODEC_ID_AAC, libfaad); -#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) -// If it's mp4 file - usually embeded into Qt Mov -AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac); -#endif - -#undef AAC_CODEC diff -r daff9ebd2e0b -r c3f2379b80db liba52.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/liba52.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,259 @@ +/* + * A52 decoder using liba52 + * Copyright (c) 2001 Fabrice Bellard. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file a52dec.c + * A52 decoder using liba52 + */ + +#include "avcodec.h" +#include + +#ifdef CONFIG_LIBA52BIN +#include +static const char* liba52name = "liba52.so.0"; +#endif + +/** + * liba52 - Copyright (C) Aaron Holtzman + * released under the GPL license. + */ +typedef struct AC3DecodeState { + uint8_t inbuf[4096]; /* input buffer */ + uint8_t *inbuf_ptr; + int frame_size; + int flags; + int channels; + a52_state_t* state; + sample_t* samples; + + /* + * virtual method table + * + * using this function table so the liba52 doesn't + * have to be really linked together with ffmpeg + * and might be linked in runtime - this allows binary + * distribution of ffmpeg library which doens't depend + * on liba52 library - but if user has it installed + * it will be used - user might install such library + * separately + */ + void* handle; + a52_state_t* (*a52_init)(uint32_t mm_accel); + sample_t* (*a52_samples)(a52_state_t * state); + int (*a52_syncinfo)(uint8_t * buf, int * flags, + int * sample_rate, int * bit_rate); + int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags, + sample_t * level, sample_t bias); + void (*a52_dynrng)(a52_state_t * state, + sample_t (* call) (sample_t, void *), void * data); + int (*a52_block)(a52_state_t * state); + void (*a52_free)(a52_state_t * state); + +} AC3DecodeState; + +#ifdef CONFIG_LIBA52BIN +static void* dlsymm(void* handle, const char* symbol) +{ + void* f = dlsym(handle, symbol); + if (!f) + av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol); + return f; +} +#endif + +static int a52_decode_init(AVCodecContext *avctx) +{ + AC3DecodeState *s = avctx->priv_data; + +#ifdef CONFIG_LIBA52BIN + s->handle = dlopen(liba52name, RTLD_LAZY); + if (!s->handle) + { + av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror()); + return -1; + } + s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init"); + s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples"); + s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo"); + s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame"); + s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block"); + s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free"); + if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo + || !s->a52_frame || !s->a52_block || !s->a52_free) + { + dlclose(s->handle); + return -1; + } +#else + s->handle = 0; + s->a52_init = a52_init; + s->a52_samples = a52_samples; + s->a52_syncinfo = a52_syncinfo; + s->a52_frame = a52_frame; + s->a52_block = a52_block; + s->a52_free = a52_free; +#endif + s->state = s->a52_init(0); /* later use CPU flags */ + s->samples = s->a52_samples(s->state); + s->inbuf_ptr = s->inbuf; + s->frame_size = 0; + + return 0; +} + +/**** the following two functions comes from a52dec */ +static inline int blah (int32_t i) +{ + if (i > 0x43c07fff) + return 32767; + else if (i < 0x43bf8000) + return -32768; + return i - 0x43c00000; +} + +static inline void float_to_int (float * _f, int16_t * s16, int nchannels) +{ + int i, j, c; + int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format + + j = 0; + nchannels *= 256; + for (i = 0; i < 256; i++) { + for (c = 0; c < nchannels; c += 256) + s16[j++] = blah (f[i + c]); + } +} + +/**** end */ + +#define HEADER_SIZE 7 + +static int a52_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + AC3DecodeState *s = avctx->priv_data; + uint8_t *buf_ptr; + int flags, i, len; + int sample_rate, bit_rate; + short *out_samples = data; + float level; + static const int ac3_channels[8] = { + 2, 1, 2, 3, 3, 4, 4, 5 + }; + + *data_size= 0; + + buf_ptr = buf; + while (buf_size > 0) { + len = s->inbuf_ptr - s->inbuf; + if (s->frame_size == 0) { + /* no header seen : find one. We need at least 7 bytes to parse it */ + len = HEADER_SIZE - len; + if (len > buf_size) + len = buf_size; + memcpy(s->inbuf_ptr, buf_ptr, len); + buf_ptr += len; + s->inbuf_ptr += len; + buf_size -= len; + if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) { + len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); + if (len == 0) { + /* no sync found : move by one byte (inefficient, but simple!) */ + memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1); + s->inbuf_ptr--; + } else { + s->frame_size = len; + /* update codec info */ + avctx->sample_rate = sample_rate; + s->channels = ac3_channels[s->flags & 7]; + if (s->flags & A52_LFE) + s->channels++; + if (avctx->channels == 0) + /* No specific number of channel requested */ + avctx->channels = s->channels; + else if (s->channels < avctx->channels) { + av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len); + avctx->channels = s->channels; + } + avctx->bit_rate = bit_rate; + } + } + } else if (len < s->frame_size) { + len = s->frame_size - len; + if (len > buf_size) + len = buf_size; + + memcpy(s->inbuf_ptr, buf_ptr, len); + buf_ptr += len; + s->inbuf_ptr += len; + buf_size -= len; + } else { + flags = s->flags; + if (avctx->channels == 1) + flags = A52_MONO; + else if (avctx->channels == 2) + flags = A52_STEREO; + else + flags |= A52_ADJUST_LEVEL; + level = 1; + if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { + fail: + av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); + s->inbuf_ptr = s->inbuf; + s->frame_size = 0; + continue; + } + for (i = 0; i < 6; i++) { + if (s->a52_block(s->state)) + goto fail; + float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); + } + s->inbuf_ptr = s->inbuf; + s->frame_size = 0; + *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); + break; + } + } + return buf_ptr - buf; +} + +static int a52_decode_end(AVCodecContext *avctx) +{ + AC3DecodeState *s = avctx->priv_data; + s->a52_free(s->state); +#ifdef CONFIG_LIBA52BIN + dlclose(s->handle); +#endif + return 0; +} + +AVCodec liba52_decoder = { + "ac3", + CODEC_TYPE_AUDIO, + CODEC_ID_AC3, + sizeof(AC3DecodeState), + a52_decode_init, + NULL, + a52_decode_end, + a52_decode_frame, +}; diff -r daff9ebd2e0b -r c3f2379b80db libamr.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libamr.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,711 @@ +/* + * AMR Audio decoder stub + * Copyright (c) 2003 the ffmpeg project + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + /** @file + * Adaptive Multi-Rate (AMR) Audio decoder stub. + * + * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand + * (AMR-WB) audio encoder/decoder through external reference code from + * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you + * have to download the code separately. Two versions exists: One fixed-point + * and one floating-point. For some reason the float encoder is significantly + * faster at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip + * at MR102). Both float and fixed point are supported for AMR-NB, but only + * float for AMR-WB. + * + * \section AMR-NB + * + * \subsection Float + * The float version (default) can be downloaded from: + * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip + * + * \subsection Fixed-point + * The fixed-point (TS26.073) can be downloaded from: + * http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-600.zip + * + * \subsection Specification + * The specification for AMR-NB can be found in TS 26.071 + * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other + * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. + * + * \section AMR-WB + * + * \subsection Float + * The reference code can be downloaded from: + * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip + * + * \subsection Fixed-point + * If someone wants to use the fixed point version it can be downloaded from: + * http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip. + * + * \subsection Specification + * The specification for AMR-WB can be found in TS 26.171 + * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other + * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm. + * + */ + +#include "avcodec.h" + +#ifdef CONFIG_LIBAMR_NB_FIXED + +#define MMS_IO + +#include "amr/sp_dec.h" +#include "amr/d_homing.h" +#include "amr/typedef.h" +#include "amr/sp_enc.h" +#include "amr/sid_sync.h" +#include "amr/e_homing.h" + +#else +#include +#include +#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"; +static const char *wb_bitrate_unsupported = + "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*/ +typedef struct AMR_bitrates +{ + int rate; + enum Mode mode; +} AMR_bitrates; + +/* Match desired bitrate */ +static int getBitrateMode(int bitrate) +{ + /* make the correspondance between bitrate and mode */ + AMR_bitrates rates[]={ {4750,MR475}, + {5150,MR515}, + {5900,MR59}, + {6700,MR67}, + {7400,MR74}, + {7950,MR795}, + {10200,MR102}, + {12200,MR122}, + }; + int i; + + for(i=0;i<8;i++) + { + if(rates[i].rate==bitrate) + { + return(rates[i].mode); + } + } + /* 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) */ +#define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5) + +typedef struct AMRContext { + int frameCount; + Speech_Decode_FrameState *speech_decoder_state; + enum RXFrameType rx_type; + enum Mode mode; + Word16 reset_flag; + Word16 reset_flag_old; + + int enc_bitrate; + Speech_Encode_FrameState *enstate; + sid_syncState *sidstate; + enum TXFrameType tx_frametype; +} AMRContext; + +static int amr_nb_decode_init(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + s->frameCount=0; + s->speech_decoder_state=NULL; + s->rx_type = (enum RXFrameType)0; + s->mode= (enum Mode)0; + s->reset_flag=0; + s->reset_flag_old=1; + + if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder")) + { + av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n"); + return -1; + } + + amr_decode_fix_avctx(avctx); + + if(avctx->channels > 1) + { + av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); + return -1; + } + + return 0; +} + +static int amr_nb_encode_init(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + s->frameCount=0; + s->speech_decoder_state=NULL; + s->rx_type = (enum RXFrameType)0; + s->mode= (enum Mode)0; + s->reset_flag=0; + s->reset_flag_old=1; + + if(avctx->sample_rate!=8000) + { + av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); + return -1; + } + + if(avctx->channels!=1) + { + av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); + return -1; + } + + avctx->frame_size=160; + avctx->coded_frame= avcodec_alloc_frame(); + + if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate)) + { + av_log(avctx, AV_LOG_ERROR, "Speech_Encode_Frame_init error\n"); + return -1; + } + + if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) + { + av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); + return -1; + } + + return 0; +} + +static int amr_nb_encode_close(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + Speech_Encode_Frame_exit(&s->enstate); + sid_sync_exit (&s->sidstate); + av_freep(&avctx->coded_frame); + return 0; +} + +static int amr_nb_decode_close(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + Speech_Decode_Frame_exit(&s->speech_decoder_state); + return 0; +} + +static int amr_nb_decode_frame(AVCodecContext * avctx, + void *data, int *data_size, + uint8_t * buf, int buf_size) +{ + AMRContext *s = avctx->priv_data; + uint8_t*amrData=buf; + int offset=0; + UWord8 toc, q, ft; + Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */ + Word16 *synth; + UWord8 *packed_bits; + static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}; + int i; + + //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount); + + synth=data; + + toc=amrData[offset]; + /* read rest of the frame based on ToC byte */ + q = (toc >> 2) & 0x01; + ft = (toc >> 3) & 0x0F; + + //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]); + + offset++; + + packed_bits=amrData+offset; + + offset+=packed_size[ft]; + + //Unsort and unpack bits + s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]); + + //We have a new frame + s->frameCount++; + + if (s->rx_type == RX_NO_DATA) + { + s->mode = s->speech_decoder_state->prev_mode; + } + else { + s->speech_decoder_state->prev_mode = s->mode; + } + + /* if homed: check if this frame is another homing frame */ + if (s->reset_flag_old == 1) + { + /* only check until end of first subframe */ + s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode); + } + /* produce encoder homing frame if homed & input=decoder homing frame */ + if ((s->reset_flag != 0) && (s->reset_flag_old != 0)) + { + for (i = 0; i < L_FRAME; i++) + { + synth[i] = EHF_MASK; + } + } + else + { + /* decode frame */ + Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth); + } + + //Each AMR-frame results in 160 16-bit samples + *data_size=160*2; + + /* if not homed: check whether current frame is a homing frame */ + if (s->reset_flag_old == 0) + { + /* check whole frame */ + s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode); + } + /* reset decoder if current frame is a homing frame */ + if (s->reset_flag != 0) + { + Speech_Decode_Frame_reset(s->speech_decoder_state); + } + s->reset_flag_old = s->reset_flag; + + return offset; +} + + +static int amr_nb_encode_frame(AVCodecContext *avctx, + unsigned char *frame/*out*/, int buf_size, void *data/*in*/) +{ + short serial_data[250] = {0}; + AMRContext *s = avctx->priv_data; + int written; + + s->reset_flag = encoder_homing_frame_test(data); + + Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode); + + /* add frame type and mode */ + sid_sync (s->sidstate, s->mode, &s->tx_frametype); + + written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame); + + if (s->reset_flag != 0) + { + Speech_Encode_Frame_reset(s->enstate); + sid_sync_reset(s->sidstate); + } + return written; +} + + +#elif defined(CONFIG_LIBAMR_NB) /* Float point version*/ + +typedef struct AMRContext { + int frameCount; + void * decState; + int *enstate; + int enc_bitrate; +} AMRContext; + +static int amr_nb_decode_init(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + s->frameCount=0; + s->decState=Decoder_Interface_init(); + if(!s->decState) + { + av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n"); + return -1; + } + + amr_decode_fix_avctx(avctx); + + if(avctx->channels > 1) + { + av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n"); + return -1; + } + + return 0; +} + +static int amr_nb_encode_init(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + s->frameCount=0; + + if(avctx->sample_rate!=8000) + { + av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n"); + return -1; + } + + if(avctx->channels!=1) + { + av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); + return -1; + } + + avctx->frame_size=160; + avctx->coded_frame= avcodec_alloc_frame(); + + s->enstate=Encoder_Interface_init(0); + if(!s->enstate) + { + av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n"); + return -1; + } + + if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) + { + av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); + return -1; + } + + return 0; +} + +static int amr_nb_decode_close(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + Decoder_Interface_exit(s->decState); + return 0; +} + +static int amr_nb_encode_close(AVCodecContext * avctx) +{ + AMRContext *s = avctx->priv_data; + + Encoder_Interface_exit(s->enstate); + av_freep(&avctx->coded_frame); + return 0; +} + +static int amr_nb_decode_frame(AVCodecContext * avctx, + void *data, int *data_size, + uint8_t * buf, int buf_size) +{ + AMRContext *s = avctx->priv_data; + uint8_t*amrData=buf; + static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 }; + enum Mode dec_mode; + 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); */ + + dec_mode = (buf[0] >> 3) & 0x000F; + packet_size = block_size[dec_mode]+1; + + if(packet_size > buf_size) { + av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size); + return -1; + } + + s->frameCount++; + /* 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]); */ + /* call decoder */ + Decoder_Interface_Decode(s->decState, amrData, data, 0); + *data_size=160*2; + + return packet_size; +} + +static int amr_nb_encode_frame(AVCodecContext *avctx, + unsigned char *frame/*out*/, int buf_size, void *data/*in*/) +{ + AMRContext *s = avctx->priv_data; + int written; + + if((s->enc_bitrate=getBitrateMode(avctx->bit_rate))<0) + { + av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported); + return -1; + } + + 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; +} + +#endif + +#if defined(CONFIG_LIBAMR_NB) || defined(CONFIG_LIBAMR_NB_FIXED) + +AVCodec libamr_nb_decoder = +{ + "libamr_nb", + CODEC_TYPE_AUDIO, + CODEC_ID_AMR_NB, + sizeof(AMRContext), + amr_nb_decode_init, + NULL, + amr_nb_decode_close, + amr_nb_decode_frame, +}; + +AVCodec libamr_nb_encoder = +{ + "libamr_nb", + CODEC_TYPE_AUDIO, + CODEC_ID_AMR_NB, + sizeof(AMRContext), + amr_nb_encode_init, + amr_nb_encode_frame, + amr_nb_encode_close, + NULL, +}; + +#endif + +/* -----------AMR wideband ------------*/ +#ifdef CONFIG_LIBAMR_WB + +#ifdef _TYPEDEF_H +//To avoid duplicate typedefs from typdef in amr-nb +#define typedef_h +#endif + +#include +#include +#include + +/* Common code for fixed and float version*/ +typedef struct AMRWB_bitrates +{ + int rate; + int mode; +} AMRWB_bitrates; + +static int getWBBitrateMode(int bitrate) +{ + /* make the correspondance between bitrate and mode */ + AMRWB_bitrates rates[]={ {6600,0}, + {8850,1}, + {12650,2}, + {14250,3}, + {15850,4}, + {18250,5}, + {19850,6}, + {23050,7}, + {23850,8}, + }; + int i; + + for(i=0;i<9;i++) + { + if(rates[i].rate==bitrate) + { + return(rates[i].mode); + } + } + /* no bitrate matching, return an error */ + return -1; +} + + +typedef struct AMRWBContext { + int frameCount; + void *state; + int mode; + Word16 allow_dtx; +} AMRWBContext; + +static int amr_wb_encode_init(AVCodecContext * avctx) +{ + AMRWBContext *s = avctx->priv_data; + + s->frameCount=0; + + if(avctx->sample_rate!=16000) + { + av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n"); + return -1; + } + + if(avctx->channels!=1) + { + av_log(avctx, AV_LOG_ERROR, "Only mono supported\n"); + return -1; + } + + if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) + { + av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); + return -1; + } + + avctx->frame_size=320; + avctx->coded_frame= avcodec_alloc_frame(); + + s->state = E_IF_init(); + s->allow_dtx=0; + + return 0; +} + +static int amr_wb_encode_close(AVCodecContext * avctx) +{ + AMRWBContext *s = avctx->priv_data; + + E_IF_exit(s->state); + av_freep(&avctx->coded_frame); + s->frameCount++; + return 0; +} + +static int amr_wb_encode_frame(AVCodecContext *avctx, + unsigned char *frame/*out*/, int buf_size, void *data/*in*/) +{ + AMRWBContext *s = avctx->priv_data; + int size; + + if((s->mode=getWBBitrateMode(avctx->bit_rate))<0) + { + av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported); + return -1; + } + size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx); + return size; +} + +static int amr_wb_decode_init(AVCodecContext * avctx) +{ + AMRWBContext *s = avctx->priv_data; + + s->frameCount=0; + s->state = D_IF_init(); + + amr_decode_fix_avctx(avctx); + + if(avctx->channels > 1) + { + av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n"); + return -1; + } + + return 0; +} + +static int amr_wb_decode_frame(AVCodecContext * avctx, + void *data, int *data_size, + uint8_t * buf, int buf_size) +{ + AMRWBContext *s = avctx->priv_data; + uint8_t*amrData=buf; + int mode; + int packet_size; + + if(buf_size==0) { + /* nothing to do */ + return 0; + } + + mode = (amrData[0] >> 3) & 0x000F; + packet_size = block_size[mode]; + + if(packet_size > buf_size) { + av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1); + return -1; + } + + s->frameCount++; + D_IF_decode( s->state, amrData, data, _good_frame); + *data_size=320*2; + return packet_size; +} + +static int amr_wb_decode_close(AVCodecContext * avctx) +{ + AMRWBContext *s = avctx->priv_data; + + D_IF_exit(s->state); + return 0; +} + +AVCodec libamr_wb_decoder = +{ + "libamr_wb", + CODEC_TYPE_AUDIO, + CODEC_ID_AMR_WB, + sizeof(AMRWBContext), + amr_wb_decode_init, + NULL, + amr_wb_decode_close, + amr_wb_decode_frame, +}; + +AVCodec libamr_wb_encoder = +{ + "libamr_wb", + CODEC_TYPE_AUDIO, + CODEC_ID_AMR_WB, + sizeof(AMRWBContext), + amr_wb_encode_init, + amr_wb_encode_frame, + amr_wb_encode_close, + NULL, +}; + +#endif //CONFIG_LIBAMR_WB diff -r daff9ebd2e0b -r c3f2379b80db libfaac.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfaac.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,154 @@ +/* + * Interface to libfaac for aac encoding + * Copyright (c) 2002 Gildas Bazin + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file faacaudio.c + * Interface to libfaac for aac encoding. + */ + +#include "avcodec.h" +#include + +typedef struct FaacAudioContext { + faacEncHandle faac_handle; +} FaacAudioContext; + +static int Faac_encode_init(AVCodecContext *avctx) +{ + FaacAudioContext *s = avctx->priv_data; + faacEncConfigurationPtr faac_cfg; + unsigned long samples_input, max_bytes_output; + + /* number of channels */ + if (avctx->channels < 1 || avctx->channels > 6) + return -1; + + s->faac_handle = faacEncOpen(avctx->sample_rate, + avctx->channels, + &samples_input, &max_bytes_output); + + /* check faac version */ + faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle); + if (faac_cfg->version != FAAC_CFG_VERSION) { + av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version); + faacEncClose(s->faac_handle); + return -1; + } + + /* put the options in the configuration struct */ + switch(avctx->profile) { + case FF_PROFILE_AAC_MAIN: + faac_cfg->aacObjectType = MAIN; + break; + case FF_PROFILE_UNKNOWN: + case FF_PROFILE_AAC_LOW: + faac_cfg->aacObjectType = LOW; + break; + case FF_PROFILE_AAC_SSR: + faac_cfg->aacObjectType = SSR; + break; + case FF_PROFILE_AAC_LTP: + faac_cfg->aacObjectType = LTP; + break; + default: + av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n"); + faacEncClose(s->faac_handle); + return -1; + } + faac_cfg->mpegVersion = MPEG4; + faac_cfg->useTns = 0; + faac_cfg->allowMidside = 1; + faac_cfg->bitRate = avctx->bit_rate / avctx->channels; + faac_cfg->bandWidth = avctx->cutoff; + if(avctx->flags & CODEC_FLAG_QSCALE) { + faac_cfg->bitRate = 0; + faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA; + } + faac_cfg->outputFormat = 1; + faac_cfg->inputFormat = FAAC_INPUT_16BIT; + + avctx->frame_size = samples_input / avctx->channels; + + avctx->coded_frame= avcodec_alloc_frame(); + avctx->coded_frame->key_frame= 1; + + /* Set decoder specific info */ + avctx->extradata_size = 0; + if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { + + unsigned char *buffer = NULL; + unsigned long decoder_specific_info_size; + + if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer, + &decoder_specific_info_size)) { + avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE); + avctx->extradata_size = decoder_specific_info_size; + memcpy(avctx->extradata, buffer, avctx->extradata_size); + faac_cfg->outputFormat = 0; + } +#undef free + free(buffer); +#define free please_use_av_free + } + + if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) { + av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n"); + return -1; + } + + return 0; +} + +static int Faac_encode_frame(AVCodecContext *avctx, + unsigned char *frame, int buf_size, void *data) +{ + FaacAudioContext *s = avctx->priv_data; + int bytes_written; + + bytes_written = faacEncEncode(s->faac_handle, + data, + avctx->frame_size * avctx->channels, + frame, + buf_size); + + return bytes_written; +} + +static int Faac_encode_close(AVCodecContext *avctx) +{ + FaacAudioContext *s = avctx->priv_data; + + av_freep(&avctx->coded_frame); + av_freep(&avctx->extradata); + + faacEncClose(s->faac_handle); + return 0; +} + +AVCodec libfaac_encoder = { + "libfaac", + CODEC_TYPE_AUDIO, + CODEC_ID_AAC, + sizeof(FaacAudioContext), + Faac_encode_init, + Faac_encode_frame, + Faac_encode_close +}; diff -r daff9ebd2e0b -r c3f2379b80db libfaad.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfaad.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,333 @@ +/* + * Faad decoder + * Copyright (c) 2003 Zdenek Kabelac. + * Copyright (c) 2004 Thomas Raivio. + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file faad.c + * AAC decoder. + * + * still a bit unfinished - but it plays something + */ + +#include "avcodec.h" +#include "faad.h" + +#ifndef FAADAPI +#define FAADAPI +#endif + +/* + * when CONFIG_LIBFAADBIN is defined the libfaad will be opened at runtime + */ +//#undef CONFIG_LIBFAADBIN +//#define CONFIG_LIBFAADBIN + +#ifdef CONFIG_LIBFAADBIN +#include +static const char* libfaadname = "libfaad.so.0"; +#else +#define dlopen(a) +#define dlclose(a) +#endif + +typedef struct { + void* handle; /* dlopen handle */ + void* faac_handle; /* FAAD library handle */ + int sample_size; + int init; + + /* faad calls */ + faacDecHandle FAADAPI (*faacDecOpen)(void); + faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder); +#ifndef FAAD2_VERSION + int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, + faacDecConfigurationPtr config); + int FAADAPI (*faacDecInit)(faacDecHandle hDecoder, + unsigned char *buffer, + unsigned long *samplerate, + unsigned long *channels); + int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer, + unsigned long SizeOfDecoderSpecificInfo, + unsigned long *samplerate, unsigned long *channels); + int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder, + unsigned char *buffer, + unsigned long *bytesconsumed, + short *sample_buffer, + unsigned long *samples); +#else + unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder, + faacDecConfigurationPtr config); + long FAADAPI (*faacDecInit)(faacDecHandle hDecoder, + unsigned char *buffer, + unsigned long buffer_size, + unsigned long *samplerate, + unsigned char *channels); + char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer, + unsigned long SizeOfDecoderSpecificInfo, + unsigned long *samplerate, unsigned char *channels); + void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder, + faacDecFrameInfo *hInfo, + unsigned char *buffer, + unsigned long buffer_size); + char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode); +#endif + + void FAADAPI (*faacDecClose)(faacDecHandle hDecoder); + + +} FAACContext; + +static const unsigned long faac_srates[] = +{ + 96000, 88200, 64000, 48000, 44100, 32000, + 24000, 22050, 16000, 12000, 11025, 8000 +}; + +static int faac_init_mp4(AVCodecContext *avctx) +{ + FAACContext *s = avctx->priv_data; + unsigned long samplerate; +#ifndef FAAD2_VERSION + unsigned long channels; +#else + unsigned char channels; +#endif + int r = 0; + + if (avctx->extradata){ + r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata, + avctx->extradata_size, + &samplerate, &channels); + if (r < 0){ + av_log(avctx, AV_LOG_ERROR, + "faacDecInit2 failed r:%d sr:%ld ch:%ld s:%d\n", + r, samplerate, (long)channels, avctx->extradata_size); + } else { + avctx->sample_rate = samplerate; + avctx->channels = channels; + s->init = 1; + } + } + + return r; +} + +static int faac_decode_frame(AVCodecContext *avctx, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + FAACContext *s = avctx->priv_data; +#ifndef FAAD2_VERSION + unsigned long bytesconsumed; + short *sample_buffer = NULL; + unsigned long samples; + int out; +#else + faacDecFrameInfo frame_info; + void *out; +#endif + if(buf_size == 0) + return 0; +#ifndef FAAD2_VERSION + out = s->faacDecDecode(s->faac_handle, + (unsigned char*)buf, + &bytesconsumed, + data, + &samples); + samples *= s->sample_size; + if (data_size) + *data_size = samples; + return (buf_size < (int)bytesconsumed) + ? buf_size : (int)bytesconsumed; +#else + + if(!s->init){ + unsigned long srate; + unsigned char channels; + int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels); + if(r < 0){ + av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n", + s->faacDecGetErrorMessage(frame_info.error)); + return -1; + } + avctx->sample_rate = srate; + avctx->channels = channels; + s->init = 1; + } + + out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size); + + if (frame_info.error > 0) { + av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n", + s->faacDecGetErrorMessage(frame_info.error)); + return -1; + } + + frame_info.samples *= s->sample_size; + memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one + + if (data_size) + *data_size = frame_info.samples; + + return (buf_size < (int)frame_info.bytesconsumed) + ? buf_size : (int)frame_info.bytesconsumed; +#endif +} + +static int faac_decode_end(AVCodecContext *avctx) +{ + FAACContext *s = avctx->priv_data; + + s->faacDecClose(s->faac_handle); + + dlclose(s->handle); + return 0; +} + +static int faac_decode_init(AVCodecContext *avctx) +{ + FAACContext *s = avctx->priv_data; + faacDecConfigurationPtr faac_cfg; + +#ifdef CONFIG_LIBFAADBIN + const char* err = 0; + + s->handle = dlopen(libfaadname, RTLD_LAZY); + if (!s->handle) + { + av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n", + libfaadname, dlerror()); + return -1; + } +#define dfaac(a, b) \ + do { static const char* n = "faacDec" #a; \ + if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0) + for(;;) { +#else /* !CONFIG_LIBFAADBIN */ +#define dfaac(a, b) s->faacDec ## a = faacDec ## a +#endif /* CONFIG_LIBFAADBIN */ + + // resolve all needed function calls + dfaac(Open, (faacDecHandle FAADAPI (*)(void))); + dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder))); + dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr + FAADAPI (*)(faacDecHandle))); +#ifndef FAAD2_VERSION + dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle, + faacDecConfigurationPtr))); + + dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*, + unsigned long*, unsigned long*))); + dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*, + unsigned long, unsigned long*, + unsigned long*))); + dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*, + unsigned long*, short*, unsigned long*))); +#else + dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle, + faacDecConfigurationPtr))); + dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*, + unsigned long, unsigned long*, unsigned char*))); + dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*, + unsigned long, unsigned long*, + unsigned char*))); + dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*, + unsigned char*, unsigned long))); + dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char))); +#endif +#undef dfacc + +#ifdef CONFIG_LIBFAADBIN + break; + } + if (err) { + dlclose(s->handle); + av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n", + err, libfaadname); + return -1; + } +#endif + + s->faac_handle = s->faacDecOpen(); + if (!s->faac_handle) { + av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n"); + faac_decode_end(avctx); + return -1; + } + + + faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle); + + if (faac_cfg) { + switch (avctx->bits_per_sample) { + case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break; + default: + case 16: +#ifdef FAAD2_VERSION + faac_cfg->outputFormat = FAAD_FMT_16BIT; +#endif + s->sample_size = 2; + break; + case 24: +#ifdef FAAD2_VERSION + faac_cfg->outputFormat = FAAD_FMT_24BIT; +#endif + s->sample_size = 3; + break; + case 32: +#ifdef FAAD2_VERSION + faac_cfg->outputFormat = FAAD_FMT_32BIT; +#endif + s->sample_size = 4; + break; + } + + faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate; + faac_cfg->defObjectType = LC; + } + + s->faacDecSetConfiguration(s->faac_handle, faac_cfg); + + faac_init_mp4(avctx); + + return 0; +} + +#define AAC_CODEC(id, name) \ +AVCodec name ## _decoder = { \ + #name, \ + CODEC_TYPE_AUDIO, \ + id, \ + sizeof(FAACContext), \ + faac_decode_init, \ + NULL, \ + faac_decode_end, \ + faac_decode_frame, \ +} + +// FIXME - raw AAC files - maybe just one entry will be enough +AAC_CODEC(CODEC_ID_AAC, libfaad); +#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0) +// If it's mp4 file - usually embeded into Qt Mov +AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac); +#endif + +#undef AAC_CODEC diff -r daff9ebd2e0b -r c3f2379b80db libmp3lame.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmp3lame.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,220 @@ +/* + * Interface to libmp3lame for mp3 encoding + * Copyright (c) 2002 Lennert Buytenhek + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file mp3lameaudio.c + * Interface to libmp3lame for mp3 encoding. + */ + +#include "avcodec.h" +#include "mpegaudio.h" +#include + +#define BUFFER_SIZE (2*MPA_FRAME_SIZE) +typedef struct Mp3AudioContext { + lame_global_flags *gfp; + int stereo; + uint8_t buffer[BUFFER_SIZE]; + int buffer_index; +} Mp3AudioContext; + +static int MP3lame_encode_init(AVCodecContext *avctx) +{ + Mp3AudioContext *s = avctx->priv_data; + + if (avctx->channels > 2) + return -1; + + s->stereo = avctx->channels > 1 ? 1 : 0; + + if ((s->gfp = lame_init()) == NULL) + goto err; + lame_set_in_samplerate(s->gfp, avctx->sample_rate); + lame_set_out_samplerate(s->gfp, avctx->sample_rate); + lame_set_num_channels(s->gfp, avctx->channels); + /* lame 3.91 dies on quality != 5 */ + lame_set_quality(s->gfp, 5); + /* lame 3.91 doesn't work in mono */ + lame_set_mode(s->gfp, JOINT_STEREO); + lame_set_brate(s->gfp, avctx->bit_rate/1000); + if(avctx->flags & CODEC_FLAG_QSCALE) { + lame_set_brate(s->gfp, 0); + lame_set_VBR(s->gfp, vbr_default); + lame_set_VBR_q(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA); + } + lame_set_bWriteVbrTag(s->gfp,0); + if (lame_init_params(s->gfp) < 0) + goto err_close; + + avctx->frame_size = lame_get_framesize(s->gfp); + + avctx->coded_frame= avcodec_alloc_frame(); + avctx->coded_frame->key_frame= 1; + + return 0; + +err_close: + lame_close(s->gfp); +err: + return -1; +} + +static const int sSampleRates[3] = { + 44100, 48000, 32000 +}; + +static const int sBitRates[2][3][15] = { + { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448}, + { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384}, + { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320} + }, + { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256}, + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}, + { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160} + }, +}; + +static const int sSamplesPerFrame[2][3] = +{ + { 384, 1152, 1152 }, + { 384, 1152, 576 } +}; + +static const int sBitsPerSlot[3] = { + 32, + 8, + 8 +}; + +static int mp3len(void *data, int *samplesPerFrame, int *sampleRate) +{ + uint32_t header = AV_RB32(data); + int layerID = 3 - ((header >> 17) & 0x03); + int bitRateID = ((header >> 12) & 0x0f); + int sampleRateID = ((header >> 10) & 0x03); + int bitsPerSlot = sBitsPerSlot[layerID]; + int isPadded = ((header >> 9) & 0x01); + static int const mode_tab[4]= {2,3,1,0}; + int mode= mode_tab[(header >> 19) & 0x03]; + int mpeg_id= mode>0; + int temp0, temp1, bitRate; + + if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) { + return -1; + } + + if(!samplesPerFrame) samplesPerFrame= &temp0; + if(!sampleRate ) sampleRate = &temp1; + +// *isMono = ((header >> 6) & 0x03) == 0x03; + + *sampleRate = sSampleRates[sampleRateID]>>mode; + bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000; + *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID]; +//av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode); + + return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded; +} + +static int MP3lame_encode_frame(AVCodecContext *avctx, + unsigned char *frame, int buf_size, void *data) +{ + Mp3AudioContext *s = avctx->priv_data; + int len; + int lame_result; + + /* lame 3.91 dies on '1-channel interleaved' data */ + + if(data){ + if (s->stereo) { + lame_result = lame_encode_buffer_interleaved( + s->gfp, + data, + avctx->frame_size, + s->buffer + s->buffer_index, + BUFFER_SIZE - s->buffer_index + ); + } else { + lame_result = lame_encode_buffer( + s->gfp, + data, + data, + avctx->frame_size, + s->buffer + s->buffer_index, + BUFFER_SIZE - s->buffer_index + ); + } + }else{ + lame_result= lame_encode_flush( + s->gfp, + s->buffer + s->buffer_index, + BUFFER_SIZE - s->buffer_index + ); + } + + if(lame_result==-1) { + /* output buffer too small */ + av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index); + return 0; + } + + s->buffer_index += lame_result; + + if(s->buffer_index<4) + return 0; + + len= mp3len(s->buffer, NULL, NULL); +//av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index); + if(len <= s->buffer_index){ + memcpy(frame, s->buffer, len); + s->buffer_index -= len; + + memmove(s->buffer, s->buffer+len, s->buffer_index); + //FIXME fix the audio codec API, so we dont need the memcpy() +/*for(i=0; ipriv_data; + + av_freep(&avctx->coded_frame); + + lame_close(s->gfp); + return 0; +} + + +AVCodec mp3lame_encoder = { + "mp3", + CODEC_TYPE_AUDIO, + CODEC_ID_MP3, + sizeof(Mp3AudioContext), + MP3lame_encode_init, + MP3lame_encode_frame, + MP3lame_encode_close, + .capabilities= CODEC_CAP_DELAY, +}; diff -r daff9ebd2e0b -r c3f2379b80db libvorbis.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libvorbis.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,383 @@ +/* + * copyright (c) 2002 Mark Hills + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file oggvorbis.c + * Ogg Vorbis codec support via libvorbisenc. + * @author Mark Hills + */ + +#include + +#include "avcodec.h" +#include "bytestream.h" + +#undef NDEBUG +#include + +#define OGGVORBIS_FRAME_SIZE 64 + +#define BUFFER_SIZE (1024*64) + +typedef struct OggVorbisContext { + vorbis_info vi ; + vorbis_dsp_state vd ; + vorbis_block vb ; + uint8_t buffer[BUFFER_SIZE]; + int buffer_index; + + /* decoder */ + vorbis_comment vc ; + ogg_packet op; +} OggVorbisContext ; + + +static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) { + double cfreq; + + if(avccontext->flags & CODEC_FLAG_QSCALE) { + /* variable bitrate */ + if(vorbis_encode_setup_vbr(vi, avccontext->channels, + avccontext->sample_rate, + avccontext->global_quality / (float)FF_QP2LAMBDA)) + return -1; + } else { + /* constant bitrate */ + if(vorbis_encode_setup_managed(vi, avccontext->channels, + avccontext->sample_rate, -1, avccontext->bit_rate, -1)) + return -1; + +#ifdef OGGVORBIS_VBR_BY_ESTIMATE + /* variable bitrate by estimate */ + if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL)) + return -1; +#endif + } + + /* cutoff frequency */ + if(avccontext->cutoff > 0) { + cfreq = avccontext->cutoff / 1000.0; + if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)) + return -1; + } + + return vorbis_encode_setup_init(vi); +} + +static int oggvorbis_encode_init(AVCodecContext *avccontext) { + OggVorbisContext *context = avccontext->priv_data ; + ogg_packet header, header_comm, header_code; + uint8_t *p; + unsigned int offset, len; + + vorbis_info_init(&context->vi) ; + if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { + av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ; + return -1 ; + } + vorbis_analysis_init(&context->vd, &context->vi) ; + vorbis_block_init(&context->vd, &context->vb) ; + + vorbis_comment_init(&context->vc); + vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ; + + vorbis_analysis_headerout(&context->vd, &context->vc, &header, + &header_comm, &header_code); + + len = header.bytes + header_comm.bytes + header_code.bytes; + avccontext->extradata_size= 64 + len + len/255; + p = avccontext->extradata= av_mallocz(avccontext->extradata_size); + p[0] = 2; + offset = 1; + offset += av_xiphlacing(&p[offset], header.bytes); + offset += av_xiphlacing(&p[offset], header_comm.bytes); + memcpy(&p[offset], header.packet, header.bytes); + offset += header.bytes; + memcpy(&p[offset], header_comm.packet, header_comm.bytes); + offset += header_comm.bytes; + memcpy(&p[offset], header_code.packet, header_code.bytes); + offset += header_code.bytes; + avccontext->extradata_size = offset; + avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size); + +/* vorbis_block_clear(&context->vb); + vorbis_dsp_clear(&context->vd); + vorbis_info_clear(&context->vi);*/ + vorbis_comment_clear(&context->vc); + + avccontext->frame_size = OGGVORBIS_FRAME_SIZE ; + + avccontext->coded_frame= avcodec_alloc_frame(); + avccontext->coded_frame->key_frame= 1; + + return 0 ; +} + + +static int oggvorbis_encode_frame(AVCodecContext *avccontext, + unsigned char *packets, + int buf_size, void *data) +{ + OggVorbisContext *context = avccontext->priv_data ; + float **buffer ; + ogg_packet op ; + signed short *audio = data ; + int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0; + + buffer = vorbis_analysis_buffer(&context->vd, samples) ; + + if(context->vi.channels == 1) { + for(l = 0 ; l < samples ; l++) + buffer[0][l]=audio[l]/32768.f; + } else { + for(l = 0 ; l < samples ; l++){ + buffer[0][l]=audio[l*2]/32768.f; + buffer[1][l]=audio[l*2+1]/32768.f; + } + } + + vorbis_analysis_wrote(&context->vd, samples) ; + + while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { + vorbis_analysis(&context->vb, NULL); + vorbis_bitrate_addblock(&context->vb) ; + + while(vorbis_bitrate_flushpacket(&context->vd, &op)) { + /* i'd love to say the following line is a hack, but sadly it's + * not, apparently the end of stream decision is in libogg. */ + if(op.bytes==1) + continue; + memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); + context->buffer_index += sizeof(ogg_packet); + memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); + context->buffer_index += op.bytes; +// av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); + } + } + + l=0; + if(context->buffer_index){ + ogg_packet *op2= (ogg_packet*)context->buffer; + op2->packet = context->buffer + sizeof(ogg_packet); + + l= op2->bytes; + avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base); + //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate + + memcpy(packets, op2->packet, l); + context->buffer_index -= l + sizeof(ogg_packet); + memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index); +// av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l); + } + + return l; +} + + +static int oggvorbis_encode_close(AVCodecContext *avccontext) { + OggVorbisContext *context = avccontext->priv_data ; +/* ogg_packet op ; */ + + vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */ + + vorbis_block_clear(&context->vb); + vorbis_dsp_clear(&context->vd); + vorbis_info_clear(&context->vi); + + av_freep(&avccontext->coded_frame); + av_freep(&avccontext->extradata); + + return 0 ; +} + + +AVCodec libvorbis_encoder = { + "libvorbis", + CODEC_TYPE_AUDIO, + CODEC_ID_VORBIS, + sizeof(OggVorbisContext), + oggvorbis_encode_init, + oggvorbis_encode_frame, + oggvorbis_encode_close, + .capabilities= CODEC_CAP_DELAY, +} ; + +static int oggvorbis_decode_init(AVCodecContext *avccontext) { + OggVorbisContext *context = avccontext->priv_data ; + uint8_t *p= avccontext->extradata; + int i, hsizes[3]; + unsigned char *headers[3], *extradata = avccontext->extradata; + + vorbis_info_init(&context->vi) ; + vorbis_comment_init(&context->vc) ; + + if(! avccontext->extradata_size || ! p) { + av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n"); + return -1; + } + + if(p[0] == 0 && p[1] == 30) { + for(i = 0; i < 3; i++){ + hsizes[i] = bytestream_get_be16(&p); + headers[i] = p; + p += hsizes[i]; + } + } else if(*p == 2) { + unsigned int offset = 1; + p++; + for(i=0; i<2; i++) { + hsizes[i] = 0; + while((*p == 0xFF) && (offset < avccontext->extradata_size)) { + hsizes[i] += 0xFF; + offset++; + p++; + } + if(offset >= avccontext->extradata_size - 1) { + av_log(avccontext, AV_LOG_ERROR, + "vorbis header sizes damaged\n"); + return -1; + } + hsizes[i] += *p; + offset++; + p++; + } + hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset; +#if 0 + av_log(avccontext, AV_LOG_DEBUG, + "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n", + hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size); +#endif + headers[0] = extradata + offset; + headers[1] = extradata + offset + hsizes[0]; + headers[2] = extradata + offset + hsizes[0] + hsizes[1]; + } else { + av_log(avccontext, AV_LOG_ERROR, + "vorbis initial header len is wrong: %d\n", *p); + return -1; + } + + for(i=0; i<3; i++){ + context->op.b_o_s= i==0; + context->op.bytes = hsizes[i]; + context->op.packet = headers[i]; + if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){ + av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1); + return -1; + } + } + + avccontext->channels = context->vi.channels; + avccontext->sample_rate = context->vi.rate; + avccontext->time_base= (AVRational){1, avccontext->sample_rate}; + + vorbis_synthesis_init(&context->vd, &context->vi); + vorbis_block_init(&context->vd, &context->vb); + + return 0 ; +} + + +static inline int conv(int samples, float **pcm, char *buf, int channels) { + int i, j, val ; + ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ; + float *mono ; + + for(i = 0 ; i < channels ; i++){ + ptr = &data[i]; + mono = pcm[i] ; + + for(j = 0 ; j < samples ; j++) { + + val = mono[j] * 32767.f; + + if(val > 32767) val = 32767 ; + if(val < -32768) val = -32768 ; + + *ptr = val ; + ptr += channels; + } + } + + return 0 ; +} + + +static int oggvorbis_decode_frame(AVCodecContext *avccontext, + void *data, int *data_size, + uint8_t *buf, int buf_size) +{ + OggVorbisContext *context = avccontext->priv_data ; + float **pcm ; + ogg_packet *op= &context->op; + int samples, total_samples, total_bytes; + + if(!buf_size){ + //FIXME flush + return 0; + } + + op->packet = buf; + op->bytes = buf_size; + +// av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %"PRId64" %"PRId64" %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate); + +/* for(i=0; ibytes; i++) + av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]); + av_log(avccontext, AV_LOG_DEBUG, "\n");*/ + + if(vorbis_synthesis(&context->vb, op) == 0) + vorbis_synthesis_blockin(&context->vd, &context->vb) ; + + total_samples = 0 ; + total_bytes = 0 ; + + while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) { + conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ; + total_bytes += samples * 2 * context->vi.channels ; + total_samples += samples ; + vorbis_synthesis_read(&context->vd, samples) ; + } + + *data_size = total_bytes ; + return buf_size ; +} + + +static int oggvorbis_decode_close(AVCodecContext *avccontext) { + OggVorbisContext *context = avccontext->priv_data ; + + vorbis_info_clear(&context->vi) ; + vorbis_comment_clear(&context->vc) ; + + return 0 ; +} + + +AVCodec libvorbis_decoder = { + "libvorbis", + CODEC_TYPE_AUDIO, + CODEC_ID_VORBIS, + sizeof(OggVorbisContext), + oggvorbis_decode_init, + NULL, + oggvorbis_decode_close, + oggvorbis_decode_frame, + .capabilities= CODEC_CAP_DELAY, +} ; diff -r daff9ebd2e0b -r c3f2379b80db libx264.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libx264.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,300 @@ +/* + * H.264 encoding using the x264 library + * Copyright (C) 2005 Mans Rullgard + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include +#include +#include +#include +#include + +typedef struct X264Context { + x264_param_t params; + x264_t *enc; + x264_picture_t pic; + AVFrame out_pic; +} X264Context; + +static void +X264_log(void *p, int level, const char *fmt, va_list args) +{ + static const int level_map[] = { + [X264_LOG_ERROR] = AV_LOG_ERROR, + [X264_LOG_WARNING] = AV_LOG_ERROR, + [X264_LOG_INFO] = AV_LOG_INFO, + [X264_LOG_DEBUG] = AV_LOG_DEBUG + }; + + if(level < 0 || level > X264_LOG_DEBUG) + return; + + av_vlog(p, level_map[level], fmt, args); +} + + +static int +encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal) +{ + uint8_t *p = buf; + int i; + + for(i = 0; i < nnal; i++){ + int s = x264_nal_encode(p, &size, 1, nals + i); + if(s < 0) + return -1; + p += s; + } + + return p - buf; +} + +static int +X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data) +{ + X264Context *x4 = ctx->priv_data; + AVFrame *frame = data; + x264_nal_t *nal; + int nnal, i; + x264_picture_t pic_out; + + x4->pic.img.i_csp = X264_CSP_I420; + x4->pic.img.i_plane = 3; + + if (frame) { + for(i = 0; i < 3; i++){ + x4->pic.img.plane[i] = frame->data[i]; + x4->pic.img.i_stride[i] = frame->linesize[i]; + } + + x4->pic.i_pts = frame->pts; + x4->pic.i_type = X264_TYPE_AUTO; + } + + if(x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, + &pic_out)) + return -1; + + bufsize = encode_nals(buf, bufsize, nal, nnal); + if(bufsize < 0) + return -1; + + /* FIXME: dts */ + x4->out_pic.pts = pic_out.i_pts; + + switch(pic_out.i_type){ + case X264_TYPE_IDR: + case X264_TYPE_I: + x4->out_pic.pict_type = FF_I_TYPE; + break; + case X264_TYPE_P: + x4->out_pic.pict_type = FF_P_TYPE; + break; + case X264_TYPE_B: + case X264_TYPE_BREF: + x4->out_pic.pict_type = FF_B_TYPE; + break; + } + + x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR; + x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; + + return bufsize; +} + +static int +X264_close(AVCodecContext *avctx) +{ + X264Context *x4 = avctx->priv_data; + + if(x4->enc) + x264_encoder_close(x4->enc); + + return 0; +} + +static int +X264_init(AVCodecContext *avctx) +{ + X264Context *x4 = avctx->priv_data; + + x264_param_default(&x4->params); + + x4->params.pf_log = X264_log; + x4->params.p_log_private = avctx; + + x4->params.i_keyint_max = avctx->gop_size; + x4->params.rc.i_bitrate = avctx->bit_rate / 1000; + x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000; + x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000; + x4->params.rc.b_stat_write = (avctx->flags & CODEC_FLAG_PASS1); + if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1; + else{ + if(avctx->crf){ + x4->params.rc.i_rc_method = X264_RC_CRF; + x4->params.rc.f_rf_constant = avctx->crf; + }else if(avctx->cqp > -1){ + x4->params.rc.i_rc_method = X264_RC_CQP; + x4->params.rc.i_qp_constant = avctx->cqp; + } + } + + // if neither crf nor cqp modes are selected we have to enable the RC + // we do it this way because we cannot check if the bitrate has been set + if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR; + + x4->params.i_bframe = avctx->max_b_frames; + x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC; + x4->params.b_bframe_adaptive = avctx->b_frame_strategy; + x4->params.i_bframe_bias = avctx->bframebias; + x4->params.b_bframe_pyramid = (avctx->flags2 & CODEC_FLAG2_BPYRAMID); + avctx->has_b_frames= (avctx->flags2 & CODEC_FLAG2_BPYRAMID) ? 2 : !!avctx->max_b_frames; + + x4->params.i_keyint_min = avctx->keyint_min; + if(x4->params.i_keyint_min > x4->params.i_keyint_max) + x4->params.i_keyint_min = x4->params.i_keyint_max; + + x4->params.i_scenecut_threshold = avctx->scenechange_threshold; + + x4->params.b_deblocking_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER); + x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha; + x4->params.i_deblocking_filter_beta = avctx->deblockbeta; + + x4->params.rc.i_qp_min = avctx->qmin; + x4->params.rc.i_qp_max = avctx->qmax; + x4->params.rc.i_qp_step = avctx->max_qdiff; + + x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */ + x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */ + x4->params.rc.f_complexity_blur = avctx->complexityblur; + + x4->params.i_frame_reference = avctx->refs; + + x4->params.i_width = avctx->width; + x4->params.i_height = avctx->height; + x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num; + x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den; + x4->params.i_fps_num = avctx->time_base.den; + x4->params.i_fps_den = avctx->time_base.num; + + x4->params.analyse.inter = 0; + if(avctx->partitions){ + if(avctx->partitions & X264_PART_I4X4) + x4->params.analyse.inter |= X264_ANALYSE_I4x4; + if(avctx->partitions & X264_PART_I8X8) + x4->params.analyse.inter |= X264_ANALYSE_I8x8; + if(avctx->partitions & X264_PART_P8X8) + x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16; + if(avctx->partitions & X264_PART_P4X4) + x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8; + if(avctx->partitions & X264_PART_B8X8) + x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16; + } + + x4->params.analyse.i_direct_mv_pred = avctx->directpred; + + x4->params.analyse.b_weighted_bipred = (avctx->flags2 & CODEC_FLAG2_WPRED); + + if(avctx->me_method == ME_EPZS) + x4->params.analyse.i_me_method = X264_ME_DIA; + else if(avctx->me_method == ME_HEX) + x4->params.analyse.i_me_method = X264_ME_HEX; + else if(avctx->me_method == ME_UMH) + x4->params.analyse.i_me_method = X264_ME_UMH; + else if(avctx->me_method == ME_FULL) + x4->params.analyse.i_me_method = X264_ME_ESA; + else x4->params.analyse.i_me_method = X264_ME_HEX; + + x4->params.analyse.i_me_range = avctx->me_range; + x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality; + + x4->params.analyse.b_bidir_me = (avctx->bidir_refine > 0); + x4->params.analyse.b_bframe_rdo = (avctx->flags2 & CODEC_FLAG2_BRDO); + x4->params.analyse.b_mixed_references = + (avctx->flags2 & CODEC_FLAG2_MIXED_REFS); + x4->params.analyse.b_chroma_me = (avctx->me_cmp & FF_CMP_CHROMA); + x4->params.analyse.b_transform_8x8 = (avctx->flags2 & CODEC_FLAG2_8X8DCT); + x4->params.analyse.b_fast_pskip = (avctx->flags2 & CODEC_FLAG2_FASTPSKIP); + + x4->params.analyse.i_trellis = avctx->trellis; + x4->params.analyse.i_noise_reduction = avctx->noise_reduction; + + if(avctx->level > 0) x4->params.i_level_idc = avctx->level; + + x4->params.rc.f_rate_tolerance = + (float)avctx->bit_rate_tolerance/avctx->bit_rate; + + if((avctx->rc_buffer_size != 0) && + (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){ + x4->params.rc.f_vbv_buffer_init = + (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size; + } + else x4->params.rc.f_vbv_buffer_init = 0.9; + + x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor); + x4->params.rc.f_pb_factor = avctx->b_quant_factor; + x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset; + x4->params.rc.psz_rc_eq = avctx->rc_eq; + + x4->params.analyse.b_psnr = (avctx->flags & CODEC_FLAG_PSNR); + x4->params.i_log_level = X264_LOG_DEBUG; + + x4->params.b_aud = (avctx->flags2 & CODEC_FLAG2_AUD); + + x4->params.i_threads = avctx->thread_count; + + if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){ + x4->params.b_repeat_headers = 0; + } + + x4->enc = x264_encoder_open(&x4->params); + if(!x4->enc) + return -1; + + avctx->coded_frame = &x4->out_pic; + + if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){ + x264_nal_t *nal; + int nnal, i, s = 0; + + x264_encoder_headers(x4->enc, &nal, &nnal); + + /* 5 bytes NAL header + worst case escaping */ + for(i = 0; i < nnal; i++) + s += 5 + nal[i].i_payload * 4 / 3; + + avctx->extradata = av_malloc(s); + avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal); + } + + return 0; +} + +AVCodec x264_encoder = { + .name = "h264", + .type = CODEC_TYPE_VIDEO, + .id = CODEC_ID_H264, + .priv_data_size = sizeof(X264Context), + .init = X264_init, + .encode = X264_frame, + .close = X264_close, + .capabilities = CODEC_CAP_DELAY, + .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 } +}; diff -r daff9ebd2e0b -r c3f2379b80db libxvid_internal.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libxvid_internal.h Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,32 @@ +/* + * copyright (C) 2006 Corey Hickey + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef XVID_INTERNAL_H +#define XVID_INTERNAL_H + +/** + * @file libxvid_internal.h + * common functions for use with the XviD wrappers + */ + + +int av_tempfile(char *prefix, char **filename); + +#endif /* XVID_INTERNAL_H */ diff -r daff9ebd2e0b -r c3f2379b80db libxvid_rc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libxvid_rc.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,148 @@ +/* + * xvid Rate control wrapper for lavc video encoders + * + * Copyright (c) 2006 Michael Niedermayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include "avcodec.h" +#include "libxvid_internal.h" +//#include "dsputil.h" +#include "mpegvideo.h" + +#undef NDEBUG +#include + +extern unsigned int xvid_debug; + +int ff_xvid_rate_control_init(MpegEncContext *s){ + char *tmp_name; + int fd, i; + xvid_plg_create_t xvid_plg_create; + xvid_plugin_2pass2_t xvid_2pass2; + +//xvid_debug=-1; + + fd=av_tempfile("xvidrc.", &tmp_name); + if (fd == -1) { + av_log(NULL, AV_LOG_ERROR, "Can't create temporary pass2 file.\n"); + return -1; + } + + for(i=0; irc_context.num_entries; i++){ + static const char *frame_types = " ipbs"; + char tmp[256]; + RateControlEntry *rce; + + rce= &s->rc_context.entry[i]; + + snprintf(tmp, sizeof(tmp), "%c %d %d %d %d %d %d\n", + frame_types[rce->pict_type], (int)lrintf(rce->qscale / FF_QP2LAMBDA), rce->i_count, s->mb_num - rce->i_count - rce->skip_count, + rce->skip_count, (rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits+7)/8, (rce->header_bits+rce->mv_bits+7)/8); + +//av_log(NULL, AV_LOG_ERROR, "%s\n", tmp); + write(fd, tmp, strlen(tmp)); + } + + close(fd); + + memset(&xvid_2pass2, 0, sizeof(xvid_2pass2)); + xvid_2pass2.version= XVID_MAKE_VERSION(1,1,0); + xvid_2pass2.filename= tmp_name; + xvid_2pass2.bitrate= s->avctx->bit_rate; + xvid_2pass2.vbv_size= s->avctx->rc_buffer_size; + xvid_2pass2.vbv_maxrate= s->avctx->rc_max_rate; + xvid_2pass2.vbv_initial= s->avctx->rc_initial_buffer_occupancy; + + memset(&xvid_plg_create, 0, sizeof(xvid_plg_create)); + xvid_plg_create.version= XVID_MAKE_VERSION(1,1,0); + xvid_plg_create.fbase= s->avctx->time_base.den; + xvid_plg_create.fincr= s->avctx->time_base.num; + xvid_plg_create.param= &xvid_2pass2; + + if(xvid_plugin_2pass2(NULL, XVID_PLG_CREATE, &xvid_plg_create, &s->rc_context.non_lavc_opaque)<0){ + av_log(NULL, AV_LOG_ERROR, "xvid_plugin_2pass2 failed\n"); + return -1; + } + return 0; +} + +float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run){ + xvid_plg_data_t xvid_plg_data; + + memset(&xvid_plg_data, 0, sizeof(xvid_plg_data)); + xvid_plg_data.version= XVID_MAKE_VERSION(1,1,0); + xvid_plg_data.width = s->width; + xvid_plg_data.height= s->height; + xvid_plg_data.mb_width = s->mb_width; + xvid_plg_data.mb_height= s->mb_height; + xvid_plg_data.fbase= s->avctx->time_base.den; + xvid_plg_data.fincr= s->avctx->time_base.num; + xvid_plg_data.min_quant[0]= s->avctx->qmin; + xvid_plg_data.min_quant[1]= s->avctx->qmin; + xvid_plg_data.min_quant[2]= s->avctx->qmin; //FIXME i/b factor & offset + xvid_plg_data.max_quant[0]= s->avctx->qmax; + xvid_plg_data.max_quant[1]= s->avctx->qmax; + xvid_plg_data.max_quant[2]= s->avctx->qmax; //FIXME i/b factor & offset + xvid_plg_data.bquant_offset = 0; // 100 * s->avctx->b_quant_offset; + xvid_plg_data.bquant_ratio = 100; // * s->avctx->b_quant_factor; + +#if 0 + xvid_plg_data.stats.hlength= X +#endif + + if(!s->rc_context.dry_run_qscale){ + if(s->picture_number){ + xvid_plg_data.length= + xvid_plg_data.stats.length= (s->frame_bits + 7)/8; + xvid_plg_data.frame_num= s->rc_context.last_picture_number; + xvid_plg_data.quant= s->qscale; + + xvid_plg_data.type= s->last_pict_type; + if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_AFTER, &xvid_plg_data, NULL)){ + av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_AFTER, ...) FAILED\n"); + return -1; + } + } + s->rc_context.last_picture_number= + xvid_plg_data.frame_num= s->picture_number; + xvid_plg_data.quant= 0; + if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_BEFORE, &xvid_plg_data, NULL)){ + av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_BEFORE, ...) FAILED\n"); + return -1; + } + s->rc_context.dry_run_qscale= xvid_plg_data.quant; + } + xvid_plg_data.quant= s->rc_context.dry_run_qscale; + if(!dry_run) + s->rc_context.dry_run_qscale= 0; + + if(s->pict_type == B_TYPE) //FIXME this is not exactly identical to xvid + return xvid_plg_data.quant * FF_QP2LAMBDA * s->avctx->b_quant_factor + s->avctx->b_quant_offset; + else + return xvid_plg_data.quant * FF_QP2LAMBDA; +} + +void ff_xvid_rate_control_uninit(MpegEncContext *s){ + xvid_plg_destroy_t xvid_plg_destroy; + + xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_DESTROY, &xvid_plg_destroy, NULL); +} + diff -r daff9ebd2e0b -r c3f2379b80db libxvidff.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libxvidff.c Wed Jun 06 00:14:18 2007 +0000 @@ -0,0 +1,767 @@ +/* + * Interface to xvidcore for mpeg4 encoding + * Copyright (c) 2004 Adam Thayer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file xvidmpeg4.c + * Interface to xvidcore for MPEG-4 compliant encoding. + * @author Adam Thayer (krevnik@comcast.net) + */ + +#include +#include +#include "avcodec.h" +#include "libxvid_internal.h" + +/** + * Buffer management macros. + */ +#define BUFFER_SIZE 1024 +#define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x)) +#define BUFFER_CAT(x) (&((x)[strlen(x)])) + +/* For PPC Use */ +#if HAVE_ALTIVEC==1 +extern int has_altivec(void); +#endif + +/** + * Structure for the private XviD context. + * This stores all the private context for the codec. + */ +typedef struct xvid_context { + void *encoder_handle; /** Handle for XviD Encoder */ + int xsize, ysize; /** Frame size */ + int vop_flags; /** VOP flags for XviD Encoder */ + int vol_flags; /** VOL flags for XviD Encoder */ + int me_flags; /** Motion Estimation flags */ + int qscale; /** Do we use constant scale? */ + int quicktime_format; /** Are we in a QT-based format? */ + AVFrame encoded_picture; /** Encoded frame information */ + char *twopassbuffer; /** Character buffer for two-pass */ + char *old_twopassbuffer; /** Old character buffer (two-pass) */ + char *twopassfile; /** second pass temp file name */ + unsigned char *intra_matrix; /** P-Frame Quant Matrix */ + unsigned char *inter_matrix; /** I-Frame Quant Matrix */ +} xvid_context_t; + +/** + * Structure for the private first-pass plugin. + */ +typedef struct xvid_ff_pass1 { + int version; /** XviD version */ + xvid_context_t *context; /** Pointer to private context */ +} xvid_ff_pass1_t; + +/* Prototypes - See function implementation for details */ +int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len); +int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2); +void xvid_correct_framerate(AVCodecContext *avctx); + +/** + * Creates the private context for the encoder. + * All buffers are allocated, settings are loaded from the user, + * and the encoder context created. + * + * @param avctx AVCodecContext pointer to context + * @return Returns 0 on success, -1 on failure + */ +int ff_xvid_encode_init(AVCodecContext *avctx) { + int xerr, i; + int xvid_flags = avctx->flags; + xvid_context_t *x = avctx->priv_data; + uint16_t *intra, *inter; + int fd; + + xvid_plugin_single_t single; + xvid_ff_pass1_t rc2pass1; + xvid_plugin_2pass2_t rc2pass2; + xvid_gbl_init_t xvid_gbl_init; + xvid_enc_create_t xvid_enc_create; + xvid_enc_plugin_t plugins[7]; + + /* Bring in VOP flags from ffmpeg command-line */ + x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */ + if( xvid_flags & CODEC_FLAG_4MV ) + x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */ + if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT) + x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */ + if( xvid_flags & CODEC_FLAG_AC_PRED ) + x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */ + if( xvid_flags & CODEC_FLAG_GRAY ) + x->vop_flags |= XVID_VOP_GREYSCALE; + + /* Decide which ME quality setting to use */ + x->me_flags = 0; + switch( avctx->me_method ) { + case ME_FULL: /* Quality 6 */ + x->me_flags |= XVID_ME_EXTSEARCH16 + | XVID_ME_EXTSEARCH8; + + case ME_EPZS: /* Quality 4 */ + x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 + | XVID_ME_HALFPELREFINE8 + | XVID_ME_CHROMA_PVOP + | XVID_ME_CHROMA_BVOP; + + case ME_LOG: /* Quality 2 */ + case ME_PHODS: + case ME_X1: + x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 + | XVID_ME_HALFPELREFINE16; + + case ME_ZERO: /* Quality 0 */ + default: + break; + } + + /* Decide how we should decide blocks */ + switch( avctx->mb_decision ) { + case 2: + x->vop_flags |= XVID_VOP_MODEDECISION_RD; + x->me_flags |= XVID_ME_HALFPELREFINE8_RD + | XVID_ME_QUARTERPELREFINE8_RD + | XVID_ME_EXTSEARCH_RD + | XVID_ME_CHECKPREDICTION_RD; + case 1: + if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) ) + x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD; + x->me_flags |= XVID_ME_HALFPELREFINE16_RD + | XVID_ME_QUARTERPELREFINE16_RD; + + default: + break; + } + + /* Bring in VOL flags from ffmpeg command-line */ + x->vol_flags = 0; + if( xvid_flags & CODEC_FLAG_GMC ) { + x->vol_flags |= XVID_VOL_GMC; + x->me_flags |= XVID_ME_GME_REFINE; + } + if( xvid_flags & CODEC_FLAG_QPEL ) { + x->vol_flags |= XVID_VOL_QUARTERPEL; + x->me_flags |= XVID_ME_QUARTERPELREFINE16; + if( x->vop_flags & XVID_VOP_INTER4V ) + x->me_flags |= XVID_ME_QUARTERPELREFINE8; + } + + memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); + xvid_gbl_init.version = XVID_VERSION; + xvid_gbl_init.debug = 0; + +#ifdef ARCH_POWERPC + /* XviD's PPC support is borked, use libavcodec to detect */ +#if HAVE_ALTIVEC==1 + if( has_altivec() ) { + xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC; + } else +#endif + xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; +#else + /* XviD can detect on x86 */ + xvid_gbl_init.cpu_flags = 0; +#endif + + /* Initialize */ + xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); + + /* Create the encoder reference */ + memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); + xvid_enc_create.version = XVID_VERSION; + + /* Store the desired frame size */ + xvid_enc_create.width = x->xsize = avctx->width; + xvid_enc_create.height = x->ysize = avctx->height; + + /* XviD can determine the proper profile to use */ + /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ + + /* We don't use zones or threads */ + xvid_enc_create.zones = NULL; + xvid_enc_create.num_zones = 0; + xvid_enc_create.num_threads = 0; + + xvid_enc_create.plugins = plugins; + xvid_enc_create.num_plugins = 0; + + /* Initialize Buffers */ + x->twopassbuffer = NULL; + x->old_twopassbuffer = NULL; + x->twopassfile = NULL; + + if( xvid_flags & CODEC_FLAG_PASS1 ) { + memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t)); + rc2pass1.version = XVID_VERSION; + rc2pass1.context = x; + x->twopassbuffer = av_malloc(BUFFER_SIZE); + x->old_twopassbuffer = av_malloc(BUFFER_SIZE); + if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) { + av_log(avctx, AV_LOG_ERROR, + "XviD: Cannot allocate 2-pass log buffers\n"); + return -1; + } + x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; + + plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; + plugins[xvid_enc_create.num_plugins].param = &rc2pass1; + xvid_enc_create.num_plugins++; + } else if( xvid_flags & CODEC_FLAG_PASS2 ) { + memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); + rc2pass2.version = XVID_VERSION; + rc2pass2.bitrate = avctx->bit_rate; + + fd = av_tempfile("xvidff.", &(x->twopassfile)); + if( fd == -1 ) { + av_log(avctx, AV_LOG_ERROR, + "XviD: Cannot write 2-pass pipe\n"); + return -1; + } + + if( avctx->stats_in == NULL ) { + av_log(avctx, AV_LOG_ERROR, + "XviD: No 2-pass information loaded for second pass\n"); + return -1; + } + + if( strlen(avctx->stats_in) > + write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) { + close(fd); + av_log(avctx, AV_LOG_ERROR, + "XviD: Cannot write to 2-pass pipe\n"); + return -1; + } + + close(fd); + rc2pass2.filename = x->twopassfile; + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; + plugins[xvid_enc_create.num_plugins].param = &rc2pass2; + xvid_enc_create.num_plugins++; + } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) { + /* Single Pass Bitrate Control! */ + memset(&single, 0, sizeof(xvid_plugin_single_t)); + single.version = XVID_VERSION; + single.bitrate = avctx->bit_rate; + + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; + plugins[xvid_enc_create.num_plugins].param = &single; + xvid_enc_create.num_plugins++; + } + + /* Luminance Masking */ + if( 0.0 != avctx->lumi_masking ) { + plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; + plugins[xvid_enc_create.num_plugins].param = NULL; + xvid_enc_create.num_plugins++; + } + + /* Frame Rate and Key Frames */ + xvid_correct_framerate(avctx); + xvid_enc_create.fincr = avctx->time_base.num; + xvid_enc_create.fbase = avctx->time_base.den; + if( avctx->gop_size > 0 ) + xvid_enc_create.max_key_interval = avctx->gop_size; + else + xvid_enc_create.max_key_interval = 240; /* XviD's best default */ + + /* Quants */ + if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; + else x->qscale = 0; + + xvid_enc_create.min_quant[0] = avctx->qmin; + xvid_enc_create.min_quant[1] = avctx->qmin; + xvid_enc_create.min_quant[2] = avctx->qmin; + xvid_enc_create.max_quant[0] = avctx->qmax; + xvid_enc_create.max_quant[1] = avctx->qmax; + xvid_enc_create.max_quant[2] = avctx->qmax; + + /* Quant Matrices */ + x->intra_matrix = x->inter_matrix = NULL; + if( avctx->mpeg_quant ) + x->vol_flags |= XVID_VOL_MPEGQUANT; + if( (avctx->intra_matrix || avctx->inter_matrix) ) { + x->vol_flags |= XVID_VOL_MPEGQUANT; + + if( avctx->intra_matrix ) { + intra = avctx->intra_matrix; + x->intra_matrix = av_malloc(sizeof(unsigned char) * 64); + } else + intra = NULL; + if( avctx->inter_matrix ) { + inter = avctx->inter_matrix; + x->inter_matrix = av_malloc(sizeof(unsigned char) * 64); + } else + inter = NULL; + + for( i = 0; i < 64; i++ ) { + if( intra ) + x->intra_matrix[i] = (unsigned char)intra[i]; + if( inter ) + x->inter_matrix[i] = (unsigned char)inter[i]; + } + } + + /* Misc Settings */ + xvid_enc_create.frame_drop_ratio = 0; + xvid_enc_create.global = 0; + if( xvid_flags & CODEC_FLAG_CLOSED_GOP ) + xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; + + /* Determines which codec mode we are operating in */ + avctx->extradata = NULL; + avctx->extradata_size = 0; + if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) { + /* In this case, we are claiming to be MPEG4 */ + x->quicktime_format = 1; + avctx->codec_id = CODEC_ID_MPEG4; + } else { + /* We are claiming to be XviD */ + x->quicktime_format = 0; + if(!avctx->codec_tag) + avctx->codec_tag = ff_get_fourcc("xvid"); + } + + /* Bframes */ + xvid_enc_create.max_bframes = avctx->max_b_frames; + xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset; + xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor; + if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; + + /* Create encoder context */ + xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); + if( xerr ) { + av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n"); + return -1; + } + + x->encoder_handle = xvid_enc_create.handle; + avctx->coded_frame = &x->encoded_picture; + + return 0; +} + +/** + * Encodes a single frame. + * + * @param avctx AVCodecContext pointer to context + * @param frame Pointer to encoded frame buffer + * @param buf_size Size of encoded frame buffer + * @param data Pointer to AVFrame of unencoded frame + * @return Returns 0 on success, -1 on failure + */ +int ff_xvid_encode_frame(AVCodecContext *avctx, + unsigned char *frame, int buf_size, void *data) { + int xerr, i; + char *tmp; + xvid_context_t *x = avctx->priv_data; + AVFrame *picture = data; + AVFrame *p = &(x->encoded_picture); + + xvid_enc_frame_t xvid_enc_frame; + xvid_enc_stats_t xvid_enc_stats; + + /* Start setting up the frame */ + memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); + xvid_enc_frame.version = XVID_VERSION; + memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); + xvid_enc_stats.version = XVID_VERSION; + *p = *picture; + + /* Let XviD know where to put the frame. */ + xvid_enc_frame.bitstream = frame; + xvid_enc_frame.length = buf_size; + + /* Initialize input image fields */ + if( avctx->pix_fmt != PIX_FMT_YUV420P ) { + av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n"); + return -1; + } + + xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */ + + for( i = 0; i < 4; i++ ) { + xvid_enc_frame.input.plane[i] = picture->data[i]; + xvid_enc_frame.input.stride[i] = picture->linesize[i]; + } + + /* Encoder Flags */ + xvid_enc_frame.vop_flags = x->vop_flags; + xvid_enc_frame.vol_flags = x->vol_flags; + xvid_enc_frame.motion = x->me_flags; + xvid_enc_frame.type = XVID_TYPE_AUTO; + + /* Quant Setting */ + if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; + else xvid_enc_frame.quant = 0; + + /* Matrices */ + xvid_enc_frame.quant_intra_matrix = x->intra_matrix; + xvid_enc_frame.quant_inter_matrix = x->inter_matrix; + + /* Encode */ + xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE, + &xvid_enc_frame, &xvid_enc_stats); + + /* Two-pass log buffer swapping */ + avctx->stats_out = NULL; + if( x->twopassbuffer ) { + tmp = x->old_twopassbuffer; + x->old_twopassbuffer = x->twopassbuffer; + x->twopassbuffer = tmp; + x->twopassbuffer[0] = 0; + if( x->old_twopassbuffer[0] != 0 ) { + avctx->stats_out = x->old_twopassbuffer; + } + } + + if( 0 <= xerr ) { + p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA; + if( xvid_enc_stats.type == XVID_TYPE_PVOP ) + p->pict_type = FF_P_TYPE; + else if( xvid_enc_stats.type == XVID_TYPE_BVOP ) + p->pict_type = FF_B_TYPE; + else if( xvid_enc_stats.type == XVID_TYPE_SVOP ) + p->pict_type = FF_S_TYPE; + else + p->pict_type = FF_I_TYPE; + if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) { + p->key_frame = 1; + if( x->quicktime_format ) + return xvid_strip_vol_header(avctx, frame, + xvid_enc_stats.hlength, xerr); + } else + p->key_frame = 0; + + return xerr; + } else { + av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr); + return -1; + } +} + +/** + * Destroys the private context for the encoder. + * All buffers are freed, and the XviD encoder context is destroyed. + * + * @param avctx AVCodecContext pointer to context + * @return Returns 0, success guaranteed + */ +int ff_xvid_encode_close(AVCodecContext *avctx) { + xvid_context_t *x = avctx->priv_data; + + xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL); + + if( avctx->extradata != NULL ) + av_free(avctx->extradata); + if( x->twopassbuffer != NULL ) { + av_free(x->twopassbuffer); + av_free(x->old_twopassbuffer); + } + if( x->twopassfile != NULL ) + av_free(x->twopassfile); + if( x->intra_matrix != NULL ) + av_free(x->intra_matrix); + if( x->inter_matrix != NULL ) + av_free(x->inter_matrix); + + return 0; +} + +/** + * Routine to create a global VO/VOL header for MP4 container. + * What we do here is extract the header from the XviD bitstream + * as it is encoded. We also strip the repeated headers from the + * bitstream when a global header is requested for MPEG-4 ISO + * compliance. + * + * @param avctx AVCodecContext pointer to context + * @param frame Pointer to encoded frame data + * @param header_len Length of header to search + * @param frame_len Length of encoded frame data + * @return Returns new length of frame data + */ +int xvid_strip_vol_header(AVCodecContext *avctx, + unsigned char *frame, + unsigned int header_len, + unsigned int frame_len) { + int vo_len = 0, i; + + for( i = 0; i < header_len - 3; i++ ) { + if( frame[i] == 0x00 && + frame[i+1] == 0x00 && + frame[i+2] == 0x01 && + frame[i+3] == 0xB6 ) { + vo_len = i; + break; + } + } + + if( vo_len > 0 ) { + /* We need to store the header, so extract it */ + if( avctx->extradata == NULL ) { + avctx->extradata = av_malloc(vo_len); + memcpy(avctx->extradata, frame, vo_len); + avctx->extradata_size = vo_len; + } + /* Less dangerous now, memmove properly copies the two + chunks of overlapping data */ + memmove(frame, &(frame[vo_len]), frame_len - vo_len); + return frame_len - vo_len; + } else + return frame_len; +} + +/** + * Routine to correct a possibly erroneous framerate being fed to us. + * XviD currently chokes on framerates where the ticks per frame is + * extremely large. This function works to correct problems in this area + * by estimating a new framerate and taking the simpler fraction of + * the two presented. + * + * @param avctx Context that contains the framerate to correct. + */ +void xvid_correct_framerate(AVCodecContext *avctx) { + int frate, fbase; + int est_frate, est_fbase; + int gcd; + float est_fps, fps; + + frate = avctx->time_base.den; + fbase = avctx->time_base.num; + + gcd = ff_gcd(frate, fbase); + if( gcd > 1 ) { + frate /= gcd; + fbase /= gcd; + } + + if( frate <= 65000 && fbase <= 65000 ) { + avctx->time_base.den = frate; + avctx->time_base.num = fbase; + return; + } + + fps = (float)frate / (float)fbase; + est_fps = roundf(fps * 1000.0) / 1000.0; + + est_frate = (int)est_fps; + if( est_fps > (int)est_fps ) { + est_frate = (est_frate + 1) * 1000; + est_fbase = (int)roundf((float)est_frate / est_fps); + } else + est_fbase = 1; + + gcd = ff_gcd(est_frate, est_fbase); + if( gcd > 1 ) { + est_frate /= gcd; + est_fbase /= gcd; + } + + if( fbase > est_fbase ) { + avctx->time_base.den = est_frate; + avctx->time_base.num = est_fbase; + av_log(avctx, AV_LOG_DEBUG, + "XviD: framerate re-estimated: %.2f, %.3f%% correction\n", + est_fps, (((est_fps - fps)/fps) * 100.0)); + } else { + avctx->time_base.den = frate; + avctx->time_base.num = fbase; + } +} + +/* + * XviD 2-Pass Kludge Section + * + * XviD's default 2-pass doesn't allow us to create data as we need to, so + * this section spends time replacing the first pass plugin so we can write + * statistic information as libavcodec requests in. We have another kludge + * that allows us to pass data to the second pass in XviD without a custom + * rate-control plugin. + */ + +/** + * Initializes the two-pass plugin and context. + * + * @param param Input construction parameter structure + * @param handle Private context handle + * @return Returns XVID_ERR_xxxx on failure, or 0 on success. + */ +static int xvid_ff_2pass_create(xvid_plg_create_t * param, + void ** handle) { + xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param; + char *log = x->context->twopassbuffer; + + /* Do a quick bounds check */ + if( log == NULL ) + return XVID_ERR_FAIL; + + /* We use snprintf() */ + /* This is because we can safely prevent a buffer overflow */ + log[0] = 0; + snprintf(log, BUFFER_REMAINING(log), + "# ffmpeg 2-pass log file, using xvid codec\n"); + snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), + "# Do not modify. libxvidcore version: %d.%d.%d\n\n", + XVID_VERSION_MAJOR(XVID_VERSION), + XVID_VERSION_MINOR(XVID_VERSION), + XVID_VERSION_PATCH(XVID_VERSION)); + + *handle = x->context; + return 0; +} + +/** + * Destroys the two-pass plugin context. + * + * @param ref Context pointer for the plugin + * @param param Destrooy context + * @return Returns 0, success guaranteed + */ +static int xvid_ff_2pass_destroy(xvid_context_t *ref, + xvid_plg_destroy_t *param) { + /* Currently cannot think of anything to do on destruction */ + /* Still, the framework should be here for reference/use */ + if( ref->twopassbuffer != NULL ) + ref->twopassbuffer[0] = 0; + return 0; +} + +/** + * Enables fast encode mode during the first pass. + * + * @param ref Context pointer for the plugin + * @param param Frame data + * @return Returns 0, success guaranteed + */ +static int xvid_ff_2pass_before(xvid_context_t *ref, + xvid_plg_data_t *param) { + int motion_remove; + int motion_replacements; + int vop_remove; + + /* Nothing to do here, result is changed too much */ + if( param->zone && param->zone->mode == XVID_ZONE_QUANT ) + return 0; + + /* We can implement a 'turbo' first pass mode here */ + param->quant = 2; + + /* Init values */ + motion_remove = ~XVID_ME_CHROMA_PVOP & + ~XVID_ME_CHROMA_BVOP & + ~XVID_ME_EXTSEARCH16 & + ~XVID_ME_ADVANCEDDIAMOND16; + motion_replacements = XVID_ME_FAST_MODEINTERPOLATE | + XVID_ME_SKIP_DELTASEARCH | + XVID_ME_FASTREFINE16 | + XVID_ME_BFRAME_EARLYSTOP; + vop_remove = ~XVID_VOP_MODEDECISION_RD & + ~XVID_VOP_FAST_MODEDECISION_RD & + ~XVID_VOP_TRELLISQUANT & + ~XVID_VOP_INTER4V & + ~XVID_VOP_HQACPRED; + + param->vol_flags &= ~XVID_VOL_GMC; + param->vop_flags &= vop_remove; + param->motion_flags &= motion_remove; + param->motion_flags |= motion_replacements; + + return 0; +} + +/** + * Captures statistic data and writes it during first pass. + * + * @param ref Context pointer for the plugin + * @param param Statistic data + * @return Returns XVID_ERR_xxxx on failure, or 0 on success + */ +static int xvid_ff_2pass_after(xvid_context_t *ref, + xvid_plg_data_t *param) { + char *log = ref->twopassbuffer; + char *frame_types = " ipbs"; + char frame_type; + + /* Quick bounds check */ + if( log == NULL ) + return XVID_ERR_FAIL; + + /* Convert the type given to us into a character */ + if( param->type < 5 && param->type > 0 ) { + frame_type = frame_types[param->type]; + } else { + return XVID_ERR_FAIL; + } + + snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), + "%c %d %d %d %d %d %d\n", + frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks, + param->stats.ublks, param->stats.length, param->stats.hlength); + + return 0; +} + +/** + * Dispatch function for our custom plugin. + * This handles the dispatch for the XviD plugin. It passes data + * on to other functions for actual processing. + * + * @param ref Context pointer for the plugin + * @param cmd The task given for us to complete + * @param p1 First parameter (varies) + * @param p2 Second parameter (varies) + * @return Returns XVID_ERR_xxxx on failure, or 0 on success + */ +int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) { + switch( cmd ) { + case XVID_PLG_INFO: + case XVID_PLG_FRAME: + return 0; + + case XVID_PLG_BEFORE: + return xvid_ff_2pass_before(ref, p1); + + case XVID_PLG_CREATE: + return xvid_ff_2pass_create(p1, p2); + + case XVID_PLG_AFTER: + return xvid_ff_2pass_after(ref, p1); + + case XVID_PLG_DESTROY: + return xvid_ff_2pass_destroy(ref, p1); + + default: + return XVID_ERR_FAIL; + } +} + +/** + * XviD codec definition for libavcodec. + */ +AVCodec xvid_encoder = { + "xvid", + CODEC_TYPE_VIDEO, + CODEC_ID_XVID, + sizeof(xvid_context_t), + ff_xvid_encode_init, + ff_xvid_encode_frame, + ff_xvid_encode_close, + .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, +}; diff -r daff9ebd2e0b -r c3f2379b80db mp3lameaudio.c --- a/mp3lameaudio.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,220 +0,0 @@ -/* - * Interface to libmp3lame for mp3 encoding - * Copyright (c) 2002 Lennert Buytenhek - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file mp3lameaudio.c - * Interface to libmp3lame for mp3 encoding. - */ - -#include "avcodec.h" -#include "mpegaudio.h" -#include - -#define BUFFER_SIZE (2*MPA_FRAME_SIZE) -typedef struct Mp3AudioContext { - lame_global_flags *gfp; - int stereo; - uint8_t buffer[BUFFER_SIZE]; - int buffer_index; -} Mp3AudioContext; - -static int MP3lame_encode_init(AVCodecContext *avctx) -{ - Mp3AudioContext *s = avctx->priv_data; - - if (avctx->channels > 2) - return -1; - - s->stereo = avctx->channels > 1 ? 1 : 0; - - if ((s->gfp = lame_init()) == NULL) - goto err; - lame_set_in_samplerate(s->gfp, avctx->sample_rate); - lame_set_out_samplerate(s->gfp, avctx->sample_rate); - lame_set_num_channels(s->gfp, avctx->channels); - /* lame 3.91 dies on quality != 5 */ - lame_set_quality(s->gfp, 5); - /* lame 3.91 doesn't work in mono */ - lame_set_mode(s->gfp, JOINT_STEREO); - lame_set_brate(s->gfp, avctx->bit_rate/1000); - if(avctx->flags & CODEC_FLAG_QSCALE) { - lame_set_brate(s->gfp, 0); - lame_set_VBR(s->gfp, vbr_default); - lame_set_VBR_q(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA); - } - lame_set_bWriteVbrTag(s->gfp,0); - if (lame_init_params(s->gfp) < 0) - goto err_close; - - avctx->frame_size = lame_get_framesize(s->gfp); - - avctx->coded_frame= avcodec_alloc_frame(); - avctx->coded_frame->key_frame= 1; - - return 0; - -err_close: - lame_close(s->gfp); -err: - return -1; -} - -static const int sSampleRates[3] = { - 44100, 48000, 32000 -}; - -static const int sBitRates[2][3][15] = { - { { 0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448}, - { 0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384}, - { 0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320} - }, - { { 0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256}, - { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}, - { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160} - }, -}; - -static const int sSamplesPerFrame[2][3] = -{ - { 384, 1152, 1152 }, - { 384, 1152, 576 } -}; - -static const int sBitsPerSlot[3] = { - 32, - 8, - 8 -}; - -static int mp3len(void *data, int *samplesPerFrame, int *sampleRate) -{ - uint32_t header = AV_RB32(data); - int layerID = 3 - ((header >> 17) & 0x03); - int bitRateID = ((header >> 12) & 0x0f); - int sampleRateID = ((header >> 10) & 0x03); - int bitsPerSlot = sBitsPerSlot[layerID]; - int isPadded = ((header >> 9) & 0x01); - static int const mode_tab[4]= {2,3,1,0}; - int mode= mode_tab[(header >> 19) & 0x03]; - int mpeg_id= mode>0; - int temp0, temp1, bitRate; - - if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) { - return -1; - } - - if(!samplesPerFrame) samplesPerFrame= &temp0; - if(!sampleRate ) sampleRate = &temp1; - -// *isMono = ((header >> 6) & 0x03) == 0x03; - - *sampleRate = sSampleRates[sampleRateID]>>mode; - bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000; - *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID]; -//av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode); - - return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded; -} - -static int MP3lame_encode_frame(AVCodecContext *avctx, - unsigned char *frame, int buf_size, void *data) -{ - Mp3AudioContext *s = avctx->priv_data; - int len; - int lame_result; - - /* lame 3.91 dies on '1-channel interleaved' data */ - - if(data){ - if (s->stereo) { - lame_result = lame_encode_buffer_interleaved( - s->gfp, - data, - avctx->frame_size, - s->buffer + s->buffer_index, - BUFFER_SIZE - s->buffer_index - ); - } else { - lame_result = lame_encode_buffer( - s->gfp, - data, - data, - avctx->frame_size, - s->buffer + s->buffer_index, - BUFFER_SIZE - s->buffer_index - ); - } - }else{ - lame_result= lame_encode_flush( - s->gfp, - s->buffer + s->buffer_index, - BUFFER_SIZE - s->buffer_index - ); - } - - if(lame_result==-1) { - /* output buffer too small */ - av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index); - return 0; - } - - s->buffer_index += lame_result; - - if(s->buffer_index<4) - return 0; - - len= mp3len(s->buffer, NULL, NULL); -//av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index); - if(len <= s->buffer_index){ - memcpy(frame, s->buffer, len); - s->buffer_index -= len; - - memmove(s->buffer, s->buffer+len, s->buffer_index); - //FIXME fix the audio codec API, so we dont need the memcpy() -/*for(i=0; ipriv_data; - - av_freep(&avctx->coded_frame); - - lame_close(s->gfp); - return 0; -} - - -AVCodec mp3lame_encoder = { - "mp3", - CODEC_TYPE_AUDIO, - CODEC_ID_MP3, - sizeof(Mp3AudioContext), - MP3lame_encode_init, - MP3lame_encode_frame, - MP3lame_encode_close, - .capabilities= CODEC_CAP_DELAY, -}; diff -r daff9ebd2e0b -r c3f2379b80db oggvorbis.c --- a/oggvorbis.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,383 +0,0 @@ -/* - * copyright (c) 2002 Mark Hills - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file oggvorbis.c - * Ogg Vorbis codec support via libvorbisenc. - * @author Mark Hills - */ - -#include - -#include "avcodec.h" -#include "bytestream.h" - -#undef NDEBUG -#include - -#define OGGVORBIS_FRAME_SIZE 64 - -#define BUFFER_SIZE (1024*64) - -typedef struct OggVorbisContext { - vorbis_info vi ; - vorbis_dsp_state vd ; - vorbis_block vb ; - uint8_t buffer[BUFFER_SIZE]; - int buffer_index; - - /* decoder */ - vorbis_comment vc ; - ogg_packet op; -} OggVorbisContext ; - - -static int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) { - double cfreq; - - if(avccontext->flags & CODEC_FLAG_QSCALE) { - /* variable bitrate */ - if(vorbis_encode_setup_vbr(vi, avccontext->channels, - avccontext->sample_rate, - avccontext->global_quality / (float)FF_QP2LAMBDA)) - return -1; - } else { - /* constant bitrate */ - if(vorbis_encode_setup_managed(vi, avccontext->channels, - avccontext->sample_rate, -1, avccontext->bit_rate, -1)) - return -1; - -#ifdef OGGVORBIS_VBR_BY_ESTIMATE - /* variable bitrate by estimate */ - if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE_AVG, NULL)) - return -1; -#endif - } - - /* cutoff frequency */ - if(avccontext->cutoff > 0) { - cfreq = avccontext->cutoff / 1000.0; - if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq)) - return -1; - } - - return vorbis_encode_setup_init(vi); -} - -static int oggvorbis_encode_init(AVCodecContext *avccontext) { - OggVorbisContext *context = avccontext->priv_data ; - ogg_packet header, header_comm, header_code; - uint8_t *p; - unsigned int offset, len; - - vorbis_info_init(&context->vi) ; - if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) { - av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed") ; - return -1 ; - } - vorbis_analysis_init(&context->vd, &context->vi) ; - vorbis_block_init(&context->vd, &context->vb) ; - - vorbis_comment_init(&context->vc); - vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ; - - vorbis_analysis_headerout(&context->vd, &context->vc, &header, - &header_comm, &header_code); - - len = header.bytes + header_comm.bytes + header_code.bytes; - avccontext->extradata_size= 64 + len + len/255; - p = avccontext->extradata= av_mallocz(avccontext->extradata_size); - p[0] = 2; - offset = 1; - offset += av_xiphlacing(&p[offset], header.bytes); - offset += av_xiphlacing(&p[offset], header_comm.bytes); - memcpy(&p[offset], header.packet, header.bytes); - offset += header.bytes; - memcpy(&p[offset], header_comm.packet, header_comm.bytes); - offset += header_comm.bytes; - memcpy(&p[offset], header_code.packet, header_code.bytes); - offset += header_code.bytes; - avccontext->extradata_size = offset; - avccontext->extradata= av_realloc(avccontext->extradata, avccontext->extradata_size); - -/* vorbis_block_clear(&context->vb); - vorbis_dsp_clear(&context->vd); - vorbis_info_clear(&context->vi);*/ - vorbis_comment_clear(&context->vc); - - avccontext->frame_size = OGGVORBIS_FRAME_SIZE ; - - avccontext->coded_frame= avcodec_alloc_frame(); - avccontext->coded_frame->key_frame= 1; - - return 0 ; -} - - -static int oggvorbis_encode_frame(AVCodecContext *avccontext, - unsigned char *packets, - int buf_size, void *data) -{ - OggVorbisContext *context = avccontext->priv_data ; - float **buffer ; - ogg_packet op ; - signed short *audio = data ; - int l, samples = data ? OGGVORBIS_FRAME_SIZE : 0; - - buffer = vorbis_analysis_buffer(&context->vd, samples) ; - - if(context->vi.channels == 1) { - for(l = 0 ; l < samples ; l++) - buffer[0][l]=audio[l]/32768.f; - } else { - for(l = 0 ; l < samples ; l++){ - buffer[0][l]=audio[l*2]/32768.f; - buffer[1][l]=audio[l*2+1]/32768.f; - } - } - - vorbis_analysis_wrote(&context->vd, samples) ; - - while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) { - vorbis_analysis(&context->vb, NULL); - vorbis_bitrate_addblock(&context->vb) ; - - while(vorbis_bitrate_flushpacket(&context->vd, &op)) { - /* i'd love to say the following line is a hack, but sadly it's - * not, apparently the end of stream decision is in libogg. */ - if(op.bytes==1) - continue; - memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet)); - context->buffer_index += sizeof(ogg_packet); - memcpy(context->buffer + context->buffer_index, op.packet, op.bytes); - context->buffer_index += op.bytes; -// av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes); - } - } - - l=0; - if(context->buffer_index){ - ogg_packet *op2= (ogg_packet*)context->buffer; - op2->packet = context->buffer + sizeof(ogg_packet); - - l= op2->bytes; - avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base); - //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate - - memcpy(packets, op2->packet, l); - context->buffer_index -= l + sizeof(ogg_packet); - memcpy(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index); -// av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l); - } - - return l; -} - - -static int oggvorbis_encode_close(AVCodecContext *avccontext) { - OggVorbisContext *context = avccontext->priv_data ; -/* ogg_packet op ; */ - - vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */ - - vorbis_block_clear(&context->vb); - vorbis_dsp_clear(&context->vd); - vorbis_info_clear(&context->vi); - - av_freep(&avccontext->coded_frame); - av_freep(&avccontext->extradata); - - return 0 ; -} - - -AVCodec libvorbis_encoder = { - "libvorbis", - CODEC_TYPE_AUDIO, - CODEC_ID_VORBIS, - sizeof(OggVorbisContext), - oggvorbis_encode_init, - oggvorbis_encode_frame, - oggvorbis_encode_close, - .capabilities= CODEC_CAP_DELAY, -} ; - -static int oggvorbis_decode_init(AVCodecContext *avccontext) { - OggVorbisContext *context = avccontext->priv_data ; - uint8_t *p= avccontext->extradata; - int i, hsizes[3]; - unsigned char *headers[3], *extradata = avccontext->extradata; - - vorbis_info_init(&context->vi) ; - vorbis_comment_init(&context->vc) ; - - if(! avccontext->extradata_size || ! p) { - av_log(avccontext, AV_LOG_ERROR, "vorbis extradata absent\n"); - return -1; - } - - if(p[0] == 0 && p[1] == 30) { - for(i = 0; i < 3; i++){ - hsizes[i] = bytestream_get_be16(&p); - headers[i] = p; - p += hsizes[i]; - } - } else if(*p == 2) { - unsigned int offset = 1; - p++; - for(i=0; i<2; i++) { - hsizes[i] = 0; - while((*p == 0xFF) && (offset < avccontext->extradata_size)) { - hsizes[i] += 0xFF; - offset++; - p++; - } - if(offset >= avccontext->extradata_size - 1) { - av_log(avccontext, AV_LOG_ERROR, - "vorbis header sizes damaged\n"); - return -1; - } - hsizes[i] += *p; - offset++; - p++; - } - hsizes[2] = avccontext->extradata_size - hsizes[0]-hsizes[1]-offset; -#if 0 - av_log(avccontext, AV_LOG_DEBUG, - "vorbis header sizes: %d, %d, %d, / extradata_len is %d \n", - hsizes[0], hsizes[1], hsizes[2], avccontext->extradata_size); -#endif - headers[0] = extradata + offset; - headers[1] = extradata + offset + hsizes[0]; - headers[2] = extradata + offset + hsizes[0] + hsizes[1]; - } else { - av_log(avccontext, AV_LOG_ERROR, - "vorbis initial header len is wrong: %d\n", *p); - return -1; - } - - for(i=0; i<3; i++){ - context->op.b_o_s= i==0; - context->op.bytes = hsizes[i]; - context->op.packet = headers[i]; - if(vorbis_synthesis_headerin(&context->vi, &context->vc, &context->op)<0){ - av_log(avccontext, AV_LOG_ERROR, "%d. vorbis header damaged\n", i+1); - return -1; - } - } - - avccontext->channels = context->vi.channels; - avccontext->sample_rate = context->vi.rate; - avccontext->time_base= (AVRational){1, avccontext->sample_rate}; - - vorbis_synthesis_init(&context->vd, &context->vi); - vorbis_block_init(&context->vd, &context->vb); - - return 0 ; -} - - -static inline int conv(int samples, float **pcm, char *buf, int channels) { - int i, j, val ; - ogg_int16_t *ptr, *data = (ogg_int16_t*)buf ; - float *mono ; - - for(i = 0 ; i < channels ; i++){ - ptr = &data[i]; - mono = pcm[i] ; - - for(j = 0 ; j < samples ; j++) { - - val = mono[j] * 32767.f; - - if(val > 32767) val = 32767 ; - if(val < -32768) val = -32768 ; - - *ptr = val ; - ptr += channels; - } - } - - return 0 ; -} - - -static int oggvorbis_decode_frame(AVCodecContext *avccontext, - void *data, int *data_size, - uint8_t *buf, int buf_size) -{ - OggVorbisContext *context = avccontext->priv_data ; - float **pcm ; - ogg_packet *op= &context->op; - int samples, total_samples, total_bytes; - - if(!buf_size){ - //FIXME flush - return 0; - } - - op->packet = buf; - op->bytes = buf_size; - -// av_log(avccontext, AV_LOG_DEBUG, "%d %d %d %"PRId64" %"PRId64" %d %d\n", op->bytes, op->b_o_s, op->e_o_s, op->granulepos, op->packetno, buf_size, context->vi.rate); - -/* for(i=0; ibytes; i++) - av_log(avccontext, AV_LOG_DEBUG, "%02X ", op->packet[i]); - av_log(avccontext, AV_LOG_DEBUG, "\n");*/ - - if(vorbis_synthesis(&context->vb, op) == 0) - vorbis_synthesis_blockin(&context->vd, &context->vb) ; - - total_samples = 0 ; - total_bytes = 0 ; - - while((samples = vorbis_synthesis_pcmout(&context->vd, &pcm)) > 0) { - conv(samples, pcm, (char*)data + total_bytes, context->vi.channels) ; - total_bytes += samples * 2 * context->vi.channels ; - total_samples += samples ; - vorbis_synthesis_read(&context->vd, samples) ; - } - - *data_size = total_bytes ; - return buf_size ; -} - - -static int oggvorbis_decode_close(AVCodecContext *avccontext) { - OggVorbisContext *context = avccontext->priv_data ; - - vorbis_info_clear(&context->vi) ; - vorbis_comment_clear(&context->vc) ; - - return 0 ; -} - - -AVCodec libvorbis_decoder = { - "libvorbis", - CODEC_TYPE_AUDIO, - CODEC_ID_VORBIS, - sizeof(OggVorbisContext), - oggvorbis_decode_init, - NULL, - oggvorbis_decode_close, - oggvorbis_decode_frame, - .capabilities= CODEC_CAP_DELAY, -} ; diff -r daff9ebd2e0b -r c3f2379b80db x264.c --- a/x264.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,300 +0,0 @@ -/* - * H.264 encoding using the x264 library - * Copyright (C) 2005 Mans Rullgard - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "avcodec.h" -#include -#include -#include -#include -#include - -typedef struct X264Context { - x264_param_t params; - x264_t *enc; - x264_picture_t pic; - AVFrame out_pic; -} X264Context; - -static void -X264_log(void *p, int level, const char *fmt, va_list args) -{ - static const int level_map[] = { - [X264_LOG_ERROR] = AV_LOG_ERROR, - [X264_LOG_WARNING] = AV_LOG_ERROR, - [X264_LOG_INFO] = AV_LOG_INFO, - [X264_LOG_DEBUG] = AV_LOG_DEBUG - }; - - if(level < 0 || level > X264_LOG_DEBUG) - return; - - av_vlog(p, level_map[level], fmt, args); -} - - -static int -encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal) -{ - uint8_t *p = buf; - int i; - - for(i = 0; i < nnal; i++){ - int s = x264_nal_encode(p, &size, 1, nals + i); - if(s < 0) - return -1; - p += s; - } - - return p - buf; -} - -static int -X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data) -{ - X264Context *x4 = ctx->priv_data; - AVFrame *frame = data; - x264_nal_t *nal; - int nnal, i; - x264_picture_t pic_out; - - x4->pic.img.i_csp = X264_CSP_I420; - x4->pic.img.i_plane = 3; - - if (frame) { - for(i = 0; i < 3; i++){ - x4->pic.img.plane[i] = frame->data[i]; - x4->pic.img.i_stride[i] = frame->linesize[i]; - } - - x4->pic.i_pts = frame->pts; - x4->pic.i_type = X264_TYPE_AUTO; - } - - if(x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, - &pic_out)) - return -1; - - bufsize = encode_nals(buf, bufsize, nal, nnal); - if(bufsize < 0) - return -1; - - /* FIXME: dts */ - x4->out_pic.pts = pic_out.i_pts; - - switch(pic_out.i_type){ - case X264_TYPE_IDR: - case X264_TYPE_I: - x4->out_pic.pict_type = FF_I_TYPE; - break; - case X264_TYPE_P: - x4->out_pic.pict_type = FF_P_TYPE; - break; - case X264_TYPE_B: - case X264_TYPE_BREF: - x4->out_pic.pict_type = FF_B_TYPE; - break; - } - - x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR; - x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; - - return bufsize; -} - -static int -X264_close(AVCodecContext *avctx) -{ - X264Context *x4 = avctx->priv_data; - - if(x4->enc) - x264_encoder_close(x4->enc); - - return 0; -} - -static int -X264_init(AVCodecContext *avctx) -{ - X264Context *x4 = avctx->priv_data; - - x264_param_default(&x4->params); - - x4->params.pf_log = X264_log; - x4->params.p_log_private = avctx; - - x4->params.i_keyint_max = avctx->gop_size; - x4->params.rc.i_bitrate = avctx->bit_rate / 1000; - x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000; - x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000; - x4->params.rc.b_stat_write = (avctx->flags & CODEC_FLAG_PASS1); - if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1; - else{ - if(avctx->crf){ - x4->params.rc.i_rc_method = X264_RC_CRF; - x4->params.rc.f_rf_constant = avctx->crf; - }else if(avctx->cqp > -1){ - x4->params.rc.i_rc_method = X264_RC_CQP; - x4->params.rc.i_qp_constant = avctx->cqp; - } - } - - // if neither crf nor cqp modes are selected we have to enable the RC - // we do it this way because we cannot check if the bitrate has been set - if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR; - - x4->params.i_bframe = avctx->max_b_frames; - x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC; - x4->params.b_bframe_adaptive = avctx->b_frame_strategy; - x4->params.i_bframe_bias = avctx->bframebias; - x4->params.b_bframe_pyramid = (avctx->flags2 & CODEC_FLAG2_BPYRAMID); - avctx->has_b_frames= (avctx->flags2 & CODEC_FLAG2_BPYRAMID) ? 2 : !!avctx->max_b_frames; - - x4->params.i_keyint_min = avctx->keyint_min; - if(x4->params.i_keyint_min > x4->params.i_keyint_max) - x4->params.i_keyint_min = x4->params.i_keyint_max; - - x4->params.i_scenecut_threshold = avctx->scenechange_threshold; - - x4->params.b_deblocking_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER); - x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha; - x4->params.i_deblocking_filter_beta = avctx->deblockbeta; - - x4->params.rc.i_qp_min = avctx->qmin; - x4->params.rc.i_qp_max = avctx->qmax; - x4->params.rc.i_qp_step = avctx->max_qdiff; - - x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */ - x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */ - x4->params.rc.f_complexity_blur = avctx->complexityblur; - - x4->params.i_frame_reference = avctx->refs; - - x4->params.i_width = avctx->width; - x4->params.i_height = avctx->height; - x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num; - x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den; - x4->params.i_fps_num = avctx->time_base.den; - x4->params.i_fps_den = avctx->time_base.num; - - x4->params.analyse.inter = 0; - if(avctx->partitions){ - if(avctx->partitions & X264_PART_I4X4) - x4->params.analyse.inter |= X264_ANALYSE_I4x4; - if(avctx->partitions & X264_PART_I8X8) - x4->params.analyse.inter |= X264_ANALYSE_I8x8; - if(avctx->partitions & X264_PART_P8X8) - x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16; - if(avctx->partitions & X264_PART_P4X4) - x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8; - if(avctx->partitions & X264_PART_B8X8) - x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16; - } - - x4->params.analyse.i_direct_mv_pred = avctx->directpred; - - x4->params.analyse.b_weighted_bipred = (avctx->flags2 & CODEC_FLAG2_WPRED); - - if(avctx->me_method == ME_EPZS) - x4->params.analyse.i_me_method = X264_ME_DIA; - else if(avctx->me_method == ME_HEX) - x4->params.analyse.i_me_method = X264_ME_HEX; - else if(avctx->me_method == ME_UMH) - x4->params.analyse.i_me_method = X264_ME_UMH; - else if(avctx->me_method == ME_FULL) - x4->params.analyse.i_me_method = X264_ME_ESA; - else x4->params.analyse.i_me_method = X264_ME_HEX; - - x4->params.analyse.i_me_range = avctx->me_range; - x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality; - - x4->params.analyse.b_bidir_me = (avctx->bidir_refine > 0); - x4->params.analyse.b_bframe_rdo = (avctx->flags2 & CODEC_FLAG2_BRDO); - x4->params.analyse.b_mixed_references = - (avctx->flags2 & CODEC_FLAG2_MIXED_REFS); - x4->params.analyse.b_chroma_me = (avctx->me_cmp & FF_CMP_CHROMA); - x4->params.analyse.b_transform_8x8 = (avctx->flags2 & CODEC_FLAG2_8X8DCT); - x4->params.analyse.b_fast_pskip = (avctx->flags2 & CODEC_FLAG2_FASTPSKIP); - - x4->params.analyse.i_trellis = avctx->trellis; - x4->params.analyse.i_noise_reduction = avctx->noise_reduction; - - if(avctx->level > 0) x4->params.i_level_idc = avctx->level; - - x4->params.rc.f_rate_tolerance = - (float)avctx->bit_rate_tolerance/avctx->bit_rate; - - if((avctx->rc_buffer_size != 0) && - (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){ - x4->params.rc.f_vbv_buffer_init = - (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size; - } - else x4->params.rc.f_vbv_buffer_init = 0.9; - - x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor); - x4->params.rc.f_pb_factor = avctx->b_quant_factor; - x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset; - x4->params.rc.psz_rc_eq = avctx->rc_eq; - - x4->params.analyse.b_psnr = (avctx->flags & CODEC_FLAG_PSNR); - x4->params.i_log_level = X264_LOG_DEBUG; - - x4->params.b_aud = (avctx->flags2 & CODEC_FLAG2_AUD); - - x4->params.i_threads = avctx->thread_count; - - if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){ - x4->params.b_repeat_headers = 0; - } - - x4->enc = x264_encoder_open(&x4->params); - if(!x4->enc) - return -1; - - avctx->coded_frame = &x4->out_pic; - - if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){ - x264_nal_t *nal; - int nnal, i, s = 0; - - x264_encoder_headers(x4->enc, &nal, &nnal); - - /* 5 bytes NAL header + worst case escaping */ - for(i = 0; i < nnal; i++) - s += 5 + nal[i].i_payload * 4 / 3; - - avctx->extradata = av_malloc(s); - avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal); - } - - return 0; -} - -AVCodec x264_encoder = { - .name = "h264", - .type = CODEC_TYPE_VIDEO, - .id = CODEC_ID_H264, - .priv_data_size = sizeof(X264Context), - .init = X264_init, - .encode = X264_frame, - .close = X264_close, - .capabilities = CODEC_CAP_DELAY, - .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 } -}; diff -r daff9ebd2e0b -r c3f2379b80db xvid_internal.h --- a/xvid_internal.h Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -/* - * copyright (C) 2006 Corey Hickey - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef XVID_INTERNAL_H -#define XVID_INTERNAL_H - -/** - * @file xvid_internal.h - * common functions for use with the XviD wrappers - */ - - -int av_tempfile(char *prefix, char **filename); - -#endif /* XVID_INTERNAL_H */ diff -r daff9ebd2e0b -r c3f2379b80db xvid_rc.c --- a/xvid_rc.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,148 +0,0 @@ -/* - * xvid Rate control wrapper for lavc video encoders - * - * Copyright (c) 2006 Michael Niedermayer - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include -#include -#include "avcodec.h" -#include "xvid_internal.h" -//#include "dsputil.h" -#include "mpegvideo.h" - -#undef NDEBUG -#include - -extern unsigned int xvid_debug; - -int ff_xvid_rate_control_init(MpegEncContext *s){ - char *tmp_name; - int fd, i; - xvid_plg_create_t xvid_plg_create; - xvid_plugin_2pass2_t xvid_2pass2; - -//xvid_debug=-1; - - fd=av_tempfile("xvidrc.", &tmp_name); - if (fd == -1) { - av_log(NULL, AV_LOG_ERROR, "Can't create temporary pass2 file.\n"); - return -1; - } - - for(i=0; irc_context.num_entries; i++){ - static const char *frame_types = " ipbs"; - char tmp[256]; - RateControlEntry *rce; - - rce= &s->rc_context.entry[i]; - - snprintf(tmp, sizeof(tmp), "%c %d %d %d %d %d %d\n", - frame_types[rce->pict_type], (int)lrintf(rce->qscale / FF_QP2LAMBDA), rce->i_count, s->mb_num - rce->i_count - rce->skip_count, - rce->skip_count, (rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits+7)/8, (rce->header_bits+rce->mv_bits+7)/8); - -//av_log(NULL, AV_LOG_ERROR, "%s\n", tmp); - write(fd, tmp, strlen(tmp)); - } - - close(fd); - - memset(&xvid_2pass2, 0, sizeof(xvid_2pass2)); - xvid_2pass2.version= XVID_MAKE_VERSION(1,1,0); - xvid_2pass2.filename= tmp_name; - xvid_2pass2.bitrate= s->avctx->bit_rate; - xvid_2pass2.vbv_size= s->avctx->rc_buffer_size; - xvid_2pass2.vbv_maxrate= s->avctx->rc_max_rate; - xvid_2pass2.vbv_initial= s->avctx->rc_initial_buffer_occupancy; - - memset(&xvid_plg_create, 0, sizeof(xvid_plg_create)); - xvid_plg_create.version= XVID_MAKE_VERSION(1,1,0); - xvid_plg_create.fbase= s->avctx->time_base.den; - xvid_plg_create.fincr= s->avctx->time_base.num; - xvid_plg_create.param= &xvid_2pass2; - - if(xvid_plugin_2pass2(NULL, XVID_PLG_CREATE, &xvid_plg_create, &s->rc_context.non_lavc_opaque)<0){ - av_log(NULL, AV_LOG_ERROR, "xvid_plugin_2pass2 failed\n"); - return -1; - } - return 0; -} - -float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run){ - xvid_plg_data_t xvid_plg_data; - - memset(&xvid_plg_data, 0, sizeof(xvid_plg_data)); - xvid_plg_data.version= XVID_MAKE_VERSION(1,1,0); - xvid_plg_data.width = s->width; - xvid_plg_data.height= s->height; - xvid_plg_data.mb_width = s->mb_width; - xvid_plg_data.mb_height= s->mb_height; - xvid_plg_data.fbase= s->avctx->time_base.den; - xvid_plg_data.fincr= s->avctx->time_base.num; - xvid_plg_data.min_quant[0]= s->avctx->qmin; - xvid_plg_data.min_quant[1]= s->avctx->qmin; - xvid_plg_data.min_quant[2]= s->avctx->qmin; //FIXME i/b factor & offset - xvid_plg_data.max_quant[0]= s->avctx->qmax; - xvid_plg_data.max_quant[1]= s->avctx->qmax; - xvid_plg_data.max_quant[2]= s->avctx->qmax; //FIXME i/b factor & offset - xvid_plg_data.bquant_offset = 0; // 100 * s->avctx->b_quant_offset; - xvid_plg_data.bquant_ratio = 100; // * s->avctx->b_quant_factor; - -#if 0 - xvid_plg_data.stats.hlength= X -#endif - - if(!s->rc_context.dry_run_qscale){ - if(s->picture_number){ - xvid_plg_data.length= - xvid_plg_data.stats.length= (s->frame_bits + 7)/8; - xvid_plg_data.frame_num= s->rc_context.last_picture_number; - xvid_plg_data.quant= s->qscale; - - xvid_plg_data.type= s->last_pict_type; - if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_AFTER, &xvid_plg_data, NULL)){ - av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_AFTER, ...) FAILED\n"); - return -1; - } - } - s->rc_context.last_picture_number= - xvid_plg_data.frame_num= s->picture_number; - xvid_plg_data.quant= 0; - if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_BEFORE, &xvid_plg_data, NULL)){ - av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_BEFORE, ...) FAILED\n"); - return -1; - } - s->rc_context.dry_run_qscale= xvid_plg_data.quant; - } - xvid_plg_data.quant= s->rc_context.dry_run_qscale; - if(!dry_run) - s->rc_context.dry_run_qscale= 0; - - if(s->pict_type == B_TYPE) //FIXME this is not exactly identical to xvid - return xvid_plg_data.quant * FF_QP2LAMBDA * s->avctx->b_quant_factor + s->avctx->b_quant_offset; - else - return xvid_plg_data.quant * FF_QP2LAMBDA; -} - -void ff_xvid_rate_control_uninit(MpegEncContext *s){ - xvid_plg_destroy_t xvid_plg_destroy; - - xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_DESTROY, &xvid_plg_destroy, NULL); -} - diff -r daff9ebd2e0b -r c3f2379b80db xvidff.c --- a/xvidff.c Tue Jun 05 14:18:13 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,767 +0,0 @@ -/* - * Interface to xvidcore for mpeg4 encoding - * Copyright (c) 2004 Adam Thayer - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file xvidmpeg4.c - * Interface to xvidcore for MPEG-4 compliant encoding. - * @author Adam Thayer (krevnik@comcast.net) - */ - -#include -#include -#include "avcodec.h" -#include "xvid_internal.h" - -/** - * Buffer management macros. - */ -#define BUFFER_SIZE 1024 -#define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x)) -#define BUFFER_CAT(x) (&((x)[strlen(x)])) - -/* For PPC Use */ -#if HAVE_ALTIVEC==1 -extern int has_altivec(void); -#endif - -/** - * Structure for the private XviD context. - * This stores all the private context for the codec. - */ -typedef struct xvid_context { - void *encoder_handle; /** Handle for XviD Encoder */ - int xsize, ysize; /** Frame size */ - int vop_flags; /** VOP flags for XviD Encoder */ - int vol_flags; /** VOL flags for XviD Encoder */ - int me_flags; /** Motion Estimation flags */ - int qscale; /** Do we use constant scale? */ - int quicktime_format; /** Are we in a QT-based format? */ - AVFrame encoded_picture; /** Encoded frame information */ - char *twopassbuffer; /** Character buffer for two-pass */ - char *old_twopassbuffer; /** Old character buffer (two-pass) */ - char *twopassfile; /** second pass temp file name */ - unsigned char *intra_matrix; /** P-Frame Quant Matrix */ - unsigned char *inter_matrix; /** I-Frame Quant Matrix */ -} xvid_context_t; - -/** - * Structure for the private first-pass plugin. - */ -typedef struct xvid_ff_pass1 { - int version; /** XviD version */ - xvid_context_t *context; /** Pointer to private context */ -} xvid_ff_pass1_t; - -/* Prototypes - See function implementation for details */ -int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len); -int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2); -void xvid_correct_framerate(AVCodecContext *avctx); - -/** - * Creates the private context for the encoder. - * All buffers are allocated, settings are loaded from the user, - * and the encoder context created. - * - * @param avctx AVCodecContext pointer to context - * @return Returns 0 on success, -1 on failure - */ -int ff_xvid_encode_init(AVCodecContext *avctx) { - int xerr, i; - int xvid_flags = avctx->flags; - xvid_context_t *x = avctx->priv_data; - uint16_t *intra, *inter; - int fd; - - xvid_plugin_single_t single; - xvid_ff_pass1_t rc2pass1; - xvid_plugin_2pass2_t rc2pass2; - xvid_gbl_init_t xvid_gbl_init; - xvid_enc_create_t xvid_enc_create; - xvid_enc_plugin_t plugins[7]; - - /* Bring in VOP flags from ffmpeg command-line */ - x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */ - if( xvid_flags & CODEC_FLAG_4MV ) - x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */ - if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT) - x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */ - if( xvid_flags & CODEC_FLAG_AC_PRED ) - x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */ - if( xvid_flags & CODEC_FLAG_GRAY ) - x->vop_flags |= XVID_VOP_GREYSCALE; - - /* Decide which ME quality setting to use */ - x->me_flags = 0; - switch( avctx->me_method ) { - case ME_FULL: /* Quality 6 */ - x->me_flags |= XVID_ME_EXTSEARCH16 - | XVID_ME_EXTSEARCH8; - - case ME_EPZS: /* Quality 4 */ - x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 - | XVID_ME_HALFPELREFINE8 - | XVID_ME_CHROMA_PVOP - | XVID_ME_CHROMA_BVOP; - - case ME_LOG: /* Quality 2 */ - case ME_PHODS: - case ME_X1: - x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 - | XVID_ME_HALFPELREFINE16; - - case ME_ZERO: /* Quality 0 */ - default: - break; - } - - /* Decide how we should decide blocks */ - switch( avctx->mb_decision ) { - case 2: - x->vop_flags |= XVID_VOP_MODEDECISION_RD; - x->me_flags |= XVID_ME_HALFPELREFINE8_RD - | XVID_ME_QUARTERPELREFINE8_RD - | XVID_ME_EXTSEARCH_RD - | XVID_ME_CHECKPREDICTION_RD; - case 1: - if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) ) - x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD; - x->me_flags |= XVID_ME_HALFPELREFINE16_RD - | XVID_ME_QUARTERPELREFINE16_RD; - - default: - break; - } - - /* Bring in VOL flags from ffmpeg command-line */ - x->vol_flags = 0; - if( xvid_flags & CODEC_FLAG_GMC ) { - x->vol_flags |= XVID_VOL_GMC; - x->me_flags |= XVID_ME_GME_REFINE; - } - if( xvid_flags & CODEC_FLAG_QPEL ) { - x->vol_flags |= XVID_VOL_QUARTERPEL; - x->me_flags |= XVID_ME_QUARTERPELREFINE16; - if( x->vop_flags & XVID_VOP_INTER4V ) - x->me_flags |= XVID_ME_QUARTERPELREFINE8; - } - - memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); - xvid_gbl_init.version = XVID_VERSION; - xvid_gbl_init.debug = 0; - -#ifdef ARCH_POWERPC - /* XviD's PPC support is borked, use libavcodec to detect */ -#if HAVE_ALTIVEC==1 - if( has_altivec() ) { - xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC; - } else -#endif - xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; -#else - /* XviD can detect on x86 */ - xvid_gbl_init.cpu_flags = 0; -#endif - - /* Initialize */ - xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); - - /* Create the encoder reference */ - memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); - xvid_enc_create.version = XVID_VERSION; - - /* Store the desired frame size */ - xvid_enc_create.width = x->xsize = avctx->width; - xvid_enc_create.height = x->ysize = avctx->height; - - /* XviD can determine the proper profile to use */ - /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ - - /* We don't use zones or threads */ - xvid_enc_create.zones = NULL; - xvid_enc_create.num_zones = 0; - xvid_enc_create.num_threads = 0; - - xvid_enc_create.plugins = plugins; - xvid_enc_create.num_plugins = 0; - - /* Initialize Buffers */ - x->twopassbuffer = NULL; - x->old_twopassbuffer = NULL; - x->twopassfile = NULL; - - if( xvid_flags & CODEC_FLAG_PASS1 ) { - memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t)); - rc2pass1.version = XVID_VERSION; - rc2pass1.context = x; - x->twopassbuffer = av_malloc(BUFFER_SIZE); - x->old_twopassbuffer = av_malloc(BUFFER_SIZE); - if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) { - av_log(avctx, AV_LOG_ERROR, - "XviD: Cannot allocate 2-pass log buffers\n"); - return -1; - } - x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; - - plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; - plugins[xvid_enc_create.num_plugins].param = &rc2pass1; - xvid_enc_create.num_plugins++; - } else if( xvid_flags & CODEC_FLAG_PASS2 ) { - memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); - rc2pass2.version = XVID_VERSION; - rc2pass2.bitrate = avctx->bit_rate; - - fd = av_tempfile("xvidff.", &(x->twopassfile)); - if( fd == -1 ) { - av_log(avctx, AV_LOG_ERROR, - "XviD: Cannot write 2-pass pipe\n"); - return -1; - } - - if( avctx->stats_in == NULL ) { - av_log(avctx, AV_LOG_ERROR, - "XviD: No 2-pass information loaded for second pass\n"); - return -1; - } - - if( strlen(avctx->stats_in) > - write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) { - close(fd); - av_log(avctx, AV_LOG_ERROR, - "XviD: Cannot write to 2-pass pipe\n"); - return -1; - } - - close(fd); - rc2pass2.filename = x->twopassfile; - plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; - plugins[xvid_enc_create.num_plugins].param = &rc2pass2; - xvid_enc_create.num_plugins++; - } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) { - /* Single Pass Bitrate Control! */ - memset(&single, 0, sizeof(xvid_plugin_single_t)); - single.version = XVID_VERSION; - single.bitrate = avctx->bit_rate; - - plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; - plugins[xvid_enc_create.num_plugins].param = &single; - xvid_enc_create.num_plugins++; - } - - /* Luminance Masking */ - if( 0.0 != avctx->lumi_masking ) { - plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; - plugins[xvid_enc_create.num_plugins].param = NULL; - xvid_enc_create.num_plugins++; - } - - /* Frame Rate and Key Frames */ - xvid_correct_framerate(avctx); - xvid_enc_create.fincr = avctx->time_base.num; - xvid_enc_create.fbase = avctx->time_base.den; - if( avctx->gop_size > 0 ) - xvid_enc_create.max_key_interval = avctx->gop_size; - else - xvid_enc_create.max_key_interval = 240; /* XviD's best default */ - - /* Quants */ - if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; - else x->qscale = 0; - - xvid_enc_create.min_quant[0] = avctx->qmin; - xvid_enc_create.min_quant[1] = avctx->qmin; - xvid_enc_create.min_quant[2] = avctx->qmin; - xvid_enc_create.max_quant[0] = avctx->qmax; - xvid_enc_create.max_quant[1] = avctx->qmax; - xvid_enc_create.max_quant[2] = avctx->qmax; - - /* Quant Matrices */ - x->intra_matrix = x->inter_matrix = NULL; - if( avctx->mpeg_quant ) - x->vol_flags |= XVID_VOL_MPEGQUANT; - if( (avctx->intra_matrix || avctx->inter_matrix) ) { - x->vol_flags |= XVID_VOL_MPEGQUANT; - - if( avctx->intra_matrix ) { - intra = avctx->intra_matrix; - x->intra_matrix = av_malloc(sizeof(unsigned char) * 64); - } else - intra = NULL; - if( avctx->inter_matrix ) { - inter = avctx->inter_matrix; - x->inter_matrix = av_malloc(sizeof(unsigned char) * 64); - } else - inter = NULL; - - for( i = 0; i < 64; i++ ) { - if( intra ) - x->intra_matrix[i] = (unsigned char)intra[i]; - if( inter ) - x->inter_matrix[i] = (unsigned char)inter[i]; - } - } - - /* Misc Settings */ - xvid_enc_create.frame_drop_ratio = 0; - xvid_enc_create.global = 0; - if( xvid_flags & CODEC_FLAG_CLOSED_GOP ) - xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; - - /* Determines which codec mode we are operating in */ - avctx->extradata = NULL; - avctx->extradata_size = 0; - if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) { - /* In this case, we are claiming to be MPEG4 */ - x->quicktime_format = 1; - avctx->codec_id = CODEC_ID_MPEG4; - } else { - /* We are claiming to be XviD */ - x->quicktime_format = 0; - if(!avctx->codec_tag) - avctx->codec_tag = ff_get_fourcc("xvid"); - } - - /* Bframes */ - xvid_enc_create.max_bframes = avctx->max_b_frames; - xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset; - xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor; - if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; - - /* Create encoder context */ - xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); - if( xerr ) { - av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n"); - return -1; - } - - x->encoder_handle = xvid_enc_create.handle; - avctx->coded_frame = &x->encoded_picture; - - return 0; -} - -/** - * Encodes a single frame. - * - * @param avctx AVCodecContext pointer to context - * @param frame Pointer to encoded frame buffer - * @param buf_size Size of encoded frame buffer - * @param data Pointer to AVFrame of unencoded frame - * @return Returns 0 on success, -1 on failure - */ -int ff_xvid_encode_frame(AVCodecContext *avctx, - unsigned char *frame, int buf_size, void *data) { - int xerr, i; - char *tmp; - xvid_context_t *x = avctx->priv_data; - AVFrame *picture = data; - AVFrame *p = &(x->encoded_picture); - - xvid_enc_frame_t xvid_enc_frame; - xvid_enc_stats_t xvid_enc_stats; - - /* Start setting up the frame */ - memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); - xvid_enc_frame.version = XVID_VERSION; - memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); - xvid_enc_stats.version = XVID_VERSION; - *p = *picture; - - /* Let XviD know where to put the frame. */ - xvid_enc_frame.bitstream = frame; - xvid_enc_frame.length = buf_size; - - /* Initialize input image fields */ - if( avctx->pix_fmt != PIX_FMT_YUV420P ) { - av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n"); - return -1; - } - - xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */ - - for( i = 0; i < 4; i++ ) { - xvid_enc_frame.input.plane[i] = picture->data[i]; - xvid_enc_frame.input.stride[i] = picture->linesize[i]; - } - - /* Encoder Flags */ - xvid_enc_frame.vop_flags = x->vop_flags; - xvid_enc_frame.vol_flags = x->vol_flags; - xvid_enc_frame.motion = x->me_flags; - xvid_enc_frame.type = XVID_TYPE_AUTO; - - /* Quant Setting */ - if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; - else xvid_enc_frame.quant = 0; - - /* Matrices */ - xvid_enc_frame.quant_intra_matrix = x->intra_matrix; - xvid_enc_frame.quant_inter_matrix = x->inter_matrix; - - /* Encode */ - xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE, - &xvid_enc_frame, &xvid_enc_stats); - - /* Two-pass log buffer swapping */ - avctx->stats_out = NULL; - if( x->twopassbuffer ) { - tmp = x->old_twopassbuffer; - x->old_twopassbuffer = x->twopassbuffer; - x->twopassbuffer = tmp; - x->twopassbuffer[0] = 0; - if( x->old_twopassbuffer[0] != 0 ) { - avctx->stats_out = x->old_twopassbuffer; - } - } - - if( 0 <= xerr ) { - p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA; - if( xvid_enc_stats.type == XVID_TYPE_PVOP ) - p->pict_type = FF_P_TYPE; - else if( xvid_enc_stats.type == XVID_TYPE_BVOP ) - p->pict_type = FF_B_TYPE; - else if( xvid_enc_stats.type == XVID_TYPE_SVOP ) - p->pict_type = FF_S_TYPE; - else - p->pict_type = FF_I_TYPE; - if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) { - p->key_frame = 1; - if( x->quicktime_format ) - return xvid_strip_vol_header(avctx, frame, - xvid_enc_stats.hlength, xerr); - } else - p->key_frame = 0; - - return xerr; - } else { - av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr); - return -1; - } -} - -/** - * Destroys the private context for the encoder. - * All buffers are freed, and the XviD encoder context is destroyed. - * - * @param avctx AVCodecContext pointer to context - * @return Returns 0, success guaranteed - */ -int ff_xvid_encode_close(AVCodecContext *avctx) { - xvid_context_t *x = avctx->priv_data; - - xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL); - - if( avctx->extradata != NULL ) - av_free(avctx->extradata); - if( x->twopassbuffer != NULL ) { - av_free(x->twopassbuffer); - av_free(x->old_twopassbuffer); - } - if( x->twopassfile != NULL ) - av_free(x->twopassfile); - if( x->intra_matrix != NULL ) - av_free(x->intra_matrix); - if( x->inter_matrix != NULL ) - av_free(x->inter_matrix); - - return 0; -} - -/** - * Routine to create a global VO/VOL header for MP4 container. - * What we do here is extract the header from the XviD bitstream - * as it is encoded. We also strip the repeated headers from the - * bitstream when a global header is requested for MPEG-4 ISO - * compliance. - * - * @param avctx AVCodecContext pointer to context - * @param frame Pointer to encoded frame data - * @param header_len Length of header to search - * @param frame_len Length of encoded frame data - * @return Returns new length of frame data - */ -int xvid_strip_vol_header(AVCodecContext *avctx, - unsigned char *frame, - unsigned int header_len, - unsigned int frame_len) { - int vo_len = 0, i; - - for( i = 0; i < header_len - 3; i++ ) { - if( frame[i] == 0x00 && - frame[i+1] == 0x00 && - frame[i+2] == 0x01 && - frame[i+3] == 0xB6 ) { - vo_len = i; - break; - } - } - - if( vo_len > 0 ) { - /* We need to store the header, so extract it */ - if( avctx->extradata == NULL ) { - avctx->extradata = av_malloc(vo_len); - memcpy(avctx->extradata, frame, vo_len); - avctx->extradata_size = vo_len; - } - /* Less dangerous now, memmove properly copies the two - chunks of overlapping data */ - memmove(frame, &(frame[vo_len]), frame_len - vo_len); - return frame_len - vo_len; - } else - return frame_len; -} - -/** - * Routine to correct a possibly erroneous framerate being fed to us. - * XviD currently chokes on framerates where the ticks per frame is - * extremely large. This function works to correct problems in this area - * by estimating a new framerate and taking the simpler fraction of - * the two presented. - * - * @param avctx Context that contains the framerate to correct. - */ -void xvid_correct_framerate(AVCodecContext *avctx) { - int frate, fbase; - int est_frate, est_fbase; - int gcd; - float est_fps, fps; - - frate = avctx->time_base.den; - fbase = avctx->time_base.num; - - gcd = ff_gcd(frate, fbase); - if( gcd > 1 ) { - frate /= gcd; - fbase /= gcd; - } - - if( frate <= 65000 && fbase <= 65000 ) { - avctx->time_base.den = frate; - avctx->time_base.num = fbase; - return; - } - - fps = (float)frate / (float)fbase; - est_fps = roundf(fps * 1000.0) / 1000.0; - - est_frate = (int)est_fps; - if( est_fps > (int)est_fps ) { - est_frate = (est_frate + 1) * 1000; - est_fbase = (int)roundf((float)est_frate / est_fps); - } else - est_fbase = 1; - - gcd = ff_gcd(est_frate, est_fbase); - if( gcd > 1 ) { - est_frate /= gcd; - est_fbase /= gcd; - } - - if( fbase > est_fbase ) { - avctx->time_base.den = est_frate; - avctx->time_base.num = est_fbase; - av_log(avctx, AV_LOG_DEBUG, - "XviD: framerate re-estimated: %.2f, %.3f%% correction\n", - est_fps, (((est_fps - fps)/fps) * 100.0)); - } else { - avctx->time_base.den = frate; - avctx->time_base.num = fbase; - } -} - -/* - * XviD 2-Pass Kludge Section - * - * XviD's default 2-pass doesn't allow us to create data as we need to, so - * this section spends time replacing the first pass plugin so we can write - * statistic information as libavcodec requests in. We have another kludge - * that allows us to pass data to the second pass in XviD without a custom - * rate-control plugin. - */ - -/** - * Initializes the two-pass plugin and context. - * - * @param param Input construction parameter structure - * @param handle Private context handle - * @return Returns XVID_ERR_xxxx on failure, or 0 on success. - */ -static int xvid_ff_2pass_create(xvid_plg_create_t * param, - void ** handle) { - xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param; - char *log = x->context->twopassbuffer; - - /* Do a quick bounds check */ - if( log == NULL ) - return XVID_ERR_FAIL; - - /* We use snprintf() */ - /* This is because we can safely prevent a buffer overflow */ - log[0] = 0; - snprintf(log, BUFFER_REMAINING(log), - "# ffmpeg 2-pass log file, using xvid codec\n"); - snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), - "# Do not modify. libxvidcore version: %d.%d.%d\n\n", - XVID_VERSION_MAJOR(XVID_VERSION), - XVID_VERSION_MINOR(XVID_VERSION), - XVID_VERSION_PATCH(XVID_VERSION)); - - *handle = x->context; - return 0; -} - -/** - * Destroys the two-pass plugin context. - * - * @param ref Context pointer for the plugin - * @param param Destrooy context - * @return Returns 0, success guaranteed - */ -static int xvid_ff_2pass_destroy(xvid_context_t *ref, - xvid_plg_destroy_t *param) { - /* Currently cannot think of anything to do on destruction */ - /* Still, the framework should be here for reference/use */ - if( ref->twopassbuffer != NULL ) - ref->twopassbuffer[0] = 0; - return 0; -} - -/** - * Enables fast encode mode during the first pass. - * - * @param ref Context pointer for the plugin - * @param param Frame data - * @return Returns 0, success guaranteed - */ -static int xvid_ff_2pass_before(xvid_context_t *ref, - xvid_plg_data_t *param) { - int motion_remove; - int motion_replacements; - int vop_remove; - - /* Nothing to do here, result is changed too much */ - if( param->zone && param->zone->mode == XVID_ZONE_QUANT ) - return 0; - - /* We can implement a 'turbo' first pass mode here */ - param->quant = 2; - - /* Init values */ - motion_remove = ~XVID_ME_CHROMA_PVOP & - ~XVID_ME_CHROMA_BVOP & - ~XVID_ME_EXTSEARCH16 & - ~XVID_ME_ADVANCEDDIAMOND16; - motion_replacements = XVID_ME_FAST_MODEINTERPOLATE | - XVID_ME_SKIP_DELTASEARCH | - XVID_ME_FASTREFINE16 | - XVID_ME_BFRAME_EARLYSTOP; - vop_remove = ~XVID_VOP_MODEDECISION_RD & - ~XVID_VOP_FAST_MODEDECISION_RD & - ~XVID_VOP_TRELLISQUANT & - ~XVID_VOP_INTER4V & - ~XVID_VOP_HQACPRED; - - param->vol_flags &= ~XVID_VOL_GMC; - param->vop_flags &= vop_remove; - param->motion_flags &= motion_remove; - param->motion_flags |= motion_replacements; - - return 0; -} - -/** - * Captures statistic data and writes it during first pass. - * - * @param ref Context pointer for the plugin - * @param param Statistic data - * @return Returns XVID_ERR_xxxx on failure, or 0 on success - */ -static int xvid_ff_2pass_after(xvid_context_t *ref, - xvid_plg_data_t *param) { - char *log = ref->twopassbuffer; - char *frame_types = " ipbs"; - char frame_type; - - /* Quick bounds check */ - if( log == NULL ) - return XVID_ERR_FAIL; - - /* Convert the type given to us into a character */ - if( param->type < 5 && param->type > 0 ) { - frame_type = frame_types[param->type]; - } else { - return XVID_ERR_FAIL; - } - - snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), - "%c %d %d %d %d %d %d\n", - frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks, - param->stats.ublks, param->stats.length, param->stats.hlength); - - return 0; -} - -/** - * Dispatch function for our custom plugin. - * This handles the dispatch for the XviD plugin. It passes data - * on to other functions for actual processing. - * - * @param ref Context pointer for the plugin - * @param cmd The task given for us to complete - * @param p1 First parameter (varies) - * @param p2 Second parameter (varies) - * @return Returns XVID_ERR_xxxx on failure, or 0 on success - */ -int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) { - switch( cmd ) { - case XVID_PLG_INFO: - case XVID_PLG_FRAME: - return 0; - - case XVID_PLG_BEFORE: - return xvid_ff_2pass_before(ref, p1); - - case XVID_PLG_CREATE: - return xvid_ff_2pass_create(p1, p2); - - case XVID_PLG_AFTER: - return xvid_ff_2pass_after(ref, p1); - - case XVID_PLG_DESTROY: - return xvid_ff_2pass_destroy(ref, p1); - - default: - return XVID_ERR_FAIL; - } -} - -/** - * XviD codec definition for libavcodec. - */ -AVCodec xvid_encoder = { - "xvid", - CODEC_TYPE_VIDEO, - CODEC_ID_XVID, - sizeof(xvid_context_t), - ff_xvid_encode_init, - ff_xvid_encode_frame, - ff_xvid_encode_close, - .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1}, -};