diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/Jamfile b/src/add-ons/media/plugins/avcodec/libavcodec/Jamfile index 9f04e1fbed..a96f922cf8 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/Jamfile +++ b/src/add-ons/media/plugins/avcodec/libavcodec/Jamfile @@ -1,6 +1,6 @@ SubDir OBOS_TOP src add-ons media plugins avcodec libavcodec ; -CFLAGS += -fomit-frame-pointer -DPIC ; +SubDirCcFlags -fomit-frame-pointer -DPIC ; DEFINES += HAVE_AV_CONFIG_H=1 ; StaticLibrary avcodec : diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/adpcm.c b/src/add-ons/media/plugins/avcodec/libavcodec/adpcm.c index 3b5f5c1545..bced66f198 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/adpcm.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/adpcm.c @@ -299,7 +299,7 @@ static int adpcm_decode_init(AVCodecContext * avctx) return 0; } -static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble) +static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift) { int step_index; int predictor; @@ -315,34 +315,7 @@ static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble) /* perform direct multiplication instead of series of jumps proposed by * the reference ADPCM implementation since modern CPUs can do the mults * quickly enough */ - diff = ((2 * delta + 1) * step) >> 3; - predictor = c->predictor; - if (sign) predictor -= diff; - else predictor += diff; - - CLAMP_TO_SHORT(predictor); - c->predictor = predictor; - c->step_index = step_index; - - return (short)predictor; -} - -static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble) -{ - int step_index; - int predictor; - int sign, delta, diff, step; - - step = step_table[c->step_index]; - step_index = c->step_index + index_table[(unsigned)nibble]; - if (step_index < 0) step_index = 0; - else if (step_index > 88) step_index = 88; - - sign = nibble & 8; - delta = nibble & 7; - - diff = (delta*step + (step>>1))>>3; // difference to code above - + diff = ((2 * delta + 1) * step) >> shift; predictor = c->predictor; if (sign) predictor -= diff; else predictor += diff; @@ -471,6 +444,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, int decode_top_nibble_next = 0; int diff_channel; + if (!buf_size) + return 0; + samples = data; src = buf; @@ -505,9 +481,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, samples++; for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */ - *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F); + *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3); samples += avctx->channels; - *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F); + *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3); samples += avctx->channels; src ++; } @@ -524,44 +500,29 @@ static int adpcm_decode_frame(AVCodecContext *avctx, if (avctx->block_align != 0 && buf_size > avctx->block_align) buf_size = avctx->block_align; - // XXX: do as per-channel loop - cs = &(c->status[0]); - cs->predictor = (*src++) & 0x0FF; - cs->predictor |= ((*src++) << 8) & 0x0FF00; - if(cs->predictor & 0x8000) - cs->predictor -= 0x10000; - CLAMP_TO_SHORT(cs->predictor); - - // XXX: is this correct ??: *samples++ = cs->predictor; - - cs->step_index = *src++; - if (cs->step_index < 0) cs->step_index = 0; - if (cs->step_index > 88) cs->step_index = 88; - if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */ - - if (st) { - cs = &(c->status[1]); - cs->predictor = (*src++) & 0x0FF; - cs->predictor |= ((*src++) << 8) & 0x0FF00; + for(i=0; ichannels; i++){ + cs = &(c->status[i]); + cs->predictor = *src++; + cs->predictor |= (*src++) << 8; if(cs->predictor & 0x8000) cs->predictor -= 0x10000; CLAMP_TO_SHORT(cs->predictor); - // XXX: is this correct ??: *samples++ = cs->predictor; + // XXX: is this correct ??: *samples++ = cs->predictor; - cs->step_index = *src++; + cs->step_index = *src++; if (cs->step_index < 0) cs->step_index = 0; if (cs->step_index > 88) cs->step_index = 88; - src++; /* if != 0 -> out-of-sync */ + if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */ } for(m=4; src < (buf + buf_size);) { - *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F); + *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3); if (st) - *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F); - *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F); + *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3); + *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3); if (st) { - *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F); + *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3); if (!--m) { m=4; src+=4; @@ -585,12 +546,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, m= (buf_size - (src - buf))>>st; for(i=0; istatus[0], src[i] & 0x0F); + *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4); if (st) - *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F); - *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4); + *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4); + *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4); if (st) - *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4); + *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4); } src += m<status[0], - (src[0] >> 4) & 0x0F); + (src[0] >> 4) & 0x0F, 3); /* take care of the bottom nibble, which is right sample for * stereo, or another mono sample */ if (st) *samples++ = adpcm_ima_expand_nibble(&c->status[1], - src[0] & 0x0F); + src[0] & 0x0F, 3); else *samples++ = adpcm_ima_expand_nibble(&c->status[0], - src[0] & 0x0F); + src[0] & 0x0F, 3); src++; } @@ -703,11 +664,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, /* process the first predictor of the sum channel */ DK3_GET_NEXT_NIBBLE(); - adpcm_ima_expand_nibble(&c->status[0], nibble); + adpcm_ima_expand_nibble(&c->status[0], nibble, 3); /* process the diff channel predictor */ DK3_GET_NEXT_NIBBLE(); - adpcm_ima_expand_nibble(&c->status[1], nibble); + adpcm_ima_expand_nibble(&c->status[1], nibble, 3); /* process the first pair of stereo PCM samples */ diff_channel = (diff_channel + c->status[1].predictor) / 2; @@ -716,7 +677,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, /* process the second predictor of the sum channel */ DK3_GET_NEXT_NIBBLE(); - adpcm_ima_expand_nibble(&c->status[0], nibble); + adpcm_ima_expand_nibble(&c->status[0], nibble, 3); /* process the second pair of stereo PCM samples */ diff_channel = (diff_channel + c->status[1].predictor) / 2; @@ -730,14 +691,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, if (st) { *samples++ = adpcm_ima_expand_nibble(&c->status[0], - (src[0] >> 4) & 0x0F); + (src[0] >> 4) & 0x0F, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[1], - src[0] & 0x0F); + src[0] & 0x0F, 3); } else { *samples++ = adpcm_ima_expand_nibble(&c->status[0], - (src[0] >> 4) & 0x0F); + (src[0] >> 4) & 0x0F, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[0], - src[0] & 0x0F); + src[0] & 0x0F, 3); } src++; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/allcodecs.c b/src/add-ons/media/plugins/avcodec/libavcodec/allcodecs.c index 03fc40816a..b71db05308 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/allcodecs.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/allcodecs.c @@ -73,6 +73,7 @@ void avcodec_register_all(void) register_avcodec(&asv1_encoder); register_avcodec(&asv2_encoder); register_avcodec(&ffv1_encoder); + register_avcodec(&zlib_encoder); #endif /* CONFIG_ENCODERS */ register_avcodec(&rawvideo_encoder); register_avcodec(&rawvideo_decoder); @@ -140,6 +141,10 @@ void avcodec_register_all(void) register_avcodec(&smc_decoder); register_avcodec(&flic_decoder); register_avcodec(&truemotion1_decoder); + register_avcodec(&vmdvideo_decoder); + register_avcodec(&vmdaudio_decoder); + register_avcodec(&mszh_decoder); + register_avcodec(&zlib_decoder); #ifdef CONFIG_AC3 register_avcodec(&ac3_decoder); #endif diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/avcodec.h b/src/add-ons/media/plugins/avcodec/libavcodec/avcodec.h index 39a71662fb..b6dee00eaf 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/avcodec.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/avcodec.h @@ -17,7 +17,7 @@ extern "C" { #define FFMPEG_VERSION_INT 0x000408 #define FFMPEG_VERSION "0.4.8" -#define LIBAVCODEC_BUILD 4697 +#define LIBAVCODEC_BUILD 4699 #define LIBAVCODEC_VERSION_INT FFMPEG_VERSION_INT #define LIBAVCODEC_VERSION FFMPEG_VERSION @@ -89,6 +89,10 @@ enum CodecID { CODEC_ID_SMC, CODEC_ID_FLIC, CODEC_ID_TRUEMOTION1, + CODEC_ID_VMDVIDEO, + CODEC_ID_VMDAUDIO, + CODEC_ID_MSZH, + CODEC_ID_ZLIB, /* various pcm "codecs" */ CODEC_ID_PCM_S16LE, @@ -263,7 +267,10 @@ static const __attribute__((unused)) int Motion_Est_QTab[] = #define CODEC_FLAG_H263P_AIV 0x00000008 ///< H263 Alternative inter vlc #define CODEC_FLAG_OBMC 0x00000001 ///< OBMC #define CODEC_FLAG_LOOP_FILTER 0x00000800 ///< loop filter -#define CODEC_FLAG_H263P_SLICE_STRUCT 0x10000000 +#define CODEC_FLAG_H263P_SLICE_STRUCT 0x10000000 +#define CODEC_FLAG_INTERLACED_ME 0x20000000 ///< interlaced motion estimation +#define CODEC_FLAG_SVCD_SCAN_OFFSET 0x40000000 ///< will reserve space for SVCD scan offset user data +#define CODEC_FLAG_CLOSED_GOP 0x80000000 /* Unsupported options : * Syntax Arithmetic coding (SAC) * Reference Picture Selection @@ -956,6 +963,8 @@ typedef struct AVCodecContext { /** * qscale factor between p and i frames. + * if > 0 then the last p frame quantizer will be used (q= lastp_q*factor+offset) + * if < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset) * - encoding: set by user. * - decoding: unused */ @@ -963,8 +972,6 @@ typedef struct AVCodecContext { /** * qscale offset between p and i frames. - * if > 0 then the last p frame quantizer will be used (q= lastp_q*factor+offset) - * if < 0 then normal ratecontrol will be done (q= -normal_q*factor+offset) * - encoding: set by user. * - decoding: unused */ @@ -1133,7 +1140,7 @@ typedef struct AVCodecContext { #define FF_DEBUG_MB_TYPE 8 #define FF_DEBUG_QP 16 #define FF_DEBUG_MV 32 -#define FF_DEBUG_VIS_MV 0x00000040 +//#define FF_DEBUG_VIS_MV 0x00000040 #define FF_DEBUG_SKIP 0x00000080 #define FF_DEBUG_STARTCODE 0x00000100 #define FF_DEBUG_PTS 0x00000200 @@ -1143,6 +1150,16 @@ typedef struct AVCodecContext { #define FF_DEBUG_VIS_QP 0x00002000 #define FF_DEBUG_VIS_MB_TYPE 0x00004000 + /** + * debug. + * - encoding: set by user. + * - decoding: set by user. + */ + int debug_mv; +#define FF_DEBUG_VIS_MV_P_FOR 0x00000001 //visualize forward predicted MVs of P frames +#define FF_DEBUG_VIS_MV_B_FOR 0x00000002 //visualize forward predicted MVs of B frames +#define FF_DEBUG_VIS_MV_B_BACK 0x00000004 //visualize backward predicted MVs of B frames + /** * error. * - encoding: set by lavc if flags&CODEC_FLAG_PSNR @@ -1182,6 +1199,12 @@ typedef struct AVCodecContext { * - decoding: unused */ int mb_cmp; + /** + * interlaced dct compare function + * - encoding: set by user. + * - decoding: unused + */ + int ildct_cmp; #define FF_CMP_SAD 0 #define FF_CMP_SSE 1 #define FF_CMP_SATD 2 @@ -1190,6 +1213,8 @@ typedef struct AVCodecContext { #define FF_CMP_BIT 5 #define FF_CMP_RD 6 #define FF_CMP_ZERO 7 +#define FF_CMP_VSAD 8 +#define FF_CMP_VSSE 9 #define FF_CMP_CHROMA 256 /** @@ -1468,6 +1493,17 @@ typedef struct AVCodecContext { * - decoding: unused. */ int error_rate; + + /** + * MP3 antialias algorithm, see FF_AA_* below. + * - encoding: unused + * - decoding: set by user + */ + int antialias_algo; +#define FF_AA_AUTO 0 +#define FF_AA_FASTINT 1 //not implemented yet +#define FF_AA_INT 2 +#define FF_AA_FLOAT 3 } AVCodecContext; @@ -1591,6 +1627,7 @@ extern AVCodec asv2_encoder; extern AVCodec vcr1_encoder; extern AVCodec ffv1_encoder; extern AVCodec mdec_encoder; +extern AVCodec zlib_encoder; extern AVCodec h263_decoder; extern AVCodec mpeg4_decoder; @@ -1651,7 +1688,11 @@ extern AVCodec idcin_decoder; extern AVCodec eightbps_decoder; extern AVCodec smc_decoder; extern AVCodec flic_decoder; +extern AVCodec vmdvideo_decoder; +extern AVCodec vmdaudio_decoder; extern AVCodec truemotion1_decoder; +extern AVCodec mszh_decoder; +extern AVCodec zlib_decoder; extern AVCodec ra_144_decoder; extern AVCodec ra_288_decoder; extern AVCodec roq_dpcm_decoder; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/common.c b/src/add-ons/media/plugins/avcodec/libavcodec/common.c index ed59ea1d82..6bca53f1e6 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/common.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/common.c @@ -1,6 +1,7 @@ /* * Common bit i/o utils * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -59,7 +60,8 @@ void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) #endif } -#ifdef CONFIG_ENCODERS +//#ifdef CONFIG_ENCODERS +#if 1 /* return the number of bits output */ int get_bit_count(PutBitContext *s) diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/common.h b/src/add-ons/media/plugins/avcodec/libavcodec/common.h index 84df80cfe8..c144ba17d4 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/common.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/common.h @@ -18,6 +18,10 @@ //#define A32_BITSTREAM_READER #define LIBMPEG2_BITSTREAM_READER_HACK //add BERO +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + #ifdef HAVE_AV_CONFIG_H /* only include the following when compiling package */ # include "config.h" @@ -37,10 +41,6 @@ # define ENODATA 61 # endif -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - #include #ifndef offsetof # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F)) @@ -82,6 +82,29 @@ extern const struct AVOption avoptions_workaround_bug[11]; # define always_inline inline #endif +#ifndef EMULATE_INTTYPES +# include +#else + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; + +# ifdef CONFIG_WIN32 + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; +# else /* other OS */ + typedef signed long long int64_t; + typedef unsigned long long uint64_t; +# endif /* other OS */ +#endif /* HAVE_INTTYPES_H */ + +#ifndef INT64_MAX +#define INT64_MAX 9223372036854775807LL +#endif + #ifdef EMULATE_FAST_INT /* note that we don't emulate 64bit ints */ typedef signed char int_fast8_t; @@ -102,15 +125,6 @@ static inline float floorf(float f) { /* windows */ -typedef unsigned short uint16_t; -typedef signed short int16_t; -typedef unsigned char uint8_t; -typedef unsigned int uint32_t; -typedef unsigned __int64 uint64_t; -typedef signed char int8_t; -typedef signed int int32_t; -typedef signed __int64 int64_t; - # ifndef __MINGW32__ # define int64_t_C(c) (c ## i64) # define uint64_t_C(c) (c ## i64) @@ -137,8 +151,6 @@ typedef signed __int64 int64_t; #elif defined (CONFIG_OS2) /* OS/2 EMX */ -#include - #ifndef int64_t_C #define int64_t_C(c) (c ## LL) #define uint64_t_C(c) (c ## ULL) @@ -159,8 +171,6 @@ typedef signed __int64 int64_t; /* unix */ -#include - #ifndef int64_t_C #define int64_t_C(c) (c ## LL) #define uint64_t_C(c) (c ## ULL) @@ -201,13 +211,9 @@ inline void dprintf(const char* fmt,...) {} # else # ifdef DEBUG -# if defined(__BEOS__) -# define dprintf(fmt...) printf(fmt) -# else -# define dprintf(fmt,...) printf(fmt, __VA_ARGS__) -# endif +# define dprintf(fmt,...) printf(fmt, __VA_ARGS__) # else -# if defined(__BEOS__) +# if defined(CONFIG_BEOS) # define dprintf(fmt...) # else # define dprintf(fmt,...) @@ -1014,23 +1020,31 @@ static inline int av_log2_16bit(unsigned int v) return n; } - /* median of 3 */ static inline int mid_pred(int a, int b, int c) { - int vmin, vmax; - vmax = vmin = a; - if (b < vmin) - vmin = b; - else - vmax = b; +#if 0 + int t= (a-b)&((a-b)>>31); + a-=t; + b+=t; + b-= (b-c)&((b-c)>>31); + b+= (a-b)&((a-b)>>31); - if (c < vmin) - vmin = c; - else if (c > vmax) - vmax = c; - - return a + b + c - vmin - vmax; + return b; +#else + if(a>b){ + if(c>b){ + if(c>a) b=a; + else b=c; + } + }else{ + if(b>c){ + if(c>a) b=c; + else b=a; + } + } + return b; +#endif } static inline int clip(int a, int amin, int amax) diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/dpcm.c b/src/add-ons/media/plugins/avcodec/libavcodec/dpcm.c index ef4ccf8860..b59a9cd6b8 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/dpcm.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/dpcm.c @@ -125,6 +125,9 @@ static int dpcm_decode_frame(AVCodecContext *avctx, unsigned char byte; short diff; + if (!buf_size) + return 0; + switch(avctx->codec->id) { case CODEC_ID_ROQ_DPCM: diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.c b/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.c index e516d7ee99..114d67b501 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.c @@ -1,6 +1,7 @@ /* * DSP utils * Copyright (c) 2000, 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -218,13 +219,13 @@ static void bswap_buf(uint32_t *dst, uint32_t *src, int w){ } } -static int sse8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size) +static int sse8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h) { int s, i; uint32_t *sq = squareTbl + 256; s = 0; - for (i = 0; i < 8; i++) { + for (i = 0; i < h; i++) { s += sq[pix1[0] - pix2[0]]; s += sq[pix1[1] - pix2[1]]; s += sq[pix1[2] - pix2[2]]; @@ -239,13 +240,13 @@ static int sse8_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size) return s; } -static int sse16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size) +static int sse16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) { int s, i; uint32_t *sq = squareTbl + 256; s = 0; - for (i = 0; i < 16; i++) { + for (i = 0; i < h; i++) { s += sq[pix1[ 0] - pix2[ 0]]; s += sq[pix1[ 1] - pix2[ 1]]; s += sq[pix1[ 2] - pix2[ 2]]; @@ -2331,12 +2332,12 @@ static void h263_h_loop_filter_c(uint8_t *src, int stride, int qscale){ } } -static inline int pix_abs16x16_c(uint8_t *pix1, uint8_t *pix2, int line_size) +static inline int pix_abs16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) { int s, i; s = 0; - for(i=0;i<16;i++) { + for(i=0;isad[i]; + break; + case FF_CMP_SATD: + cmp[i]= c->hadamard8_diff[i]; + break; + case FF_CMP_SSE: + cmp[i]= c->sse[i]; + break; + case FF_CMP_DCT: + cmp[i]= c->dct_sad[i]; + break; + case FF_CMP_PSNR: + cmp[i]= c->quant_psnr[i]; + break; + case FF_CMP_BIT: + cmp[i]= c->bit[i]; + break; + case FF_CMP_RD: + cmp[i]= c->rd[i]; + break; + case FF_CMP_VSAD: + cmp[i]= c->vsad[i]; + break; + case FF_CMP_VSSE: + cmp[i]= c->vsse[i]; + break; + case FF_CMP_ZERO: + cmp[i]= zero_cmp; + break; + default: + av_log(NULL, AV_LOG_ERROR,"internal error in cmp function selection\n"); + } + } +} + /** * memset(blocks, 0, sizeof(DCTELEM)*6*64) */ @@ -2641,10 +2681,12 @@ o2= (i1)-(i2); #define BUTTERFLYA(x,y) (ABS((x)+(y)) + ABS((x)-(y))) -static int hadamard8_diff_c(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride){ +static int hadamard8_diff8x8_c(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){ int i; int temp[64]; int sum=0; + + assert(h==8); for(i=0; i<8; i++){ //FIXME try pointer walks @@ -2691,17 +2733,19 @@ if(sum>maxi){ return sum; } -static int hadamard8_abs_c(uint8_t *src, int stride, int mean){ +static int hadamard8_intra8x8_c(/*MpegEncContext*/ void *s, uint8_t *src, uint8_t *dummy, int stride, int h){ int i; int temp[64]; int sum=0; -//FIXME OOOPS ignore 0 term instead of mean mess + + assert(h==8); + for(i=0; i<8; i++){ //FIXME try pointer walks - BUTTERFLY2(temp[8*i+0], temp[8*i+1], src[stride*i+0]-mean,src[stride*i+1]-mean); - BUTTERFLY2(temp[8*i+2], temp[8*i+3], src[stride*i+2]-mean,src[stride*i+3]-mean); - BUTTERFLY2(temp[8*i+4], temp[8*i+5], src[stride*i+4]-mean,src[stride*i+5]-mean); - BUTTERFLY2(temp[8*i+6], temp[8*i+7], src[stride*i+6]-mean,src[stride*i+7]-mean); + BUTTERFLY2(temp[8*i+0], temp[8*i+1], src[stride*i+0],src[stride*i+1]); + BUTTERFLY2(temp[8*i+2], temp[8*i+3], src[stride*i+2],src[stride*i+3]); + BUTTERFLY2(temp[8*i+4], temp[8*i+5], src[stride*i+4],src[stride*i+5]); + BUTTERFLY2(temp[8*i+6], temp[8*i+7], src[stride*i+6],src[stride*i+7]); BUTTERFLY1(temp[8*i+0], temp[8*i+2]); BUTTERFLY1(temp[8*i+1], temp[8*i+3]); @@ -2732,14 +2776,18 @@ static int hadamard8_abs_c(uint8_t *src, int stride, int mean){ +BUTTERFLYA(temp[8*3+i], temp[8*7+i]); } + sum -= ABS(temp[8*0] + temp[8*4]); // -mean + return sum; } -static int dct_sad8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride){ +static int dct_sad8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ MpegEncContext * const s= (MpegEncContext *)c; uint64_t __align8 aligned_temp[sizeof(DCTELEM)*64/8]; DCTELEM * const temp= (DCTELEM*)aligned_temp; int sum=0, i; + + assert(h==8); s->dsp.diff_pixels(temp, src1, src2, stride); s->dsp.fdct(temp); @@ -2752,13 +2800,14 @@ static int dct_sad8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2 void simple_idct(DCTELEM *block); //FIXME -static int quant_psnr8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride){ +static int quant_psnr8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ MpegEncContext * const s= (MpegEncContext *)c; uint64_t __align8 aligned_temp[sizeof(DCTELEM)*64*2/8]; DCTELEM * const temp= (DCTELEM*)aligned_temp; DCTELEM * const bak = ((DCTELEM*)aligned_temp)+64; int sum=0, i; + assert(h==8); s->mb_intra=0; s->dsp.diff_pixels(temp, src1, src2, stride); @@ -2775,7 +2824,7 @@ static int quant_psnr8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *s return sum; } -static int rd8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride){ +static int rd8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ MpegEncContext * const s= (MpegEncContext *)c; const uint8_t *scantable= s->intra_scantable.permutated; uint64_t __align8 aligned_temp[sizeof(DCTELEM)*64/8]; @@ -2787,6 +2836,8 @@ static int rd8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int uint8_t * length; uint8_t * last_length; + assert(h==8); + for(i=0; i<8; i++){ ((uint32_t*)(bak + i*stride))[0]= ((uint32_t*)(src2 + i*stride))[0]; ((uint32_t*)(bak + i*stride))[1]= ((uint32_t*)(src2 + i*stride))[1]; @@ -2847,12 +2898,12 @@ static int rd8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int s->dsp.idct_add(bak, stride, temp); - distoration= s->dsp.sse[1](NULL, bak, src1, stride); + distoration= s->dsp.sse[1](NULL, bak, src1, stride, 8); return distoration + ((bits*s->qscale*s->qscale*109 + 64)>>7); } -static int bit8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride){ +static int bit8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, int stride, int h){ MpegEncContext * const s= (MpegEncContext *)c; const uint8_t *scantable= s->intra_scantable.permutated; uint64_t __align8 aligned_temp[sizeof(DCTELEM)*64/8]; @@ -2861,6 +2912,8 @@ static int bit8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, in const int esc_length= s->ac_esc_length; uint8_t * length; uint8_t * last_length; + + assert(h==8); s->dsp.diff_pixels(temp, src1, src2, stride); @@ -2910,12 +2963,73 @@ static int bit8x8_c(/*MpegEncContext*/ void *c, uint8_t *src1, uint8_t *src2, in return bits; } +static int vsad_intra16_c(/*MpegEncContext*/ void *c, uint8_t *s, uint8_t *dummy, int stride, int h){ + int score=0; + int x,y; + + for(y=1; yclear_blocks = clear_blocks_c; c->pix_sum = pix_sum_c; c->pix_norm1 = pix_norm1_c; - c->sse[0]= sse16_c; - c->sse[1]= sse8_c; /* TODO [0] 16 [1] 8 */ - c->pix_abs16x16 = pix_abs16x16_c; - c->pix_abs16x16_x2 = pix_abs16x16_x2_c; - c->pix_abs16x16_y2 = pix_abs16x16_y2_c; - c->pix_abs16x16_xy2 = pix_abs16x16_xy2_c; - c->pix_abs8x8 = pix_abs8x8_c; - c->pix_abs8x8_x2 = pix_abs8x8_x2_c; - c->pix_abs8x8_y2 = pix_abs8x8_y2_c; - c->pix_abs8x8_xy2 = pix_abs8x8_xy2_c; + c->pix_abs[0][0] = pix_abs16_c; + c->pix_abs[0][1] = pix_abs16_x2_c; + c->pix_abs[0][2] = pix_abs16_y2_c; + c->pix_abs[0][3] = pix_abs16_xy2_c; + c->pix_abs[1][0] = pix_abs8_c; + c->pix_abs[1][1] = pix_abs8_x2_c; + c->pix_abs[1][2] = pix_abs8_y2_c; + c->pix_abs[1][3] = pix_abs8_xy2_c; #define dspfunc(PFX, IDX, NUM) \ c->PFX ## _pixels_tab[IDX][0] = PFX ## _pixels ## NUM ## _c; \ @@ -3097,24 +3209,24 @@ void dsputil_init(DSPContext* c, AVCodecContext *avctx) c->put_mspel_pixels_tab[6]= put_mspel8_mc22_c; c->put_mspel_pixels_tab[7]= put_mspel8_mc32_c; - c->hadamard8_diff[0]= hadamard8_diff16_c; - c->hadamard8_diff[1]= hadamard8_diff_c; - c->hadamard8_abs = hadamard8_abs_c; +#define SET_CMP_FUNC(name) \ + c->name[0]= name ## 16_c;\ + c->name[1]= name ## 8x8_c; - c->dct_sad[0]= dct_sad16x16_c; - c->dct_sad[1]= dct_sad8x8_c; - - c->sad[0]= sad16x16_c; - c->sad[1]= sad8x8_c; - - c->quant_psnr[0]= quant_psnr16x16_c; - c->quant_psnr[1]= quant_psnr8x8_c; - - c->rd[0]= rd16x16_c; - c->rd[1]= rd8x8_c; - - c->bit[0]= bit16x16_c; - c->bit[1]= bit8x8_c; + SET_CMP_FUNC(hadamard8_diff) + c->hadamard8_diff[4]= hadamard8_intra16_c; + SET_CMP_FUNC(dct_sad) + c->sad[0]= pix_abs16_c; + c->sad[1]= pix_abs8_c; + c->sse[0]= sse16_c; + c->sse[1]= sse8_c; + SET_CMP_FUNC(quant_psnr) + SET_CMP_FUNC(rd) + SET_CMP_FUNC(bit) + c->vsad[0]= vsad16_c; + c->vsad[4]= vsad_intra16_c; + c->vsse[0]= vsse16_c; + c->vsse[4]= vsse_intra16_c; c->add_bytes= add_bytes_c; c->diff_bytes= diff_bytes_c; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.h b/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.h index 79b6c59c70..fbe2c684e1 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/dsputil.h @@ -1,6 +1,7 @@ /* * DSP utils * Copyright (c) 2000, 2001, 2002 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -44,6 +45,7 @@ void j_rev_dct (DCTELEM *data); void ff_fdct_mmx(DCTELEM *block); void ff_fdct_mmx2(DCTELEM *block); +void ff_fdct_sse2(DCTELEM *block); /* encoding scans */ extern const uint8_t ff_alternate_horizontal_scan[64]; @@ -79,6 +81,7 @@ void clear_blocks_c(DCTELEM *blocks); /* add and put pixel (decoding) */ // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16 +//h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller then 4 typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h); typedef void (*tpel_mc_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int w, int h); typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride); @@ -109,10 +112,9 @@ static void a(uint8_t *block, const uint8_t *pixels, int line_size, int h){\ } /* motion estimation */ - -typedef int (*op_pixels_abs_func)(uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size)/* __attribute__ ((const))*/; - -typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size)/* __attribute__ ((const))*/; +// h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller then 2 +// allthough currently h<4 is not used as functions with width <8 are not used and neither implemented +typedef int (*me_cmp_func)(void /*MpegEncContext*/ *s, uint8_t *blk1/*align width (8 or 16)*/, uint8_t *blk2/*align 1*/, int line_size, int h)/* __attribute__ ((const))*/; /** @@ -136,25 +138,28 @@ typedef struct DSPContext { void (*clear_blocks)(DCTELEM *blocks/*align 16*/); int (*pix_sum)(uint8_t * pix, int line_size); int (*pix_norm1)(uint8_t * pix, int line_size); - me_cmp_func sad[2]; /* identical to pix_absAxA except additional void * */ - me_cmp_func sse[2]; - me_cmp_func hadamard8_diff[2]; - me_cmp_func dct_sad[2]; - me_cmp_func quant_psnr[2]; - me_cmp_func bit[2]; - me_cmp_func rd[2]; - int (*hadamard8_abs )(uint8_t *src, int stride, int mean); +// 16x16 8x8 4x4 2x2 16x8 8x4 4x2 8x16 4x8 2x4 + + me_cmp_func sad[5]; /* identical to pix_absAxA except additional void * */ + me_cmp_func sse[5]; + me_cmp_func hadamard8_diff[5]; + me_cmp_func dct_sad[5]; + me_cmp_func quant_psnr[5]; + me_cmp_func bit[5]; + me_cmp_func rd[5]; + me_cmp_func vsad[5]; + me_cmp_func vsse[5]; - me_cmp_func me_pre_cmp[11]; - me_cmp_func me_cmp[11]; - me_cmp_func me_sub_cmp[11]; - me_cmp_func mb_cmp[11]; + me_cmp_func me_pre_cmp[5]; + me_cmp_func me_cmp[5]; + me_cmp_func me_sub_cmp[5]; + me_cmp_func mb_cmp[5]; + me_cmp_func ildct_cmp[5]; //only width 16 used - /* maybe create an array for 16/8/4/2 functions */ /** * Halfpel motion compensation with rounding (a+b+1)>>1. * this is an array[4][4] of motion compensation funcions for 4 - * horizontal blocksizes (2,4,8,16) and the 4 halfpel positions
+ * horizontal blocksizes (8,16) and the 4 halfpel positions
* *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ] * @param block destination where the result is stored * @param pixels source @@ -166,7 +171,7 @@ typedef struct DSPContext { /** * Halfpel motion compensation with rounding (a+b+1)>>1. * This is an array[4][4] of motion compensation functions for 4 - * horizontal blocksizes (2,4,8,16) and the 4 halfpel positions
+ * horizontal blocksizes (8,16) and the 4 halfpel positions
* *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ] * @param block destination into which the result is averaged (a+b+1)>>1 * @param pixels source @@ -226,14 +231,7 @@ typedef struct DSPContext { qpel_mc_func put_h264_qpel_pixels_tab[3][16]; qpel_mc_func avg_h264_qpel_pixels_tab[3][16]; - op_pixels_abs_func pix_abs16x16; - op_pixels_abs_func pix_abs16x16_x2; - op_pixels_abs_func pix_abs16x16_y2; - op_pixels_abs_func pix_abs16x16_xy2; - op_pixels_abs_func pix_abs8x8; - op_pixels_abs_func pix_abs8x8_x2; - op_pixels_abs_func pix_abs8x8_y2; - op_pixels_abs_func pix_abs8x8_xy2; + me_cmp_func pix_abs[2][4]; /* huffyuv specific */ void (*add_bytes)(uint8_t *dst/*align 16*/, uint8_t *src/*align 16*/, int w); @@ -298,6 +296,8 @@ void dsputil_init(DSPContext* p, AVCodecContext *avctx); */ void ff_block_permute(DCTELEM *block, uint8_t *permutation, const uint8_t *scantable, int last); +void ff_set_cmp(DSPContext* c, me_cmp_func *cmp, int type); + #define BYTE_VEC32(c) ((c)*0x01010101UL) static inline uint32_t rnd_avg32(uint32_t a, uint32_t b) @@ -484,12 +484,24 @@ void ff_mdct_calc(MDCTContext *s, FFTSample *out, const FFTSample *input, FFTSample *tmp); void ff_mdct_end(MDCTContext *s); -#define WARPER88_1616(name8, name16)\ -static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride){\ - return name8(s, dst , src , stride)\ - +name8(s, dst+8 , src+8 , stride)\ - +name8(s, dst +8*stride, src +8*stride, stride)\ - +name8(s, dst+8+8*stride, src+8+8*stride, stride);\ +#define WARPER8_16(name8, name16)\ +static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\ + return name8(s, dst , src , stride, h)\ + +name8(s, dst+8 , src+8 , stride, h);\ +} + +#define WARPER8_16_SQ(name8, name16)\ +static int name16(void /*MpegEncContext*/ *s, uint8_t *dst, uint8_t *src, int stride, int h){\ + int score=0;\ + score +=name8(s, dst , src , stride, 8);\ + score +=name8(s, dst+8 , src+8 , stride, 8);\ + if(h==16){\ + dst += 8*stride;\ + src += 8*stride;\ + score +=name8(s, dst , src , stride, 8);\ + score +=name8(s, dst+8 , src+8 , stride, 8);\ + }\ + return score;\ } #ifndef HAVE_LRINTF diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/dv.c b/src/add-ons/media/plugins/avcodec/libavcodec/dv.c index 48216603a0..8e041b5039 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/dv.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/dv.c @@ -921,6 +921,11 @@ static int dvvideo_decode_frame(AVCodecContext *avctx, DVVideoDecodeContext *s = avctx->priv_data; int ds, vs; const uint16_t *mb_pos_ptr; + + *data_size=0; + /* special case for last picture */ + if(buf_size==0) + return 0; s->sys = dv_frame_profile(buf); if (!s->sys || buf_size < s->sys->frame_size) diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/error_resilience.c b/src/add-ons/media/plugins/avcodec/libavcodec/error_resilience.c index adec943122..5067a248f5 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/error_resilience.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/error_resilience.c @@ -1,7 +1,7 @@ /* * Error resilience / concealment * - * Copyright (c) 2002 Michael Niedermayer + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -582,8 +582,8 @@ static int is_intra_more_likely(MpegEncContext *s){ uint8_t *mb_ptr = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize; uint8_t *last_mb_ptr= s->last_picture.data [0] + mb_x*16 + mb_y*16*s->linesize; - is_intra_likely += s->dsp.pix_abs16x16(last_mb_ptr, mb_ptr , s->linesize); - is_intra_likely -= s->dsp.pix_abs16x16(last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize); + is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr , s->linesize, 16); + is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr, last_mb_ptr+s->linesize*16, s->linesize, 16); }else{ if(IS_INTRA(s->current_picture.mb_type[mb_xy])) is_intra_likely++; @@ -673,10 +673,16 @@ void ff_er_frame_end(MpegEncContext *s){ if(s->current_picture.motion_val[0] == NULL){ int size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); + Picture *pic= s->current_picture_ptr; av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n"); - - s->current_picture.motion_val[0]= av_mallocz(size * 2 * sizeof(int16_t)); //FIXME + + for(i=0; i<2; i++){ + pic->motion_val_base[i]= av_mallocz((size+1) * 2 * sizeof(uint16_t)); //FIXME size + pic->motion_val[i]= pic->motion_val_base[i]+1; + } + pic->motion_subsample_log2= 3; + s->current_picture= *s->current_picture_ptr; } if(s->avctx->debug&FF_DEBUG_ER){ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/flicvideo.c b/src/add-ons/media/plugins/avcodec/libavcodec/flicvideo.c index b5cd366b96..248fc843a3 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/flicvideo.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/flicvideo.c @@ -1,6 +1,6 @@ /* * FLI/FLC Animation Video Decoder - * Copyright (C) 2003 the ffmpeg project + * Copyright (C) 2003, 2004 the ffmpeg project * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -60,7 +60,6 @@ typedef struct FlicDecodeContext { AVCodecContext *avctx; AVFrame frame; - AVFrame prev_frame; unsigned int palette[256]; int new_palette; @@ -82,11 +81,11 @@ static int flic_decode_init(AVCodecContext *avctx) } else if (s->avctx->extradata_size == 128) { s->fli_type = LE_16(&fli_header[4]); } else { - printf (" FLI video: expected extradata of 12 or 128 bytes\n"); + av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n"); return -1; } - s->frame.data[0] = s->prev_frame.data[0] = NULL; + s->frame.data[0] = NULL; s->new_palette = 0; return 0; @@ -126,18 +125,16 @@ static int flic_decode_frame(AVCodecContext *avctx, signed char byte_run; int pixel_skip; int pixel_countdown; - int height_countdown; unsigned char *pixels; - unsigned char *prev_pixels; s->frame.reference = 1; - if (avctx->get_buffer(avctx, &s->frame) < 0) { - fprintf(stderr, " FLI: get_buffer() failed\n"); + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + if (avctx->reget_buffer(avctx, &s->frame) < 0) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return -1; } pixels = s->frame.data[0]; - prev_pixels = s->prev_frame.data[0]; frame_size = LE_32(&buf[stream_ptr]); stream_ptr += 6; /* skip the magic number */ @@ -146,11 +143,6 @@ static int flic_decode_frame(AVCodecContext *avctx, frame_size -= 16; - /* if there is no data, copy previous frame */ - if (frame_size == 0) { - memcpy(pixels, prev_pixels, s->frame.linesize[0] * s->avctx->height); - } - /* iterate through the chunks */ while ((frame_size > 0) && (num_chunks > 0)) { chunk_size = LE_32(&buf[stream_ptr]); @@ -210,7 +202,6 @@ static int flic_decode_frame(AVCodecContext *avctx, case FLI_DELTA: y_ptr = 0; - height_countdown = s->avctx->height; compressed_lines = LE_16(&buf[stream_ptr]); stream_ptr += 2; while (compressed_lines > 0) { @@ -218,25 +209,14 @@ static int flic_decode_frame(AVCodecContext *avctx, stream_ptr += 2; if (line_packets < 0) { line_packets = -line_packets; - /* height_countdown was already decremented once on - * this iteration */ - height_countdown -= line_packets; - /* copy the skipped lines from the previous frame */ - while (line_packets--) { - memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], - s->avctx->width); - y_ptr += s->frame.linesize[0]; - } + y_ptr += line_packets * s->frame.linesize[0]; } else { - height_countdown--; compressed_lines--; pixel_ptr = y_ptr; pixel_countdown = s->avctx->width; for (i = 0; i < line_packets; i++) { /* account for the skip bytes */ pixel_skip = buf[stream_ptr++]; - memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], - pixel_skip); pixel_ptr += pixel_skip; pixel_countdown -= pixel_skip; byte_run = buf[stream_ptr++]; @@ -256,36 +236,17 @@ static int flic_decode_frame(AVCodecContext *avctx, } } - /* copy the remaining pixels in the line from the - * previous frame */ - memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], - pixel_countdown); - y_ptr += s->frame.linesize[0]; } } - - /* copy the remainder of the lines from the previous frame */ - while (height_countdown--) { - memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); - y_ptr += s->frame.linesize[0]; - } break; case FLI_LC: /* line compressed */ - height_countdown = s->avctx->height; starting_line = LE_16(&buf[stream_ptr]); stream_ptr += 2; - - /* copy from the previous frame all of the lines prior to the - * starting line */ y_ptr = 0; - height_countdown -= starting_line; - while (starting_line--) { - memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); - y_ptr += s->frame.linesize[0]; - } + y_ptr += starting_line * s->frame.linesize[0]; compressed_lines = LE_16(&buf[stream_ptr]); stream_ptr += 2; @@ -297,8 +258,6 @@ static int flic_decode_frame(AVCodecContext *avctx, for (i = 0; i < line_packets; i++) { /* account for the skip bytes */ pixel_skip = buf[stream_ptr++]; - memcpy(&pixels[pixel_ptr], - &prev_pixels[pixel_ptr], pixel_skip); pixel_ptr += pixel_skip; pixel_countdown -= pixel_skip; byte_run = buf[stream_ptr++]; @@ -317,19 +276,8 @@ static int flic_decode_frame(AVCodecContext *avctx, } } - /* copy the remainder of the line from the previous frame */ - memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr], - pixel_countdown); - y_ptr += s->frame.linesize[0]; compressed_lines--; - height_countdown--; - } - - /* copy the remainder of the lines from the previous frame */ - while (height_countdown--) { - memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width); - y_ptr += s->frame.linesize[0]; } break; @@ -357,8 +305,8 @@ static int flic_decode_frame(AVCodecContext *avctx, pixels[pixel_ptr++] = palette_idx1; pixel_countdown--; if (pixel_countdown < 0) - printf ("fli warning: pixel_countdown < 0 (%d)\n", - pixel_countdown); + av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n", + pixel_countdown); } } else { /* copy bytes if byte_run < 0 */ byte_run = -byte_run; @@ -367,8 +315,8 @@ static int flic_decode_frame(AVCodecContext *avctx, pixels[pixel_ptr++] = palette_idx1; pixel_countdown--; if (pixel_countdown < 0) - printf ("fli warning: pixel_countdown < 0 (%d)\n", - pixel_countdown); + av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n", + pixel_countdown); } } } @@ -378,13 +326,10 @@ static int flic_decode_frame(AVCodecContext *avctx, break; case FLI_COPY: - /* copy the chunk (uncompressed frame) to the ghost image and - * schedule the whole frame to be updated */ + /* copy the chunk (uncompressed frame) */ if (chunk_size - 6 > s->avctx->width * s->avctx->height) { - printf( - "FLI: in chunk FLI_COPY : source data (%d bytes) bigger than" \ - " image, skipping chunk\n", - chunk_size - 6); + av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \ + "bigger than image, skipping chunk\n", chunk_size - 6); stream_ptr += chunk_size - 6; } else { for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height; @@ -402,7 +347,7 @@ static int flic_decode_frame(AVCodecContext *avctx, break; default: - printf ("FLI: Unrecognized chunk type: %d\n", chunk_type); + av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type); break; } @@ -413,9 +358,8 @@ static int flic_decode_frame(AVCodecContext *avctx, /* by the end of the chunk, the stream ptr should equal the frame * size (minus 1, possibly); if it doesn't, issue a warning */ if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1)) - printf (" warning: processed FLI chunk where chunk size = %d\n" \ - " and final chunk ptr = %d\n", - buf_size, stream_ptr); + av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \ + "and final chunk ptr = %d\n", buf_size, stream_ptr); /* make the palette available on the way out */ // if (s->new_palette) { @@ -425,12 +369,6 @@ static int flic_decode_frame(AVCodecContext *avctx, s->new_palette = 0; } - if (s->prev_frame.data[0]) - avctx->release_buffer(avctx, &s->prev_frame); - - /* shuffle frames */ - s->prev_frame = s->frame; - *data_size=sizeof(AVFrame); *(AVFrame*)data = s->frame; @@ -441,8 +379,8 @@ static int flic_decode_end(AVCodecContext *avctx) { FlicDecodeContext *s = avctx->priv_data; - if (s->prev_frame.data[0]) - avctx->release_buffer(avctx, &s->prev_frame); + if (s->frame.data[0]) + avctx->release_buffer(avctx, &s->frame); return 0; } diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/golomb.c b/src/add-ons/media/plugins/avcodec/libavcodec/golomb.c index a696b2a766..a63f822809 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/golomb.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/golomb.c @@ -25,7 +25,7 @@ * @author Michael Niedermayer */ -#include +#include "common.h" const uint8_t ff_golomb_vlc_len[512]={ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/h263.c b/src/add-ons/media/plugins/avcodec/libavcodec/h263.c index df520545f6..de9110a4e2 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/h263.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/h263.c @@ -3,6 +3,7 @@ * Copyright (c) 2000,2001 Fabrice Bellard. * H263+ support. * Copyright (c) 2001 Juan J. Sierralta P. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -479,9 +480,9 @@ void ff_clean_mpeg4_qscales(MpegEncContext *s){ for(i=1; imb_num; i++){ int mb_xy= s->mb_index2xy[i]; - if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_INTER4V)){ - s->mb_type[mb_xy]&= ~MB_TYPE_INTER4V; - s->mb_type[mb_xy]|= MB_TYPE_INTER; + if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){ + s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V; + s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER; } } @@ -508,9 +509,9 @@ void ff_clean_mpeg4_qscales(MpegEncContext *s){ for(i=1; imb_num; i++){ int mb_xy= s->mb_index2xy[i]; - if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&MB_TYPE_DIRECT)){ - s->mb_type[mb_xy]&= ~MB_TYPE_DIRECT; - s->mb_type[mb_xy]|= MB_TYPE_BIDIR; + if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){ + s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT; + s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR; } } } @@ -523,7 +524,7 @@ void ff_clean_mpeg4_qscales(MpegEncContext *s){ */ int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){ const int mb_index= s->mb_x + s->mb_y*s->mb_stride; - const int colocated_mb_type= s->next_picture.mb_type[mb_index]; //FIXME or next? + const int colocated_mb_type= s->next_picture.mb_type[mb_index]; int xy= s->block_index[0]; uint16_t time_pp= s->pp_time; uint16_t time_pb= s->pb_time; @@ -547,18 +548,18 @@ int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){ s->mv_type = MV_TYPE_FIELD; for(i=0; i<2; i++){ if(s->top_field_first){ - time_pp= s->pp_field_time - s->field_select_table[mb_index][i] + i; - time_pb= s->pb_field_time - s->field_select_table[mb_index][i] + i; + time_pp= s->pp_field_time - s->p_field_select_table[i][mb_index] + i; + time_pb= s->pb_field_time - s->p_field_select_table[i][mb_index] + i; }else{ - time_pp= s->pp_field_time + s->field_select_table[mb_index][i] - i; - time_pb= s->pb_field_time + s->field_select_table[mb_index][i] - i; + time_pp= s->pp_field_time + s->p_field_select_table[i][mb_index] - i; + time_pb= s->pb_field_time + s->p_field_select_table[i][mb_index] - i; } - s->mv[0][i][0] = s->field_mv_table[mb_index][i][0]*time_pb/time_pp + mx; - s->mv[0][i][1] = s->field_mv_table[mb_index][i][1]*time_pb/time_pp + my; - s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->field_mv_table[mb_index][i][0] - : s->field_mv_table[mb_index][i][0]*(time_pb - time_pp)/time_pp; - s->mv[1][i][1] = my ? s->mv[0][i][1] - s->field_mv_table[mb_index][i][1] - : s->field_mv_table[mb_index][i][1]*(time_pb - time_pp)/time_pp; + s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx; + s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my; + s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0] + : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp; + s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1] + : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp; } return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED; }else{ @@ -598,9 +599,9 @@ void ff_h263_update_motion_val(MpegEncContext * s){ motion_y = s->mv[0][0][1] + s->mv[0][1][1]; motion_x = (motion_x>>1) | (motion_x&1); for(i=0; i<2; i++){ - s->field_mv_table[mb_xy][i][0]= s->mv[0][i][0]; - s->field_mv_table[mb_xy][i][1]= s->mv[0][i][1]; - s->field_select_table[mb_xy][i]= s->field_select[0][i]; + s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0]; + s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1]; + s->p_field_select_table[i][mb_xy]= s->field_select[0][i]; } } @@ -744,12 +745,14 @@ void mpeg4_encode_mb(MpegEncContext * s, if(s->pict_type==B_TYPE){ static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */ int mb_type= mb_type_table[s->mv_dir]; - + if(s->mb_x==0){ - s->last_mv[0][0][0]= - s->last_mv[0][0][1]= - s->last_mv[1][0][0]= - s->last_mv[1][0][1]= 0; + for(i=0; i<2; i++){ + s->last_mv[i][0][0]= + s->last_mv[i][0][1]= + s->last_mv[i][1][0]= + s->last_mv[i][1][1]= 0; + } } assert(s->dquant>=-2 && s->dquant<=2); @@ -803,50 +806,64 @@ void mpeg4_encode_mb(MpegEncContext * s, if(cbp) put_bits(&s->pb, 1, s->interlaced_dct); if(mb_type) // not diect mode - put_bits(&s->pb, 1, 0); // no interlaced ME yet + put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD); } if(interleaved_stats){ s->misc_bits+= get_bits_diff(s); } - switch(mb_type) - { - case 0: /* direct */ + if(mb_type == 0){ + assert(s->mv_dir & MV_DIRECT); h263_encode_motion(s, motion_x, 1); h263_encode_motion(s, motion_y, 1); s->b_count++; s->f_count++; - break; - case 1: /* bidir */ - h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); - h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); - h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); - h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); - s->last_mv[0][0][0]= s->mv[0][0][0]; - s->last_mv[0][0][1]= s->mv[0][0][1]; - s->last_mv[1][0][0]= s->mv[1][0][0]; - s->last_mv[1][0][1]= s->mv[1][0][1]; - s->b_count++; - s->f_count++; - break; - case 2: /* backward */ - h263_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); - h263_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); - s->last_mv[1][0][0]= motion_x; - s->last_mv[1][0][1]= motion_y; - s->b_count++; - break; - case 3: /* forward */ - h263_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); - h263_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); - s->last_mv[0][0][0]= motion_x; - s->last_mv[0][0][1]= motion_y; - s->f_count++; - break; - default: - av_log(s->avctx, AV_LOG_ERROR, "unknown mb type\n"); - return; + }else{ + assert(mb_type > 0 && mb_type < 4); + if(s->mv_type != MV_TYPE_FIELD){ + if(s->mv_dir & MV_DIR_FORWARD){ + h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); + h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); + s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0]; + s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1]; + s->f_count++; + } + if(s->mv_dir & MV_DIR_BACKWARD){ + h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); + h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); + s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0]; + s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1]; + s->b_count++; + } + }else{ + if(s->mv_dir & MV_DIR_FORWARD){ + put_bits(&s->pb, 1, s->field_select[0][0]); + put_bits(&s->pb, 1, s->field_select[0][1]); + } + if(s->mv_dir & MV_DIR_BACKWARD){ + put_bits(&s->pb, 1, s->field_select[1][0]); + put_bits(&s->pb, 1, s->field_select[1][1]); + } + if(s->mv_dir & MV_DIR_FORWARD){ + for(i=0; i<2; i++){ + h263_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); + h263_encode_motion(s, s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code); + s->last_mv[0][i][0]= s->mv[0][i][0]; + s->last_mv[0][i][1]= s->mv[0][i][1]*2; + } + s->f_count++; + } + if(s->mv_dir & MV_DIR_BACKWARD){ + for(i=0; i<2; i++){ + h263_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code); + h263_encode_motion(s, s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code); + s->last_mv[1][i][0]= s->mv[1][i][0]; + s->last_mv[1][i][1]= s->mv[1][i][1]*2; + } + s->b_count++; + } + } } if(interleaved_stats){ @@ -861,6 +878,7 @@ void mpeg4_encode_mb(MpegEncContext * s, if(interleaved_stats){ s->p_tex_bits+= get_bits_diff(s); } + }else{ /* s->pict_type==B_TYPE */ cbp= get_p_cbp(s, block, motion_x, motion_y); @@ -889,7 +907,7 @@ void mpeg4_encode_mb(MpegEncContext * s, if(pic==NULL || pic->pict_type!=B_TYPE) break; b_pic= pic->data[0] + offset + 16; //FIXME +16 - diff= s->dsp.pix_abs16x16(p_pic, b_pic, s->linesize); + diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16); if(diff>s->qscale*70){ //FIXME check that 70 is optimal s->mb_skiped=0; break; @@ -929,7 +947,7 @@ void mpeg4_encode_mb(MpegEncContext * s, if(!s->progressive_sequence){ if(cbp) put_bits(pb2, 1, s->interlaced_dct); - put_bits(pb2, 1, 0); // no interlaced ME yet + put_bits(pb2, 1, 0); } if(interleaved_stats){ @@ -941,7 +959,38 @@ void mpeg4_encode_mb(MpegEncContext * s, h263_encode_motion(s, motion_x - pred_x, s->f_code); h263_encode_motion(s, motion_y - pred_y, s->f_code); + }else if(s->mv_type==MV_TYPE_FIELD){ + if(s->dquant) cbpc+= 8; + put_bits(&s->pb, + inter_MCBPC_bits[cbpc], + inter_MCBPC_code[cbpc]); + + put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]); + if(s->dquant) + put_bits(pb2, 2, dquant_code[s->dquant+2]); + + assert(!s->progressive_sequence); + if(cbp) + put_bits(pb2, 1, s->interlaced_dct); + put_bits(pb2, 1, 1); + + if(interleaved_stats){ + s->misc_bits+= get_bits_diff(s); + } + + /* motion vectors: 16x8 interlaced mode */ + h263_pred_motion(s, 0, &pred_x, &pred_y); + pred_y /=2; + + put_bits(&s->pb, 1, s->field_select[0][0]); + put_bits(&s->pb, 1, s->field_select[0][1]); + + h263_encode_motion(s, s->mv[0][0][0] - pred_x, s->f_code); + h263_encode_motion(s, s->mv[0][0][1] - pred_y, s->f_code); + h263_encode_motion(s, s->mv[0][1][0] - pred_x, s->f_code); + h263_encode_motion(s, s->mv[0][1][1] - pred_y, s->f_code); }else{ + assert(s->mv_type==MV_TYPE_8X8); put_bits(&s->pb, inter_MCBPC_bits[cbpc+16], inter_MCBPC_code[cbpc+16]); @@ -1081,6 +1130,8 @@ void h263_encode_mb(MpegEncContext * s, s->misc_bits++; s->last_bits++; } + s->skip_count++; + return; } put_bits(&s->pb, 1, 0); /* mb coded */ @@ -1548,9 +1599,9 @@ int16_t *h263_pred_motion2(MpegEncContext * s, int block, int dir, static const int off[4]= {2, 1, 1, -1}; wrap = s->b8_stride; - xy = s->mb_x + s->mb_y * wrap; + xy = 2*(s->mb_x + s->mb_y * wrap); - mot_val = s->current_picture.motion_val[0][dir] + xy; + mot_val = s->current_picture.motion_val[dir] + xy; A = mot_val[ - 1]; /* special case for first (slice) line */ @@ -1983,7 +2034,7 @@ static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) if (level == 128) //FIXME check rv10 put_bits(&s->pb, 8, 0xff); else - put_bits(&s->pb, 8, level & 0xff); + put_bits(&s->pb, 8, level); i = 1; } else { i = 0; @@ -2053,7 +2104,7 @@ static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) assert(slevel != 0); - if(slevel < 128 && slevel > -128) + if(level < 128) put_bits(&s->pb, 8, slevel & 0xff); else{ put_bits(&s->pb, 8, 128); @@ -2061,8 +2112,7 @@ static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n) put_bits(&s->pb, 6, (slevel>>5)&0x3f); } }else{ - if(slevel < 64 && slevel > -64) { - /* 7-bit level */ + if(level < 64) { // 7-bit level put_bits(&s->pb, 1, 0); put_bits(&s->pb, 1, last); put_bits(&s->pb, 6, run); @@ -2123,11 +2173,18 @@ void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){ static void mpeg4_encode_gop_header(MpegEncContext * s){ int hours, minutes, seconds; + int64_t time; put_bits(&s->pb, 16, 0); put_bits(&s->pb, 16, GOP_STARTCODE); - seconds= s->time/s->time_increment_resolution; + if(s->current_picture_ptr->pts && s->reordered_input_picture[1]){ + time= FFMIN(s->reordered_input_picture[1]->pts, s->current_picture_ptr->pts); + time= (time*s->time_increment_resolution + 500*1000)/(1000*1000); + }else + time= av_rescale(s->current_picture_ptr->coded_picture_number*(int64_t)s->avctx->frame_rate_base, s->time_increment_resolution, s->avctx->frame_rate); + + seconds= time/s->time_increment_resolution; minutes= seconds/60; seconds %= 60; hours= minutes/60; minutes %= 60; hours%=24; @@ -2137,8 +2194,10 @@ static void mpeg4_encode_gop_header(MpegEncContext * s){ put_bits(&s->pb, 1, 1); put_bits(&s->pb, 6, seconds); - put_bits(&s->pb, 1, 0); //closed gov == NO + put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP)); put_bits(&s->pb, 1, 0); //broken link == NO + + s->last_time_base= time / s->time_increment_resolution; ff_mpeg4_stuffing(&s->pb); } @@ -3815,15 +3874,15 @@ int ff_h263_decode_mb(MpegEncContext *s, } else if(s->pict_type==B_TYPE) { int mb_type; const int stride= s->b8_stride; - int16_t *mot_val0 = s->current_picture.motion_val[0][ s->mb_x + s->mb_y*stride ]; - int16_t *mot_val1 = s->current_picture.motion_val[1][ s->mb_x + s->mb_y*stride ]; + int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ]; + int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ]; // const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride; //FIXME ugly - mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+stride]= mot_val0[2+stride]= 0; - mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+stride]= mot_val0[3+stride]= 0; - mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+stride]= mot_val1[2+stride]= 0; - mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+stride]= mot_val1[3+stride]= 0; + mot_val0[0 ]= mot_val0[2 ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]= + mot_val0[1 ]= mot_val0[3 ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]= + mot_val1[0 ]= mot_val1[2 ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]= + mot_val1[1 ]= mot_val1[3 ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0; do{ mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2); @@ -3877,22 +3936,24 @@ int ff_h263_decode_mb(MpegEncContext *s, mx = h263_decode_motion(s, mx, 1); my = h263_decode_motion(s, my, 1); + s->mv[0][0][0] = mx; s->mv[0][0][1] = my; - mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx; - mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my; + mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; + mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; } if(USES_LIST(mb_type, 1)){ int16_t *mot_val= h263_pred_motion2(s, 0, 1, &mx, &my); s->mv_dir |= MV_DIR_BACKWARD; - + mx = h263_decode_motion(s, mx, 1); my = h263_decode_motion(s, my, 1); + s->mv[1][0][0] = mx; s->mv[1][0][1] = my; - mot_val[0 ]= mot_val[2 ]= mot_val[0+stride]= mot_val[2+stride]= mx; - mot_val[1 ]= mot_val[3 ]= mot_val[1+stride]= mot_val[3+stride]= my; + mot_val[0 ]= mot_val[2 ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx; + mot_val[1 ]= mot_val[3 ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my; } } @@ -4381,7 +4442,8 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block, level = get_bits(&s->gb, 8); if((level&0x7F) == 0){ av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y); - return -1; + if(s->error_resilience >= FF_ER_COMPLIANT) + return -1; } if (level == 255) level = 128; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/h263data.h b/src/add-ons/media/plugins/avcodec/libavcodec/h263data.h index 2dc6e40d67..4da105ffc5 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/h263data.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/h263data.h @@ -51,18 +51,18 @@ static const int h263_mb_type_b_map[15]= { MB_TYPE_DIRECT2 | MB_TYPE_L0L1, MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP, MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT, - MB_TYPE_L0, - MB_TYPE_L0 | MB_TYPE_CBP, - MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT, - MB_TYPE_L1, - MB_TYPE_L1 | MB_TYPE_CBP, - MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT, - MB_TYPE_L0L1, - MB_TYPE_L0L1 | MB_TYPE_CBP, - MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT, + MB_TYPE_L0 | MB_TYPE_16x16, + MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_16x16, + MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16, + MB_TYPE_L1 | MB_TYPE_16x16, + MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_16x16, + MB_TYPE_L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16, + MB_TYPE_L0L1 | MB_TYPE_16x16, + MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_16x16, + MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16, 0, //stuffing - MB_TYPE_INTRA | MB_TYPE_CBP, - MB_TYPE_INTRA | MB_TYPE_CBP | MB_TYPE_QUANT, + MB_TYPE_INTRA4x4 | MB_TYPE_CBP, + MB_TYPE_INTRA4x4 | MB_TYPE_CBP | MB_TYPE_QUANT, }; const uint8_t cbpc_b_tab[4][2] = { diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/h263dec.c b/src/add-ons/media/plugins/avcodec/libavcodec/h263dec.c index 1372e4a7e1..88db359fe9 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/h263dec.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/h263dec.c @@ -1,6 +1,7 @@ /* * H.263 decoder * Copyright (c) 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -392,6 +393,7 @@ uint64_t time= rdtsc(); printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); #endif s->flags= avctx->flags; + s->flags2= avctx->flags2; *data_size = 0; @@ -424,9 +426,10 @@ uint64_t time= rdtsc(); return buf_size; } + retry: - if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder + if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8); }else init_get_bits(&s->gb, buf, buf_size*8); @@ -677,21 +680,26 @@ retry: /* divx 5.01+ bistream reorder stuff */ if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){ int current_pos= get_bits_count(&s->gb)>>3; + int startcode_found=0; if( buf_size - current_pos > 5 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){ int i; - int startcode_found=0; for(i=current_pos; ibitstream_buffer, buf + current_pos, buf_size - current_pos); - s->bitstream_buffer_size= buf_size - current_pos; - } + } + if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style + startcode_found=1; + current_pos=0; + } + + if(startcode_found){ + memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); + s->bitstream_buffer_size= buf_size - current_pos; } } @@ -703,10 +711,10 @@ assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); assert(s->current_picture.pict_type == s->pict_type); if(s->pict_type==B_TYPE || s->low_delay){ *pict= *(AVFrame*)&s->current_picture; - ff_print_debug_info(s, s->current_picture_ptr); + ff_print_debug_info(s, pict); } else { *pict= *(AVFrame*)&s->last_picture; - ff_print_debug_info(s, s->last_picture_ptr); + ff_print_debug_info(s, pict); } /* Return the Picture timestamp as the frame number */ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/h264.c b/src/add-ons/media/plugins/avcodec/libavcodec/h264.c index afd9302ce0..43ed13e99f 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/h264.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/h264.c @@ -2158,6 +2158,7 @@ static void common_init(H264Context *h){ init_pred_ptrs(h); + s->unrestricted_mv=1; s->decode=1; //FIXME } @@ -4130,6 +4131,7 @@ static int decode_frame(AVCodecContext *avctx, int buf_index; s->flags= avctx->flags; + s->flags2= avctx->flags2; *data_size = 0; @@ -4171,7 +4173,7 @@ static int decode_frame(AVCodecContext *avctx, } *pict= *(AVFrame*)&s->current_picture; //FIXME - ff_print_debug_info(s, s->current_picture_ptr); + ff_print_debug_info(s, pict); assert(pict->data[0]); //printf("out %d\n", (int)pict->data[0]); #if 0 //? diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/huffyuv.c b/src/add-ons/media/plugins/avcodec/libavcodec/huffyuv.c index 41294c85fc..d180d6aeee 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/huffyuv.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/huffyuv.c @@ -30,10 +30,6 @@ #include "avcodec.h" #include "dsputil.h" -#ifndef INT64_MAX -#define INT64_MAX 9223372036854775807LL -#endif - #define VLC_BITS 11 typedef enum Predictor{ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/interplayvideo.c b/src/add-ons/media/plugins/avcodec/libavcodec/interplayvideo.c index f354b734c3..06816ba3e9 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/interplayvideo.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/interplayvideo.c @@ -900,6 +900,11 @@ static int ipvideo_decode_frame(AVCodecContext *avctx, IpvideoContext *s = avctx->priv_data; AVPaletteControl *palette_control = avctx->palctrl; + /* compressed buffer needs to be large enough to at least hold an entire + * decoding map */ + if (buf_size < s->decoding_map_size) + return buf_size; + s->decoding_map = buf; s->buf = buf + s->decoding_map_size; s->size = buf_size - s->decoding_map_size; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c index a8dbc2bbd4..1a948aa56f 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mjpeg.c @@ -855,7 +855,6 @@ static int mjpeg_decode_init(AVCodecContext *avctx) /* ugly way to get the idct & scantable FIXME */ memset(&s2, 0, sizeof(MpegEncContext)); - s2.flags= avctx->flags; s2.avctx= avctx; // s2->out_format = FMT_MJPEG; s2.width = 8; @@ -1052,8 +1051,10 @@ static int mjpeg_decode_sof(MJpegDecodeContext *s) case 0x11: if(s->rgb){ s->avctx->pix_fmt = PIX_FMT_RGBA32; - }else + }else if(s->nb_components==3) s->avctx->pix_fmt = PIX_FMT_YUV444P; + else + s->avctx->pix_fmt = PIX_FMT_GRAY8; break; case 0x21: s->avctx->pix_fmt = PIX_FMT_YUV422P; @@ -1372,7 +1373,7 @@ static int mjpeg_decode_sos(MJpegDecodeContext *s) return -1; } /* XXX: only interleaved scan accepted */ - if (nb_components != 3) + if (nb_components != s->nb_components) { dprintf("decode_sos: components(%d) mismatch\n", nb_components); return -1; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c index 1a449cedd0..e8641790b2 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est.c @@ -1,7 +1,7 @@ /* * Motion estimation * Copyright (c) 2000,2001 Fabrice Bellard. - * Copyright (c) 2002-2003 Michael Niedermayer + * Copyright (c) 2002-2004 Michael Niedermayer * * * This library is free software; you can redistribute it and/or @@ -46,9 +46,9 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, - int xmin, int ymin, int xmax, int ymax, - int pred_x, int pred_y, Picture *picture, - int n, int size, uint8_t * const mv_penalty); + int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[6], int stride, int uvstride, + int size, int h, uint8_t * const mv_penalty); static inline int update_map_generation(MpegEncContext * s) { @@ -78,20 +78,21 @@ static int minima_cmp(const void *a, const void *b){ #define RENAME(a) simple_ ## a #define CMP(d, x, y, size)\ -d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride); +d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h); #define CMP_HPEL(d, dx, dy, x, y, size)\ {\ const int dxy= (dx) + 2*(dy);\ - hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ + hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\ + d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ } + #define CMP_QPEL(d, dx, dy, x, y, size)\ {\ const int dxy= (dx) + 4*(dy);\ qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ + d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ } #include "motion_est_template.c" @@ -105,29 +106,29 @@ d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride); #define RENAME(a) simple_chroma_ ## a #define CMP(d, x, y, size)\ -d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);\ +d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride, h);\ if(chroma_cmp){\ int dxy= ((x)&1) + 2*((y)&1);\ int c= ((x)>>1) + ((y)>>1)*uvstride;\ \ - chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ - d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride);\ - chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ - d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride);\ + chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ + d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride, h>>1);\ + chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ + d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride, h>>1);\ } #define CMP_HPEL(d, dx, dy, x, y, size)\ {\ const int dxy= (dx) + 2*(dy);\ - hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ + hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, h);\ + d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ if(chroma_cmp_sub){\ int cxy= (dxy) | ((x)&1) | (2*((y)&1));\ int c= ((x)>>1) + ((y)>>1)*uvstride;\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\ + chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ + d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\ + chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ + d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\ }\ } @@ -135,7 +136,7 @@ if(chroma_cmp){\ {\ const int dxy= (dx) + 4*(dy);\ qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\ - d = cmp_sub(s, s->me.scratchpad, src_y, stride);\ + d = cmp_sub(s, s->me.scratchpad, src_y, stride, h);\ if(chroma_cmp_sub){\ int cxy, c;\ int cx= (4*(x) + (dx))/2;\ @@ -144,10 +145,10 @@ if(chroma_cmp){\ cy= (cy>>1)|(cy&1);\ cxy= (cx&1) + 2*(cy&1);\ c= ((cx)>>1) + ((cy)>>1)*uvstride;\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\ - chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\ - d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\ + chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, h>>1);\ + d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride, h>>1);\ + chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, h>>1);\ + d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride, h>>1);\ }\ } @@ -178,7 +179,7 @@ if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*yma \ uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ hpel_put[1][fxy](dst, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 8);\ - hpel_avg[1][bxy](dst, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 8);\ + hpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 8);\ }\ }else{\ int fx = s->me.direct_basis_mv[0][0] + hx;\ @@ -198,9 +199,9 @@ if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*yma assert((by>>1) + 16*s->mb_y <= s->height);\ \ hpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 16);\ - hpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 16);\ + hpel_avg[0][bxy](s->me.scratchpad, (ref_data[3]) + (bx>>1) + (by>>1)*(stride), stride, 16);\ }\ - d = cmp_func(s, s->me.scratchpad, src_y, stride);\ + d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\ }else\ d= 256*256*256*32; @@ -238,7 +239,7 @@ if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*yma \ uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\ qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\ - qpel_avg[1][bxy](dst, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\ + qpel_avg[1][bxy](dst, (ref_data[3]) + (bx>>2) + (by>>2)*(stride), stride);\ }\ }else{\ int fx = s->me.direct_basis_mv[0][0] + qx;\ @@ -252,12 +253,12 @@ if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*yma qpel_put[1][fxy](s->me.scratchpad + 8 , (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8 , stride);\ qpel_put[1][fxy](s->me.scratchpad + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8*stride, stride);\ qpel_put[1][fxy](s->me.scratchpad + 8 + 8*stride, (ref_y ) + (fx>>2) + (fy>>2)*(stride) + 8 + 8*stride, stride);\ - qpel_avg[1][bxy](s->me.scratchpad , (ref2_y) + (bx>>2) + (by>>2)*(stride) , stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8 , (ref2_y) + (bx>>2) + (by>>2)*(stride) + 8 , stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8*stride, (ref2_y) + (bx>>2) + (by>>2)*(stride) + 8*stride, stride);\ - qpel_avg[1][bxy](s->me.scratchpad + 8 + 8*stride, (ref2_y) + (bx>>2) + (by>>2)*(stride) + 8 + 8*stride, stride);\ + qpel_avg[1][bxy](s->me.scratchpad , (ref_data[3]) + (bx>>2) + (by>>2)*(stride) , stride);\ + qpel_avg[1][bxy](s->me.scratchpad + 8 , (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8 , stride);\ + qpel_avg[1][bxy](s->me.scratchpad + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8*stride, stride);\ + qpel_avg[1][bxy](s->me.scratchpad + 8 + 8*stride, (ref_data[3]) + (bx>>2) + (by>>2)*(stride) + 8 + 8*stride, stride);\ }\ - d = cmp_func(s, s->me.scratchpad, src_y, stride);\ + d = cmp_func(s, s->me.scratchpad, src_y, stride, 16);\ }else\ d= 256*256*256*32; @@ -276,56 +277,6 @@ if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*yma #undef INIT #undef CMP__DIRECT - -static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride){ - return 0; -} - -static void set_cmp(MpegEncContext *s, me_cmp_func *cmp, int type){ - DSPContext* c= &s->dsp; - int i; - - memset(cmp, 0, sizeof(void*)*11); - - switch(type&0xFF){ - case FF_CMP_SAD: - cmp[0]= c->sad[0]; - cmp[1]= c->sad[1]; - break; - case FF_CMP_SATD: - cmp[0]= c->hadamard8_diff[0]; - cmp[1]= c->hadamard8_diff[1]; - break; - case FF_CMP_SSE: - cmp[0]= c->sse[0]; - cmp[1]= c->sse[1]; - break; - case FF_CMP_DCT: - cmp[0]= c->dct_sad[0]; - cmp[1]= c->dct_sad[1]; - break; - case FF_CMP_PSNR: - cmp[0]= c->quant_psnr[0]; - cmp[1]= c->quant_psnr[1]; - break; - case FF_CMP_BIT: - cmp[0]= c->bit[0]; - cmp[1]= c->bit[1]; - break; - case FF_CMP_RD: - cmp[0]= c->rd[0]; - cmp[1]= c->rd[1]; - break; - case FF_CMP_ZERO: - for(i=0; i<7; i++){ - cmp[i]= zero_cmp; - } - break; - default: - av_log(s->avctx, AV_LOG_ERROR,"internal error in cmp function selection\n"); - } -} - static inline int get_penalty_factor(MpegEncContext *s, int type){ switch(type&0xFF){ default: @@ -346,10 +297,10 @@ static inline int get_penalty_factor(MpegEncContext *s, int type){ } void ff_init_me(MpegEncContext *s){ - set_cmp(s, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp); - set_cmp(s, s->dsp.me_cmp, s->avctx->me_cmp); - set_cmp(s, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); - set_cmp(s, s->dsp.mb_cmp, s->avctx->mb_cmp); + ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, s->avctx->me_pre_cmp); + ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp); + ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp); + ff_set_cmp(&s->dsp, s->dsp.mb_cmp, s->avctx->mb_cmp); if(s->flags&CODEC_FLAG_QPEL){ if(s->avctx->me_sub_cmp&FF_CMP_CHROMA) @@ -362,7 +313,7 @@ void ff_init_me(MpegEncContext *s){ else if( s->avctx->me_sub_cmp == FF_CMP_SAD && s->avctx-> me_cmp == FF_CMP_SAD && s->avctx-> mb_cmp == FF_CMP_SAD) - s->me.sub_motion_search= sad_hpel_motion_search; + s->me.sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles else s->me.sub_motion_search= simple_hpel_motion_search; } @@ -370,9 +321,11 @@ void ff_init_me(MpegEncContext *s){ if(s->avctx->me_cmp&FF_CMP_CHROMA){ s->me.motion_search[0]= simple_chroma_epzs_motion_search; s->me.motion_search[1]= simple_chroma_epzs_motion_search4; + s->me.motion_search[4]= simple_chroma_epzs_motion_search2; }else{ s->me.motion_search[0]= simple_epzs_motion_search; s->me.motion_search[1]= simple_epzs_motion_search4; + s->me.motion_search[4]= simple_epzs_motion_search2; } if(s->avctx->me_pre_cmp&FF_CMP_CHROMA){ @@ -453,8 +406,8 @@ static int full_motion_search(MpegEncContext * s, my = 0; for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) { - d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, - s->linesize); + d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, + s->linesize, 16); if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < @@ -518,7 +471,7 @@ static int log_motion_search(MpegEncContext * s, do { for (y = y1; y <= y2; y += range) { for (x = x1; x <= x2; x += range) { - d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); + d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { dmin = d; mx = x; @@ -598,7 +551,7 @@ static int phods_motion_search(MpegEncContext * s, lastx = x; for (x = x1; x <= x2; x += range) { - d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); + d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { dminx = d; mx = x; @@ -607,7 +560,7 @@ static int phods_motion_search(MpegEncContext * s, x = lastx; for (y = y1; y <= y2; y += range) { - d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize); + d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16); if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) { dminy = d; my = y; @@ -651,35 +604,25 @@ static int phods_motion_search(MpegEncContext * s, #define CHECK_SAD_HALF_MV(suffix, x, y) \ {\ - d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\ + d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\ d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\ COPY3_IF_LT(dminh, d, dx, x, dy, y)\ } static inline int sad_hpel_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, - int xmin, int ymin, int xmax, int ymax, - int pred_x, int pred_y, Picture *picture, - int n, int size, uint8_t * const mv_penalty) + int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[6], int stride, int uvstride, + int size, int h, uint8_t * const mv_penalty) { - uint8_t *ref_picture= picture->data[0]; uint32_t *score_map= s->me.score_map; const int penalty_factor= s->me.sub_penalty_factor; - int mx, my, xx, yy, dminh; + int mx, my, dminh; uint8_t *pix, *ptr; - op_pixels_abs_func pix_abs_x2; - op_pixels_abs_func pix_abs_y2; - op_pixels_abs_func pix_abs_xy2; - - if(size==0){ - pix_abs_x2 = s->dsp.pix_abs16x16_x2; - pix_abs_y2 = s->dsp.pix_abs16x16_y2; - pix_abs_xy2= s->dsp.pix_abs16x16_xy2; - }else{ - pix_abs_x2 = s->dsp.pix_abs8x8_x2; - pix_abs_y2 = s->dsp.pix_abs8x8_y2; - pix_abs_xy2= s->dsp.pix_abs8x8_xy2; - } + const int xmin= s->me.xmin; + const int ymin= s->me.ymin; + const int xmax= s->me.xmax; + const int ymax= s->me.ymax; if(s->me.skip){ // printf("S"); @@ -689,13 +632,11 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, } // printf("N"); - xx = 16 * s->mb_x + 8*(n&1); - yy = 16 * s->mb_y + 8*(n>>1); - pix = s->new_picture.data[0] + (yy * s->linesize) + xx; + pix = src_data[0]; mx = *mx_ptr; my = *my_ptr; - ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx); + ptr = ref_data[0] + (my * stride) + mx; dminh = dmin; @@ -715,16 +656,16 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, pen_x= pred_x + mx; pen_y= pred_y + my; - ptr-= s->linesize; + ptr-= stride; if(t<=b){ CHECK_SAD_HALF_MV(y2 , 0, -1) if(l<=r){ CHECK_SAD_HALF_MV(xy2, -1, -1) if(t+r<=b+l){ CHECK_SAD_HALF_MV(xy2, +1, -1) - ptr+= s->linesize; + ptr+= stride; }else{ - ptr+= s->linesize; + ptr+= stride; CHECK_SAD_HALF_MV(xy2, -1, +1) } CHECK_SAD_HALF_MV(x2 , -1, 0) @@ -732,9 +673,9 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, CHECK_SAD_HALF_MV(xy2, +1, -1) if(t+l<=b+r){ CHECK_SAD_HALF_MV(xy2, -1, -1) - ptr+= s->linesize; + ptr+= stride; }else{ - ptr+= s->linesize; + ptr+= stride; CHECK_SAD_HALF_MV(xy2, +1, +1) } CHECK_SAD_HALF_MV(x2 , +1, 0) @@ -743,9 +684,9 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, if(l<=r){ if(t+l<=b+r){ CHECK_SAD_HALF_MV(xy2, -1, -1) - ptr+= s->linesize; + ptr+= stride; }else{ - ptr+= s->linesize; + ptr+= stride; CHECK_SAD_HALF_MV(xy2, +1, +1) } CHECK_SAD_HALF_MV(x2 , -1, 0) @@ -753,9 +694,9 @@ static inline int sad_hpel_motion_search(MpegEncContext * s, }else{ if(t+r<=b+l){ CHECK_SAD_HALF_MV(xy2, +1, -1) - ptr+= s->linesize; + ptr+= stride; }else{ - ptr+= s->linesize; + ptr+= stride; CHECK_SAD_HALF_MV(xy2, -1, +1) } CHECK_SAD_HALF_MV(x2 , +1, 0) @@ -802,35 +743,41 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) /** * get fullpel ME search limits. - * @param range the approximate search range for the old ME code, unused for EPZS and newer */ -static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax) +static inline void get_limits(MpegEncContext *s, int x, int y) { - if(s->avctx->me_range) *range= s->avctx->me_range >> 1; - else *range= 16; - +/* + if(s->avctx->me_range) s->me.range= s->avctx->me_range >> 1; + else s->me.range= 16; +*/ if (s->unrestricted_mv) { - *xmin = -16; - *ymin = -16; - *xmax = s->mb_width*16; - *ymax = s->mb_height*16; + s->me.xmin = - x - 16; + s->me.ymin = - y - 16; + s->me.xmax = - x + s->mb_width *16; + s->me.ymax = - y + s->mb_height*16; } else { - *xmin = 0; - *ymin = 0; - *xmax = s->mb_width*16 - 16; - *ymax = s->mb_height*16 - 16; + s->me.xmin = - x; + s->me.ymin = - y; + s->me.xmax = - x + s->mb_width *16 - 16; + s->me.ymax = - y + s->mb_height*16 - 16; } - - //FIXME try to limit x/y min/max if me_range is set } -static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift) +static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) { + const int size= 1; + const int h=8; int block; int P[10][2]; int dmin_sum=0, mx4_sum=0, my4_sum=0; uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; int same=1; + const int stride= s->linesize; + const int uvstride= s->uvlinesize; + const int xmin= s->me.xmin; + const int ymin= s->me.ymin; + const int xmax= s->me.xmax; + const int ymax= s->me.ymax; for(block=0; block<4; block++){ int mx4, my4; @@ -839,23 +786,23 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma static const int off[4]= {2, 1, 1, -1}; const int mot_stride = s->block_wrap[0]; const int mot_xy = s->block_index[block]; -// const int block_x= (block&1); -// const int block_y= (block>>1); -#if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way - const int rel_xmin4= xmin; - const int rel_xmax4= xmax; - const int rel_ymin4= ymin; - const int rel_ymax4= ymax; -#else - const int rel_xmin4= xmin - block_x*8; - const int rel_xmax4= xmax - block_x*8 + 8; - const int rel_ymin4= ymin - block_y*8; - const int rel_ymax4= ymax - block_y*8 + 8; -#endif + const int block_x= (block&1); + const int block_y= (block>>1); + uint8_t *src_data[3]= { + s->new_picture.data[0] + 8*(2*s->mb_x + block_x) + stride *8*(2*s->mb_y + block_y), //FIXME chroma? + s->new_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y), + s->new_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y) + }; + uint8_t *ref_data[3]= { + s->last_picture.data[0] + 8*(2*s->mb_x + block_x) + stride *8*(2*s->mb_y + block_y), //FIXME chroma? + s->last_picture.data[1] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y), + s->last_picture.data[2] + 4*(2*s->mb_x + block_x) + uvstride*4*(2*s->mb_y + block_y) + }; + P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; - if(P_LEFT[0] > (rel_xmax4< (s->me.xmax<me.xmax<mb_y == 0 && block<2) { @@ -866,10 +813,10 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0]; P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1]; - if(P_TOP[1] > (rel_ymax4< (rel_xmax4< (rel_ymax4< (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.xmax<me.xmax< (s->me.ymax<me.ymax<me.motion_search[1](s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, - &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); + dmin4 = s->me.motion_search[1](s, &mx4, &my4, P, pred_x4, pred_y4, + src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); - dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, - pred_x4, pred_y4, &s->last_picture, block, 1, mv_penalty); + dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, + pred_x4, pred_y4, src_data, ref_data, stride, uvstride, size, h, mv_penalty); - if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ + if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0] + && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE){ int dxy; - const int offset= ((block&1) + (block>>1)*s->linesize)*8; + const int offset= ((block&1) + (block>>1)*stride)*8; uint8_t *dest_y = s->me.scratchpad + offset; if(s->quarter_sample){ - uint8_t *ref= s->last_picture.data[0] + (s->mb_x*16 + (mx4>>2)) + (s->mb_y*16 + (my4>>2))*s->linesize + offset; + uint8_t *ref= ref_data[0] + (mx4>>2) + (my4>>2)*stride; dxy = ((my4 & 3) << 2) | (mx4 & 3); if(s->no_rounding) s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , s->linesize); else - s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , s->linesize); + s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride); }else{ - uint8_t *ref= s->last_picture.data[0] + (s->mb_x*16 + (mx4>>1)) + (s->mb_y*16 + (my4>>1))*s->linesize + offset; + uint8_t *ref= ref_data[0] + (mx4>>1) + (my4>>1)*stride; dxy = ((my4 & 1) << 1) | (mx4 & 1); if(s->no_rounding) - s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , s->linesize, 8); + s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h); else - s->dsp.put_pixels_tab [1][dxy](dest_y , ref , s->linesize, 8); + s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h); } dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*s->me.mb_penalty_factor; }else @@ -937,7 +885,7 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma return INT_MAX; if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ - dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*s->linesize, s->me.scratchpad, s->linesize); + dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, s->me.scratchpad, stride, 16); } if(s->avctx->mb_cmp&FF_CMP_CHROMA){ @@ -959,8 +907,8 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma s->dsp.put_pixels_tab [1][dxy](s->me.scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); } - dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad , s->uvlinesize); - dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad+8, s->uvlinesize); + dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad , s->uvlinesize, 8); + dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, s->me.scratchpad+8, s->uvlinesize, 8); } switch(s->avctx->mb_cmp&0xFF){ @@ -973,13 +921,135 @@ static inline int h263_mv4_search(MpegEncContext *s, int xmin, int ymin, int xma } } +static int interlaced_search(MpegEncContext *s, uint8_t *frame_src_data[3], uint8_t *frame_ref_data[3], + int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int f_code, int mx, int my) +{ + const int size=0; + const int h=8; + int block; + int P[10][2]; + uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; + int same=1; + const int stride= 2*s->linesize; + const int uvstride= 2*s->uvlinesize; + int dmin_sum= 0; + const int mot_stride= s->mb_stride; + const int xy= s->mb_x + s->mb_y*mot_stride; + + s->me.ymin>>=1; + s->me.ymax>>=1; + + for(block=0; block<2; block++){ + int field_select; + int best_dmin= INT_MAX; + int best_field= -1; + + uint8_t *src_data[3]= { + frame_src_data[0] + s-> linesize*block, + frame_src_data[1] + s->uvlinesize*block, + frame_src_data[2] + s->uvlinesize*block + }; + + for(field_select=0; field_select<2; field_select++){ + int dmin, mx_i, my_i, pred_x, pred_y; + uint8_t *ref_data[3]= { + frame_ref_data[0] + s-> linesize*field_select, + frame_ref_data[1] + s->uvlinesize*field_select, + frame_ref_data[2] + s->uvlinesize*field_select + }; + int16_t (*mv_table)[2]= mv_tables[block][field_select]; + + P_LEFT[0] = mv_table[xy - 1][0]; + P_LEFT[1] = mv_table[xy - 1][1]; + if(P_LEFT[0] > (s->me.xmax<<1)) P_LEFT[0] = (s->me.xmax<<1); + + pred_x= P_LEFT[0]; + pred_y= P_LEFT[1]; + + if(s->mb_y){ + P_TOP[0] = mv_table[xy - mot_stride][0]; + P_TOP[1] = mv_table[xy - mot_stride][1]; + P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0]; + P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1]; + if(P_TOP[1] > (s->me.ymax<<1)) P_TOP[1] = (s->me.ymax<<1); + if(P_TOPRIGHT[0] < (s->me.xmin<<1)) P_TOPRIGHT[0]= (s->me.xmin<<1); + if(P_TOPRIGHT[0] > (s->me.xmax<<1)) P_TOPRIGHT[0]= (s->me.xmax<<1); + if(P_TOPRIGHT[1] > (s->me.ymax<<1)) P_TOPRIGHT[1]= (s->me.ymax<<1); + + P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); + P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); + } + P_MV1[0]= mx; //FIXME not correct if block != field_select + P_MV1[1]= my / 2; + + dmin = s->me.motion_search[4](s, &mx_i, &my_i, P, pred_x, pred_y, + src_data, ref_data, stride, uvstride, mv_table, (1<<16)>>1, mv_penalty); + + dmin= s->me.sub_motion_search(s, &mx_i, &my_i, dmin, + pred_x, pred_y, src_data, ref_data, stride, uvstride, size, h, mv_penalty); + + mv_table[xy][0]= mx_i; + mv_table[xy][1]= my_i; + + if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0] + && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE){ + int dxy; + + //FIXME chroma ME + uint8_t *ref= ref_data[0] + (mx_i>>1) + (my_i>>1)*stride; + dxy = ((my_i & 1) << 1) | (mx_i & 1); + + if(s->no_rounding){ + s->dsp.put_no_rnd_pixels_tab[size][dxy](s->me.scratchpad, ref , stride, h); + }else{ + s->dsp.put_pixels_tab [size][dxy](s->me.scratchpad, ref , stride, h); + } + dmin= s->dsp.mb_cmp[size](s, src_data[0], s->me.scratchpad, stride, h); + dmin+= (mv_penalty[mx_i-pred_x] + mv_penalty[my_i-pred_y] + 1)*s->me.mb_penalty_factor; + }else + dmin+= s->me.mb_penalty_factor; //field_select bits + + dmin += field_select != block; //slightly prefer same field + + if(dmin < best_dmin){ + best_dmin= dmin; + best_field= field_select; + } + } + { + int16_t (*mv_table)[2]= mv_tables[block][best_field]; + + if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all + if(mv_table[xy][1]&1) same=0; + if(mv_table[xy][1]*2 != my) same=0; + if(best_field != block) same=0; + } + + field_select_tables[block][xy]= best_field; + dmin_sum += best_dmin; + } + + s->me.ymin<<=1; + s->me.ymax<<=1; + + if(same) + return INT_MAX; + + switch(s->avctx->mb_cmp&0xFF){ + /*case FF_CMP_SSE: + return dmin_sum+ 32*s->qscale*s->qscale;*/ + case FF_CMP_RD: + return dmin_sum; + default: + return dmin_sum+ 11*s->me.mb_penalty_factor; + } +} + void ff_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { uint8_t *pix, *ppix; - int sum, varc, vard, mx, my, range, dmin, xx, yy; - int xmin, ymin, xmax, ymax; - int rel_xmin, rel_ymin, rel_xmax, rel_ymax; + int sum, varc, vard, mx, my, dmin, xx, yy; int pred_x=0, pred_y=0; int P[10][2]; const int shift= 1+s->quarter_sample; @@ -987,18 +1057,26 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, uint8_t *ref_picture= s->last_picture.data[0]; Picture * const pic= &s->current_picture; uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; - + const int stride= s->linesize; + const int uvstride= s->uvlinesize; + uint8_t *src_data[3]= { + s->new_picture.data[0] + 16*(mb_x + stride*mb_y), + s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y), + s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y) + }; + uint8_t *ref_data[3]= { + s->last_picture.data[0] + 16*(mb_x + stride*mb_y), + s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y), + s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y) + }; + assert(s->quarter_sample==0 || s->quarter_sample==1); s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp); s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp); - get_limits(s, &range, &xmin, &ymin, &xmax, &ymax); - rel_xmin= xmin - mb_x*16; - rel_xmax= xmax - mb_x*16; - rel_ymin= ymin - mb_y*16; - rel_ymax= ymax - mb_y*16; + get_limits(s, 16*mb_x, 16*mb_y); s->me.skip=0; switch(s->me_method) { @@ -1009,21 +1087,23 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, my-= mb_y*16; dmin = 0; break; +#if 0 case ME_FULL: - dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); + dmin = full_motion_search(s, &mx, &my, range, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; case ME_LOG: - dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); + dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; case ME_PHODS: - dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); + dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; +#endif case ME_X1: case ME_EPZS: { @@ -1033,16 +1113,16 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; - if(P_LEFT[0] > (rel_xmax< (s->me.xmax<me.xmax<current_picture.motion_val[0][mot_xy - mot_stride ][0]; P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0]; P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1]; - if(P_TOP[1] > (rel_ymax< (rel_ymax< (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.ymax<me.ymax<me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); + dmin = s->me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, + src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); break; } @@ -1070,14 +1150,14 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, xx = mb_x * 16; yy = mb_y * 16; - pix = s->new_picture.data[0] + (yy * s->linesize) + xx; + pix = src_data[0]; /* At this point (mx,my) are full-pell and the relative displacement */ - ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx); + ppix = ref_data[0] + (my * s->linesize) + mx; sum = s->dsp.pix_sum(pix, s->linesize); varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; - vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8; + vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8; //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout); pic->mb_var [s->mb_stride * mb_y + mb_x] = varc; @@ -1099,47 +1179,59 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, s->scene_change_score+= s->qscale; if (vard*2 + 200 > varc) - mb_type|= MB_TYPE_INTRA; + mb_type|= CANDIDATE_MB_TYPE_INTRA; if (varc*2 + 200 > vard){ - mb_type|= MB_TYPE_INTER; - s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); + mb_type|= CANDIDATE_MB_TYPE_INTER; + s->me.sub_motion_search(s, &mx, &my, dmin, + pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); if(s->flags&CODEC_FLAG_MV0) if(mx || my) - mb_type |= MB_TYPE_SKIPED; //FIXME check difference + mb_type |= CANDIDATE_MB_TYPE_SKIPED; //FIXME check difference }else{ mx <<=shift; my <<=shift; } if((s->flags&CODEC_FLAG_4MV) && !s->me.skip && varc>50 && vard>10){ - if(h263_mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift) < INT_MAX) - mb_type|=MB_TYPE_INTER4V; + if(h263_mv4_search(s, mx, my, shift) < INT_MAX) + mb_type|=CANDIDATE_MB_TYPE_INTER4V; set_p_mv_tables(s, mx, my, 0); }else set_p_mv_tables(s, mx, my, 1); + if((s->flags&CODEC_FLAG_INTERLACED_ME) + && !s->me.skip){ //FIXME varc/d checks + if(interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my) < INT_MAX) + mb_type |= CANDIDATE_MB_TYPE_INTER_I; + } }else{ int intra_score, i; - mb_type= MB_TYPE_INTER; + mb_type= CANDIDATE_MB_TYPE_INTER; - dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty); - + dmin= s->me.sub_motion_search(s, &mx, &my, dmin, + pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, &s->last_picture, mv_penalty); + dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty); if((s->flags&CODEC_FLAG_4MV) && !s->me.skip && varc>50 && vard>10){ - int dmin4= h263_mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift); + int dmin4= h263_mv4_search(s, mx, my, shift); if(dmin4 < dmin){ - mb_type= MB_TYPE_INTER4V; + mb_type= CANDIDATE_MB_TYPE_INTER4V; dmin=dmin4; } } + if((s->flags&CODEC_FLAG_INTERLACED_ME) + && !s->me.skip){ //FIXME varc/d checks + int dmin_i= interlaced_search(s, src_data, ref_data, s->p_field_mv_table, s->p_field_select_table, s->f_code, mx, my); + if(dmin_i < dmin){ + mb_type = CANDIDATE_MB_TYPE_INTER_I; + dmin= dmin_i; + } + } // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; - set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V); + set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V); /* get intra luma score */ if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){ @@ -1155,7 +1247,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean; } - intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize); + intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, pix, s->linesize, 16); } #if 0 //FIXME /* get chroma score */ @@ -1184,8 +1276,8 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, intra_score += s->me.mb_penalty_factor*16; if(intra_score < dmin){ - mb_type= MB_TYPE_INTRA; - s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_INTRA; //FIXME cleanup + mb_type= CANDIDATE_MB_TYPE_INTRA; + s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup }else s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0; @@ -1202,30 +1294,36 @@ void ff_estimate_p_frame_motion(MpegEncContext * s, int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { - int mx, my, range, dmin; - int xmin, ymin, xmax, ymax; - int rel_xmin, rel_ymin, rel_xmax, rel_ymax; + int mx, my, dmin; int pred_x=0, pred_y=0; int P[10][2]; const int shift= 1+s->quarter_sample; uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; const int xy= mb_x + mb_y*s->mb_stride; + const int stride= s->linesize; + const int uvstride= s->uvlinesize; + uint8_t *src_data[3]= { + s->new_picture.data[0] + 16*(mb_x + stride*mb_y), + s->new_picture.data[1] + 8*(mb_x + uvstride*mb_y), + s->new_picture.data[2] + 8*(mb_x + uvstride*mb_y) + }; + uint8_t *ref_data[3]= { + s->last_picture.data[0] + 16*(mb_x + stride*mb_y), + s->last_picture.data[1] + 8*(mb_x + uvstride*mb_y), + s->last_picture.data[2] + 8*(mb_x + uvstride*mb_y) + }; assert(s->quarter_sample==0 || s->quarter_sample==1); s->me.pre_penalty_factor = get_penalty_factor(s, s->avctx->me_pre_cmp); - get_limits(s, &range, &xmin, &ymin, &xmax, &ymax); - rel_xmin= xmin - mb_x*16; - rel_xmax= xmax - mb_x*16; - rel_ymin= ymin - mb_y*16; - rel_ymax= ymax - mb_y*16; + get_limits(s, 16*mb_x, 16*mb_y); s->me.skip=0; P_LEFT[0] = s->p_mv_table[xy + 1][0]; P_LEFT[1] = s->p_mv_table[xy + 1][1]; - if(P_LEFT[0] < (rel_xmin<me.xmin<me.xmin<mb_height-1) { @@ -1238,9 +1336,9 @@ int ff_pre_estimate_p_frame_motion(MpegEncContext * s, P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1]; P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0]; P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1]; - if(P_TOP[1] < (rel_ymin< (rel_xmax<me.ymin<me.ymin< (s->me.xmax<me.xmax<me.ymin<me.ymin<me.pre_motion_search(s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty); + dmin = s->me.pre_motion_search(s, &mx, &my, P, pred_x, pred_y, + src_data, ref_data, stride, uvstride, s->p_mv_table, (1<<16)>>shift, mv_penalty); s->p_mv_table[xy][0] = mx<p_mv_table[xy][1] = my<quarter_sample; const int mot_stride = s->mb_stride; const int mot_xy = mb_y*mot_stride + mb_x; - uint8_t * const ref_picture= picture->data[0]; + uint8_t * const ref_picture= ref_data[0] - 16*s->mb_x - 16*s->mb_y*s->linesize; //FIXME ugly uint8_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV; int mv_scale; @@ -1276,11 +1373,7 @@ static int ff_estimate_motion_b(MpegEncContext * s, s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp); s->me.mb_penalty_factor = get_penalty_factor(s, s->avctx->mb_cmp); - get_limits(s, &range, &xmin, &ymin, &xmax, &ymax); - rel_xmin= xmin - mb_x*16; - rel_xmax= xmax - mb_x*16; - rel_ymin= ymin - mb_y*16; - rel_ymax= ymax - mb_y*16; + get_limits(s, 16*mb_x, 16*mb_y); switch(s->me_method) { case ME_ZERO: @@ -1290,28 +1383,30 @@ static int ff_estimate_motion_b(MpegEncContext * s, mx-= mb_x*16; my-= mb_y*16; break; +#if 0 case ME_FULL: - dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture); + dmin = full_motion_search(s, &mx, &my, range, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; case ME_LOG: - dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); + dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; case ME_PHODS: - dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture); + dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture); mx-= mb_x*16; my-= mb_y*16; break; +#endif case ME_X1: case ME_EPZS: { P_LEFT[0] = mv_table[mot_xy - 1][0]; P_LEFT[1] = mv_table[mot_xy - 1][1]; - if(P_LEFT[0] > (rel_xmax< (s->me.xmax<me.xmax< (rel_ymax< (rel_ymax< (s->me.ymax<me.ymax<me.xmin<me.xmin< (s->me.ymax<me.ymax<pb_time - s->pp_time)<<16) / (s->pp_time<me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - picture, s->p_mv_table, mv_scale, mv_penalty); + dmin = s->me.motion_search[0](s, &mx, &my, P, pred_x, pred_y, + src_data, ref_data, stride, uvstride, s->p_mv_table, mv_scale, mv_penalty); break; } - dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax, - pred_x, pred_y, picture, 0, 0, mv_penalty); + dmin= s->me.sub_motion_search(s, &mx, &my, dmin, + pred_x, pred_y, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, picture, mv_penalty); + dmin= s->me.get_mb_score(s, mx, my, pred_x, pred_y, src_data, ref_data, stride, uvstride, mv_penalty); //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; @@ -1356,16 +1451,18 @@ static int ff_estimate_motion_b(MpegEncContext * s, return dmin; } -static inline int check_bidir_mv(MpegEncContext * s, - int mb_x, int mb_y, +static inline int check_bidir_mv(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], + int stride, int uvstride, int motion_fx, int motion_fy, int motion_bx, int motion_by, int pred_fx, int pred_fy, - int pred_bx, int pred_by) + int pred_bx, int pred_by, + int size, int h) { //FIXME optimize? //FIXME move into template? //FIXME better f_code prediction (max mv & distance) + //FIXME pointers uint8_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame uint8_t *dest_y = s->me.scratchpad; uint8_t *ptr; @@ -1375,45 +1472,37 @@ static inline int check_bidir_mv(MpegEncContext * s, if(s->quarter_sample){ dxy = ((motion_fy & 3) << 2) | (motion_fx & 3); - src_x = mb_x * 16 + (motion_fx >> 2); - src_y = mb_y * 16 + (motion_fy >> 2); - assert(src_x >=-16 && src_x<=s->h_edge_pos); - assert(src_y >=-16 && src_y<=s->v_edge_pos); + src_x = motion_fx >> 2; + src_y = motion_fy >> 2; - ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; - s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize); + ptr = ref_data[0] + (src_y * stride) + src_x; + s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride); dxy = ((motion_by & 3) << 2) | (motion_bx & 3); - src_x = mb_x * 16 + (motion_bx >> 2); - src_y = mb_y * 16 + (motion_by >> 2); - assert(src_x >=-16 && src_x<=s->h_edge_pos); - assert(src_y >=-16 && src_y<=s->v_edge_pos); + src_x = motion_bx >> 2; + src_y = motion_by >> 2; - ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; - s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize); + ptr = ref_data[3] + (src_y * stride) + src_x; + s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride); }else{ dxy = ((motion_fy & 1) << 1) | (motion_fx & 1); - src_x = mb_x * 16 + (motion_fx >> 1); - src_y = mb_y * 16 + (motion_fy >> 1); - assert(src_x >=-16 && src_x<=s->h_edge_pos); - assert(src_y >=-16 && src_y<=s->v_edge_pos); + src_x = motion_fx >> 1; + src_y = motion_fy >> 1; - ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x; - s->dsp.put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16); + ptr = ref_data[0] + (src_y * stride) + src_x; + s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h); dxy = ((motion_by & 1) << 1) | (motion_bx & 1); - src_x = mb_x * 16 + (motion_bx >> 1); - src_y = mb_y * 16 + (motion_by >> 1); - assert(src_x >=-16 && src_x<=s->h_edge_pos); - assert(src_y >=-16 && src_y<=s->v_edge_pos); + src_x = motion_bx >> 1; + src_y = motion_by >> 1; - ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x; - s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16); + ptr = ref_data[3] + (src_y * stride) + src_x; + s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h); } fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.mb_penalty_factor +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.mb_penalty_factor - + s->dsp.mb_cmp[0](s, s->new_picture.data[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize); + + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic if(s->avctx->mb_cmp&FF_CMP_CHROMA){ } @@ -1423,7 +1512,8 @@ static inline int check_bidir_mv(MpegEncContext * s, } /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/ -static inline int bidir_refine(MpegEncContext * s, +static inline int bidir_refine(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], + int stride, int uvstride, int mb_x, int mb_y) { const int mot_stride = s->mb_stride; @@ -1440,16 +1530,18 @@ static inline int bidir_refine(MpegEncContext * s, //FIXME do refinement and add flag - fbmin= check_bidir_mv(s, mb_x, mb_y, + fbmin= check_bidir_mv(s, src_data, ref_data, stride, uvstride, motion_fx, motion_fy, motion_bx, motion_by, pred_fx, pred_fy, - pred_bx, pred_by); + pred_bx, pred_by, + 0, 16); return fbmin; } -static inline int direct_search(MpegEncContext * s, +static inline int direct_search(MpegEncContext * s, uint8_t *src_data[3], uint8_t *ref_data[6], + int stride, int uvstride, int mb_x, int mb_y) { int P[10][2]; @@ -1485,15 +1577,15 @@ static inline int direct_search(MpegEncContext * s, max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift; - max+= (2*mb_x + (i& 1))*8 + 1; // +-1 is for the simpler rounding - min+= (2*mb_x + (i& 1))*8 - 1; + max+= 16*mb_x + 1; // +-1 is for the simpler rounding + min+= 16*mb_x - 1; xmax= FFMIN(xmax, s->width - max); xmin= FFMAX(xmin, - 16 - min); max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift; - max+= (2*mb_y + (i>>1))*8 + 1; // +-1 is for the simpler rounding - min+= (2*mb_y + (i>>1))*8 - 1; + max+= 16*mb_y + 1; // +-1 is for the simpler rounding + min+= 16*mb_y - 1; ymax= FFMIN(ymax, s->height - max); ymin= FFMAX(ymin, - 16 - min); @@ -1508,6 +1600,11 @@ static inline int direct_search(MpegEncContext * s, return 256*256*256*64; } + + s->me.xmin= xmin; + s->me.ymin= ymin; + s->me.xmax= xmax; + s->me.ymax= ymax; P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<flags&CODEC_FLAG_QPEL){ - dmin = simple_direct_qpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, - &s->last_picture, mv_table, 1<<14, mv_penalty); - dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, - 0, 0, &s->last_picture, 0, 0, mv_penalty); + dmin = simple_direct_qpel_epzs_motion_search(s, &mx, &my, P, 0, 0, + src_data, ref_data, stride, uvstride, mv_table, 1<<14, mv_penalty); + dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, + 0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= simple_direct_qpel_qpel_get_mb_score(s, mx, my, 0, 0, &s->last_picture, mv_penalty); + dmin= simple_direct_qpel_qpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty); }else{ - dmin = simple_direct_hpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax, - &s->last_picture, mv_table, 1<<15, mv_penalty); - dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, - 0, 0, &s->last_picture, 0, 0, mv_penalty); + dmin = simple_direct_hpel_epzs_motion_search(s, &mx, &my, P, 0, 0, + src_data, ref_data, stride, uvstride, mv_table, 1<<15, mv_penalty); + dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, + 0, 0, src_data, ref_data, stride, uvstride, 0, 16, mv_penalty); if(s->avctx->me_sub_cmp != s->avctx->mb_cmp && !s->me.skip) - dmin= simple_direct_hpel_hpel_get_mb_score(s, mx, my, 0, 0, &s->last_picture, mv_penalty); + dmin= simple_direct_hpel_hpel_get_mb_score(s, mx, my, 0, 0, src_data, ref_data, stride, uvstride, mv_penalty); } + + get_limits(s, 16*mb_x, 16*mb_y); //restore s->me.?min/max, maybe not needed s->b_direct_mv_table[mot_xy][0]= mx; s->b_direct_mv_table[mot_xy][1]= my; @@ -1551,40 +1650,80 @@ void ff_estimate_b_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { const int penalty_factor= s->me.mb_penalty_factor; - int fmin, bmin, dmin, fbmin; + int fmin, bmin, dmin, fbmin, bimin, fimin; int type=0; + const int stride= s->linesize; + const int uvstride= s->uvlinesize; + uint8_t *src_data[3]= { + s->new_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), + s->new_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), + s->new_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y) + }; + uint8_t *ref_data[6]= { + s->last_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), + s->last_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), + s->last_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y), + s->next_picture.data[0] + 16*(s->mb_x + stride*s->mb_y), + s->next_picture.data[1] + 8*(s->mb_x + uvstride*s->mb_y), + s->next_picture.data[2] + 8*(s->mb_x + uvstride*s->mb_y) + }; s->me.skip=0; if (s->codec_id == CODEC_ID_MPEG4) - dmin= direct_search(s, mb_x, mb_y); + dmin= direct_search(s, src_data, ref_data, stride, uvstride, mb_x, mb_y); else dmin= INT_MAX; - +//FIXME penalty stuff for non mpeg4 s->me.skip=0; - fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, &s->last_picture, s->f_code) + 3*penalty_factor; + fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, src_data, + ref_data, stride, uvstride, s->f_code) + 3*penalty_factor; s->me.skip=0; - bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, &s->next_picture, s->b_code) + 2*penalty_factor; + bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, src_data, + ref_data+3, stride, uvstride, s->b_code) + 2*penalty_factor; //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); s->me.skip=0; - fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor; + fbmin= bidir_refine(s, src_data, ref_data, stride, uvstride, mb_x, mb_y) + penalty_factor; //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin); + + if(s->flags & CODEC_FLAG_INTERLACED_ME){ + const int xy = mb_y*s->mb_stride + mb_x; + +//FIXME mb type penalty + s->me.skip=0; + fimin= interlaced_search(s, src_data, ref_data , + s->b_field_mv_table[0], s->b_field_select_table[0], s->f_code, + s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); + bimin= interlaced_search(s, src_data, ref_data+3, + s->b_field_mv_table[1], s->b_field_select_table[1], s->b_code, + s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1]); + }else + fimin= bimin= INT_MAX; + { int score= fmin; - type = MB_TYPE_FORWARD; + type = CANDIDATE_MB_TYPE_FORWARD; if (dmin <= score){ score = dmin; - type = MB_TYPE_DIRECT; + type = CANDIDATE_MB_TYPE_DIRECT; } if(bmin>16; @@ -1593,8 +1732,20 @@ void ff_estimate_b_frame_motion(MpegEncContext * s, } if(s->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ - type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter - if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB + type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT; + if(fimin < INT_MAX) + type |= CANDIDATE_MB_TYPE_FORWARD_I; + if(bimin < INT_MAX) + type |= CANDIDATE_MB_TYPE_BACKWARD_I; + if(fimin < INT_MAX && bimin < INT_MAX){ + type |= CANDIDATE_MB_TYPE_BIDIR_I; + } + //FIXME something smarter + if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB +#if 0 + if(s->out_format == FMT_MPEG1) + type |= CANDIDATE_MB_TYPE_INTRA; +#endif } s->mb_type[mb_y*s->mb_stride + mb_x]= type; @@ -1661,24 +1812,6 @@ void ff_fix_long_p_mvs(MpegEncContext * s) if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range; - /* clip / convert to intra 16x16 type MVs */ - for(y=0; ymb_height; y++){ - int x; - int xy= y*s->mb_stride; - for(x=0; xmb_width; x++){ - if(s->mb_type[xy]&MB_TYPE_INTER){ - if( s->p_mv_table[xy][0] >=range || s->p_mv_table[xy][0] <-range - || s->p_mv_table[xy][1] >=range || s->p_mv_table[xy][1] <-range){ - s->mb_type[xy] &= ~MB_TYPE_INTER; - s->mb_type[xy] |= MB_TYPE_INTRA; - s->current_picture.mb_type[xy]= MB_TYPE_INTRA; - s->p_mv_table[xy][0] = 0; - s->p_mv_table[xy][1] = 0; - } - } - xy++; - } - } //printf("%d no:%d %d//\n", clip, noclip, f_code); if(s->flags&CODEC_FLAG_4MV){ const int wrap= 2+ s->mb_width*2; @@ -1690,7 +1823,7 @@ void ff_fix_long_p_mvs(MpegEncContext * s) int x; for(x=0; xmb_width; x++){ - if(s->mb_type[i]&MB_TYPE_INTER4V){ + if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){ int block; for(block=0; block<4; block++){ int off= (block& 1) + (block>>1)*wrap; @@ -1699,9 +1832,9 @@ void ff_fix_long_p_mvs(MpegEncContext * s) if( mx >=range || mx <-range || my >=range || my <-range){ - s->mb_type[i] &= ~MB_TYPE_INTER4V; - s->mb_type[i] |= MB_TYPE_INTRA; - s->current_picture.mb_type[i]= MB_TYPE_INTRA; + s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V; + s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA; + s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA; } } } @@ -1712,30 +1845,45 @@ void ff_fix_long_p_mvs(MpegEncContext * s) } } -void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type) +/** + * + * @param truncate 1 for truncation, 0 for using intra + */ +void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, + int16_t (*mv_table)[2], int f_code, int type, int truncate) { - int y; + int y, h_range, v_range; // RAL: 8 in MPEG-1, 16 in MPEG-4 int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code); - + + if(s->msmpeg4_version) range= 16; if(s->avctx->me_range && range > s->avctx->me_range) range= s->avctx->me_range; + h_range= range; + v_range= field_select_table ? range>>1 : range; + /* clip / convert to intra 16x16 type MVs */ for(y=0; ymb_height; y++){ int x; int xy= y*s->mb_stride; for(x=0; xmb_width; x++){ if (s->mb_type[xy] & type){ // RAL: "type" test added... - if( mv_table[xy][0] >=range || mv_table[xy][0] <-range - || mv_table[xy][1] >=range || mv_table[xy][1] <-range){ + if(field_select_table==NULL || field_select_table[xy] == field_select){ + if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range + || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){ - if(s->codec_id == CODEC_ID_MPEG1VIDEO && 0){ - }else{ - if (mv_table[xy][0] > range-1) mv_table[xy][0]= range-1; - else if(mv_table[xy][0] < -range ) mv_table[xy][0]= -range; - if (mv_table[xy][1] > range-1) mv_table[xy][1]= range-1; - else if(mv_table[xy][1] < -range ) mv_table[xy][1]= -range; + if(truncate){ + if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1; + else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range; + if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1; + else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range; + }else{ + s->mb_type[xy] &= ~type; + s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA; + mv_table[xy][0]= + mv_table[xy][1]= 0; + } } } } diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c index db51d676db..18203ec065 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/motion_est_template.c @@ -1,6 +1,6 @@ /* * Motion estimation - * Copyright (c) 2002 Michael Niedermayer + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -22,29 +22,31 @@ * @file motion_est_template.c * Motion estimation template. */ - +//FIXME ref2_y next_pic? //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...) //Note, the last line is there to kill these ugly unused var warnings -#define LOAD_COMMON(x, y)\ +#define LOAD_COMMON\ uint32_t * const score_map= s->me.score_map;\ - const int stride= s->linesize;\ - const int uvstride= s->uvlinesize;\ const int time_pp= s->pp_time;\ const int time_pb= s->pb_time;\ - uint8_t * const src_y= s->new_picture.data[0] + ((y) * stride) + (x);\ - uint8_t * const src_u= s->new_picture.data[1] + (((y)>>1) * uvstride) + ((x)>>1);\ - uint8_t * const src_v= s->new_picture.data[2] + (((y)>>1) * uvstride) + ((x)>>1);\ - uint8_t * const ref_y= ref_picture->data[0] + ((y) * stride) + (x);\ - uint8_t * const ref_u= ref_picture->data[1] + (((y)>>1) * uvstride) + ((x)>>1);\ - uint8_t * const ref_v= ref_picture->data[2] + (((y)>>1) * uvstride) + ((x)>>1);\ - uint8_t * const ref2_y= s->next_picture.data[0] + ((y) * stride) + (x);\ + const int xmin= s->me.xmin;\ + const int ymin= s->me.ymin;\ + const int xmax= s->me.xmax;\ + const int ymax= s->me.ymax;\ + uint8_t * const src_y= src_data[0];\ + uint8_t * const src_u= src_data[1];\ + uint8_t * const src_v= src_data[2];\ + uint8_t * const ref_y= ref_data[0];\ + uint8_t * const ref_u= ref_data[1];\ + uint8_t * const ref_v= ref_data[2];\ op_pixels_func (*hpel_put)[4];\ op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\ op_pixels_func (*chroma_hpel_put)[4];\ qpel_mc_func (*qpel_put)[16];\ qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\ const __attribute__((unused)) int unu= time_pp + time_pb + (size_t)src_u + (size_t)src_v + (size_t)ref_u + (size_t)ref_v\ - + (size_t)ref2_y + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map;\ + + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map\ + + xmin + xmax + ymin + ymax;\ if(s->no_rounding /*FIXME b_type*/){\ hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\ chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\ @@ -70,9 +72,8 @@ #if 0 static int RENAME(hpel_motion_search)(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, - int xmin, int ymin, int xmax, int ymax, - int pred_x, int pred_y, Picture *ref_picture, - int n, int size, uint8_t * const mv_penalty) + int pred_x, int pred_y, uint8_t *ref_data[3], + int size, uint8_t * const mv_penalty) { const int xx = 16 * s->mb_x + 8*(n&1); const int yy = 16 * s->mb_y + 8*(n>>1); @@ -80,7 +81,7 @@ static int RENAME(hpel_motion_search)(MpegEncContext * s, const int my = *my_ptr; const int penalty_factor= s->me.sub_penalty_factor; - LOAD_COMMON(xx, yy); + LOAD_COMMON // INIT; //FIXME factorize @@ -139,19 +140,17 @@ static int RENAME(hpel_motion_search)(MpegEncContext * s, #else static int RENAME(hpel_motion_search)(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, - int xmin, int ymin, int xmax, int ymax, - int pred_x, int pred_y, Picture *ref_picture, - int n, int size, uint8_t * const mv_penalty) + int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, + int size, int h, uint8_t * const mv_penalty) { - const int xx = 16 * s->mb_x + 8*(n&1); - const int yy = 16 * s->mb_y + 8*(n>>1); const int mx = *mx_ptr; const int my = *my_ptr; const int penalty_factor= s->me.sub_penalty_factor; me_cmp_func cmp_sub, chroma_cmp_sub; int bx=2*mx, by=2*my; - LOAD_COMMON(xx, yy); + LOAD_COMMON //FIXME factorize @@ -247,20 +246,18 @@ static int RENAME(hpel_motion_search)(MpegEncContext * s, } #endif -static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, +static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, uint8_t * const mv_penalty) { // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp; const int size= 0; - const int xx = 16 * s->mb_x; - const int yy = 16 * s->mb_y; + const int h= 16; const int penalty_factor= s->me.mb_penalty_factor; - const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these - const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit me_cmp_func cmp_sub, chroma_cmp_sub; int d; - LOAD_COMMON(xx, yy); + LOAD_COMMON //FIXME factorize @@ -295,12 +292,10 @@ static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pre static int RENAME(qpel_motion_search)(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, - int xmin, int ymin, int xmax, int ymax, - int pred_x, int pred_y, Picture *ref_picture, - int n, int size, uint8_t * const mv_penalty) + int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, + int size, int h, uint8_t * const mv_penalty) { - const int xx = 16 * s->mb_x + 8*(n&1); - const int yy = 16 * s->mb_y + 8*(n>>1); const int mx = *mx_ptr; const int my = *my_ptr; const int penalty_factor= s->me.sub_penalty_factor; @@ -310,7 +305,7 @@ static int RENAME(qpel_motion_search)(MpegEncContext * s, me_cmp_func cmp, chroma_cmp; me_cmp_func cmp_sub, chroma_cmp_sub; - LOAD_COMMON(xx, yy); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME @@ -514,19 +509,17 @@ static int RENAME(qpel_motion_search)(MpegEncContext * s, return dmin; } -static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, Picture *ref_picture, +static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, uint8_t * const mv_penalty) { const int size= 0; - const int xx = 16 * s->mb_x; - const int yy = 16 * s->mb_y; + const int h= 16; const int penalty_factor= s->me.mb_penalty_factor; - const int xmin= -256*256, ymin= -256*256, xmax= 256*256, ymax= 256*256; //assume that the caller checked these - const __attribute__((unused)) int unu2= xmin + xmax +ymin + ymax; //no unused warning shit me_cmp_func cmp_sub, chroma_cmp_sub; int d; - LOAD_COMMON(xx, yy); + LOAD_COMMON //FIXME factorize @@ -597,15 +590,16 @@ if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin, - Picture *ref_picture, + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int const pred_x, int const pred_y, int const penalty_factor, - int const xmin, int const ymin, int const xmax, int const ymax, int const shift, - uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty + int const shift, + uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty ) { me_cmp_func cmp, chroma_cmp; int next_dir=-1; - LOAD_COMMON(s->mb_x*16, s->mb_y*16); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -639,15 +633,16 @@ static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, in } static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin, - Picture *ref_picture, + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int const pred_x, int const pred_y, int const penalty_factor, - int const xmin, int const ymin, int const xmax, int const ymax, int const shift, - uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty + int const shift, + uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty ) { me_cmp_func cmp, chroma_cmp; int dia_size; - LOAD_COMMON(s->mb_x*16, s->mb_y*16); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -730,17 +725,18 @@ if(256*256*256*64 % (stats[0]+1)==0){ #define MAX_SAB_SIZE 16 static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin, - Picture *ref_picture, + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int const pred_x, int const pred_y, int const penalty_factor, - int const xmin, int const ymin, int const xmax, int const ymax, int const shift, - uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty + int const shift, + uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty ) { me_cmp_func cmp, chroma_cmp; Minima minima[MAX_SAB_SIZE]; const int minima_count= ABS(s->me.dia_size); int i, j; - LOAD_COMMON(s->mb_x*16, s->mb_y*16); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -810,15 +806,16 @@ static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int } static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin, - Picture *ref_picture, + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int const pred_x, int const pred_y, int const penalty_factor, - int const xmin, int const ymin, int const xmax, int const ymax, int const shift, - uint32_t *map, int map_generation, int size, uint8_t * const mv_penalty + int const shift, + uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty ) { me_cmp_func cmp, chroma_cmp; int dia_size; - LOAD_COMMON(s->mb_x*16, s->mb_y*16); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -886,10 +883,10 @@ if(256*256*256*64 % (stats[0]+1)==0){ return dmin; } -static int RENAME(epzs_motion_search)(MpegEncContext * s, int block, +static int RENAME(epzs_motion_search)(MpegEncContext * s, int *mx_ptr, int *my_ptr, - int P[10][2], int pred_x, int pred_y, - int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], + int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], int ref_mv_scale, uint8_t * const mv_penalty) { int best[2]={0, 0}; @@ -899,10 +896,11 @@ static int RENAME(epzs_motion_search)(MpegEncContext * s, int block, int map_generation; const int penalty_factor= s->me.penalty_factor; const int size=0; - const int ref_mv_stride= s->mb_stride; - const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; + const int h=16; + const int ref_mv_stride= s->mb_stride; //pass as arg FIXME + const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME me_cmp_func cmp, chroma_cmp; - LOAD_COMMON(s->mb_x*16, s->mb_y*16); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -973,21 +971,21 @@ static int RENAME(epzs_motion_search)(MpegEncContext * s, int block, //check(best[0],best[1],0, b0) if(s->me.dia_size==-1) - dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else if(s->me.dia_size<-1) - dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else if(s->me.dia_size<2) - dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else - dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); //check(best[0],best[1],0, b1) *mx_ptr= best[0]; @@ -998,10 +996,11 @@ static int RENAME(epzs_motion_search)(MpegEncContext * s, int block, } #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */ -static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block, +static int RENAME(epzs_motion_search4)(MpegEncContext * s, int *mx_ptr, int *my_ptr, int P[10][2], int pred_x, int pred_y, - int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2], + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], int ref_mv_scale, uint8_t * const mv_penalty) { int best[2]={0, 0}; @@ -1011,10 +1010,11 @@ static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block, int map_generation; const int penalty_factor= s->me.penalty_factor; const int size=1; + const int h=8; const int ref_mv_stride= s->mb_stride; const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; me_cmp_func cmp, chroma_cmp; - LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8); + LOAD_COMMON cmp= s->dsp.me_cmp[size]; chroma_cmp= s->dsp.me_cmp[size+1]; @@ -1024,7 +1024,7 @@ static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block, dmin = 1000000; //printf("%d %d %d %d //",xmin, ymin, xmax, ymax); /* first line */ - if (s->mb_y == 0 && block<2) { + if (s->mb_y == 0/* && block<2*/) { CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) @@ -1049,21 +1049,100 @@ static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block, } if(s->me.dia_size==-1) - dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else if(s->me.dia_size<-1) - dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else if(s->me.dia_size<2) - dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); else - dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture, - pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax, - shift, map, map_generation, size, mv_penalty); + dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); + + + *mx_ptr= best[0]; + *my_ptr= best[1]; + +// printf("%d %d %d \n", best[0], best[1], dmin); + return dmin; +} + +//try to merge with above FIXME (needs PSNR test) +static int RENAME(epzs_motion_search2)(MpegEncContext * s, + int *mx_ptr, int *my_ptr, + int P[10][2], int pred_x, int pred_y, + uint8_t *src_data[3], + uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2], + int ref_mv_scale, uint8_t * const mv_penalty) +{ + int best[2]={0, 0}; + int d, dmin; + const int shift= 1+s->quarter_sample; + uint32_t *map= s->me.map; + int map_generation; + const int penalty_factor= s->me.penalty_factor; + const int size=0; //FIXME pass as arg + const int h=8; + const int ref_mv_stride= s->mb_stride; + const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride; + me_cmp_func cmp, chroma_cmp; + LOAD_COMMON + + cmp= s->dsp.me_cmp[size]; + chroma_cmp= s->dsp.me_cmp[size+1]; + + map_generation= update_map_generation(s); + + dmin = 1000000; +//printf("%d %d %d %d //",xmin, ymin, xmax, ymax); + /* first line */ + if (s->mb_y == 0) { + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) + CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) + }else{ + CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift) + //FIXME try some early stop + if(dmin>64*2){ + CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift) + CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift) + CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift) + CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift) + CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16) + } + } + if(dmin>64*4){ + CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16) + CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16, + (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16) + } + + if(s->me.dia_size==-1) + dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); + else if(s->me.dia_size<-1) + dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); + else if(s->me.dia_size<2) + dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); + else + dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride, + pred_x, pred_y, penalty_factor, + shift, map, map_generation, size, h, mv_penalty); + *mx_ptr= best[0]; *my_ptr= best[1]; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12.c b/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12.c index 2d50ff9b73..3edad5b802 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12.c @@ -1,6 +1,7 @@ /* * MPEG1 codec / MPEG2 decoder * Copyright (c) 2000,2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -29,6 +30,9 @@ #include "mpeg12data.h" +//#undef NDEBUG +//#include + /* Start codes. */ #define SEQ_END_CODE 0x000001b7 @@ -54,7 +58,6 @@ static void mpeg1_encode_block(MpegEncContext *s, int component); static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added #endif //CONFIG_ENCODERS -static void mpeg1_skip_picture(MpegEncContext *s, int pict_num); static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n); @@ -178,6 +181,46 @@ static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni } } + +static int find_frame_rate_index(MpegEncContext *s){ + int i; + int64_t dmin= INT64_MAX; + int64_t d; + + for(i=1;i<14;i++) { + if(s->avctx->strict_std_compliance >= 0 && i>=9) break; + + d = ABS(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate - frame_rate_tab[i]*(int64_t)s->avctx->frame_rate_base); + if(d < dmin){ + dmin=d; + s->frame_rate_index= i; + } + } + if(dmin) + return -1; + else + return 0; +} + +static int encode_init(AVCodecContext *avctx) +{ + MpegEncContext *s = avctx->priv_data; + + if(MPV_encode_init(avctx) < 0) + return -1; + + if(find_frame_rate_index(s) < 0){ + if(s->strict_std_compliance >=0){ + av_log(avctx, AV_LOG_ERROR, "MPEG1/2 doesnt support %d/%d fps\n", avctx->frame_rate, avctx->frame_rate_base); + return -1; + }else{ + av_log(avctx, AV_LOG_INFO, "MPEG1/2 doesnt support %d/%d fps, there may be AV sync issues\n", avctx->frame_rate, avctx->frame_rate_base); + } + } + + return 0; +} + static void put_header(MpegEncContext *s, int header) { align_put_bits(&s->pb); @@ -201,22 +244,6 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) if (s->current_picture.key_frame) { /* mpeg1 header repeated every gop */ put_header(s, SEQ_START_CODE); - - /* search closest frame rate */ - { - int i, dmin, d; - s->frame_rate_index = 0; - dmin = 0x7fffffff; - for(i=1;i<14;i++) { - if(s->avctx->strict_std_compliance >= 0 && i>=9) break; - - d = abs(MPEG1_FRAME_RATE_BASE*(int64_t)s->avctx->frame_rate/s->avctx->frame_rate_base - frame_rate_tab[i]); - if (d < dmin) { - dmin = d; - s->frame_rate_index = i; - } - } - } put_bits(&s->pb, 12, s->width); put_bits(&s->pb, 12, s->height); @@ -294,30 +321,18 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) put_bits(&s->pb, 1, 0); /* do drop frame */ /* time code : we must convert from the real frame rate to a fake mpeg frame rate in case of low frame rate */ - fps = frame_rate_tab[s->frame_rate_index]; - time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE; - s->gop_picture_number = s->fake_picture_number; + fps = (frame_rate_tab[s->frame_rate_index] + MPEG1_FRAME_RATE_BASE/2)/ MPEG1_FRAME_RATE_BASE; + time_code = s->current_picture_ptr->coded_picture_number; + + s->gop_picture_number = time_code; put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24)); put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60)); put_bits(&s->pb, 1, 1); put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60)); - put_bits(&s->pb, 6, (uint32_t)((time_code % fps) / MPEG1_FRAME_RATE_BASE)); - put_bits(&s->pb, 1, 0); /* closed gop */ + put_bits(&s->pb, 6, (uint32_t)((time_code % fps))); + put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP)); put_bits(&s->pb, 1, 0); /* broken link */ } - - if (s->avctx->frame_rate < (24 * s->avctx->frame_rate_base) && s->picture_number > 0) { - /* insert empty P pictures to slow down to the desired - frame rate. Each fake pictures takes about 20 bytes */ - fps = frame_rate_tab[s->frame_rate_index]; - n = av_rescale((int64_t)s->picture_number * s->avctx->frame_rate_base, fps, s->avctx->frame_rate) / MPEG1_FRAME_RATE_BASE - 1; - while (s->fake_picture_number < n) { - mpeg1_skip_picture(s, s->fake_picture_number - - s->gop_picture_number); - s->fake_picture_number++; - } - - } } static inline void encode_mb_skip_run(MpegEncContext *s, int run){ @@ -328,49 +343,6 @@ static inline void encode_mb_skip_run(MpegEncContext *s, int run){ put_bits(&s->pb, mbAddrIncrTable[run][1], mbAddrIncrTable[run][0]); } - -/* insert a fake P picture */ -static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) -{ - assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things - - /* mpeg1 picture header */ - put_header(s, PICTURE_START_CODE); - /* temporal reference */ - put_bits(&s->pb, 10, pict_num & 0x3ff); - - put_bits(&s->pb, 3, P_TYPE); - put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ - - put_bits(&s->pb, 1, 1); /* integer coordinates */ - put_bits(&s->pb, 3, 1); /* forward_f_code */ - - put_bits(&s->pb, 1, 0); /* extra bit picture */ - - /* only one slice */ - put_header(s, SLICE_MIN_START_CODE); - put_bits(&s->pb, 5, 1); /* quantizer scale */ - put_bits(&s->pb, 1, 0); /* slice extra information */ - - encode_mb_skip_run(s, 0); - - /* empty macroblock */ - put_bits(&s->pb, 3, 1); /* motion only */ - - /* zero motion x & y */ - put_bits(&s->pb, 1, 1); - put_bits(&s->pb, 1, 1); - - /* output a number of empty slice */ - encode_mb_skip_run(s, s->mb_width * s->mb_height - 2); - - /* empty macroblock */ - put_bits(&s->pb, 3, 1); /* motion only */ - - /* zero motion x & y */ - put_bits(&s->pb, 1, 1); - put_bits(&s->pb, 1, 1); -} #endif //CONFIG_ENCODERS static void common_init(MpegEncContext *s) @@ -405,10 +377,10 @@ void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) // RAL: s->picture_number instead of s->fake_picture_number put_bits(&s->pb, 10, (s->picture_number - s->gop_picture_number) & 0x3ff); - s->fake_picture_number++; - put_bits(&s->pb, 3, s->pict_type); - put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */ + + s->vbv_delay_ptr= s->pb.buf + get_bit_count(&s->pb)/8; + put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */ // RAL: Forward f_code also needed for B frames if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { @@ -468,18 +440,26 @@ void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number) put_bits(&s->pb, 1, s->progressive_frame); put_bits(&s->pb, 1, 0); //composite_display_flag } + if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){ + int i; + + put_header(s, USER_START_CODE); + for(i=0; ipb, 8, svcd_scan_offset_placeholder[i]); + } + } s->mb_y=0; ff_mpeg1_encode_slice_header(s); } static inline void put_mb_modes(MpegEncContext *s, int n, int bits, - int has_mv) + int has_mv, int field_motion) { put_bits(&s->pb, n, bits); if (!s->frame_pred_frame_dct) { if (has_mv) - put_bits(&s->pb, 2, 2); /* motion_type: frame */ + put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */ put_bits(&s->pb, 1, s->interlaced_dct); } } @@ -499,9 +479,9 @@ void mpeg1_encode_mb(MpegEncContext *s, if (s->block_last_index[i] >= 0) cbp |= 1 << (5 - i); } - + if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && - ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) || + ((s->pict_type == P_TYPE && s->mv_type == MV_TYPE_16X16 && (motion_x | motion_y) == 0) || (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { s->mb_skip_run++; @@ -509,6 +489,10 @@ void mpeg1_encode_mb(MpegEncContext *s, s->skip_count++; s->misc_bits++; s->last_bits++; + if(s->pict_type == P_TYPE){ + s->last_mv[0][1][0]= s->last_mv[0][0][0]= + s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0; + } } else { if(first_mb){ assert(s->mb_skip_run == 0); @@ -519,150 +503,167 @@ void mpeg1_encode_mb(MpegEncContext *s, if (s->pict_type == I_TYPE) { if(s->dquant && cbp){ - put_mb_modes(s, 2, 1, 0); /* macroblock_type : macroblock_quant = 1 */ + put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */ put_bits(&s->pb, 5, s->qscale); }else{ - put_mb_modes(s, 1, 1, 0); /* macroblock_type : macroblock_quant = 0 */ + put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */ s->qscale -= s->dquant; } s->misc_bits+= get_bits_diff(s); s->i_count++; } else if (s->mb_intra) { if(s->dquant && cbp){ - put_mb_modes(s, 6, 0x01, 0); + put_mb_modes(s, 6, 0x01, 0, 0); put_bits(&s->pb, 5, s->qscale); }else{ - put_mb_modes(s, 5, 0x03, 0); + put_mb_modes(s, 5, 0x03, 0, 0); s->qscale -= s->dquant; } s->misc_bits+= get_bits_diff(s); s->i_count++; - s->last_mv[0][0][0] = - s->last_mv[0][0][1] = 0; + memset(s->last_mv, 0, sizeof(s->last_mv)); } else if (s->pict_type == P_TYPE) { + if(s->mv_type == MV_TYPE_16X16){ if (cbp != 0) { - if (motion_x == 0 && motion_y == 0) { + if ((motion_x|motion_y) == 0) { if(s->dquant){ - put_mb_modes(s, 5, 1, 0); /* macroblock_pattern & quant */ + put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */ put_bits(&s->pb, 5, s->qscale); }else{ - put_mb_modes(s, 2, 1, 0); /* macroblock_pattern only */ + put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */ } s->misc_bits+= get_bits_diff(s); - put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); } else { if(s->dquant){ - put_mb_modes(s, 5, 2, 1); /* motion + cbp */ + put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */ put_bits(&s->pb, 5, s->qscale); }else{ - put_mb_modes(s, 1, 1, 1); /* motion + cbp */ + put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */ } s->misc_bits+= get_bits_diff(s); mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added s->mv_bits+= get_bits_diff(s); - put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); } } else { put_bits(&s->pb, 3, 1); /* motion only */ if (!s->frame_pred_frame_dct) put_bits(&s->pb, 2, 2); /* motion_type: frame */ + s->misc_bits+= get_bits_diff(s); mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added s->qscale -= s->dquant; s->mv_bits+= get_bits_diff(s); } - s->f_count++; - } else - { // RAL: All the following bloc added for B frames: - if (cbp != 0) - { // With coded bloc pattern - if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) - { // Bi-directional motion - if (s->dquant) { - put_mb_modes(s, 5, 2, 1); - put_bits(&s->pb, 5, s->qscale); - } else { - put_mb_modes(s, 2, 3, 1); - } - s->misc_bits += get_bits_diff(s); - mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); - mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); - mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); - mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); - s->b_count++; - s->f_count++; - s->mv_bits += get_bits_diff(s); - put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); - } - else if (s->mv_dir == MV_DIR_BACKWARD) - { // Backward motion - if (s->dquant) { - put_mb_modes(s, 6, 2, 1); - put_bits(&s->pb, 5, s->qscale); - } else { - put_mb_modes(s, 3, 3, 1); - } - s->misc_bits += get_bits_diff(s); - mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); - mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); - s->b_count++; - s->mv_bits += get_bits_diff(s); - put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); - } - else if (s->mv_dir == MV_DIR_FORWARD) - { // Forward motion - if (s->dquant) { - put_mb_modes(s, 6, 3, 1); - put_bits(&s->pb, 5, s->qscale); - } else { - put_mb_modes(s, 4, 3, 1); - } - s->misc_bits += get_bits_diff(s); - mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); - mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); - s->f_count++; - s->mv_bits += get_bits_diff(s); - put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); - } + s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x; + s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y; + }else{ + assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD); + + if (cbp) { + if(s->dquant){ + put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */ + put_bits(&s->pb, 5, s->qscale); + }else{ + put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */ } - else - { // No coded bloc pattern - if (s->mv_dir == (MV_DIR_FORWARD | MV_DIR_BACKWARD)) - { // Bi-directional motion - put_bits(&s->pb, 2, 2); /* backward & forward motion */ - if (!s->frame_pred_frame_dct) - put_bits(&s->pb, 2, 2); /* motion_type: frame */ - mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); - mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); - mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); - mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); - s->b_count++; - s->f_count++; - } - else if (s->mv_dir == MV_DIR_BACKWARD) - { // Backward motion - put_bits(&s->pb, 3, 2); /* backward motion only */ - if (!s->frame_pred_frame_dct) - put_bits(&s->pb, 2, 2); /* motion_type: frame */ - mpeg1_encode_motion(s, motion_x - s->last_mv[1][0][0], s->b_code); - mpeg1_encode_motion(s, motion_y - s->last_mv[1][0][1], s->b_code); - s->b_count++; - } - else if (s->mv_dir == MV_DIR_FORWARD) - { // Forward motion - put_bits(&s->pb, 4, 2); /* forward motion only */ - if (!s->frame_pred_frame_dct) - put_bits(&s->pb, 2, 2); /* motion_type: frame */ - mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); - mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); - s->f_count++; - } + } else { + put_bits(&s->pb, 3, 1); /* motion only */ + put_bits(&s->pb, 2, 1); /* motion_type: field */ s->qscale -= s->dquant; - s->mv_bits += get_bits_diff(s); - } - // End of bloc from RAL + } + s->misc_bits+= get_bits_diff(s); + for(i=0; i<2; i++){ + put_bits(&s->pb, 1, s->field_select[0][i]); + mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); + mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code); + s->last_mv[0][i][0]= s->mv[0][i][0]; + s->last_mv[0][i][1]= 2*s->mv[0][i][1]; + } + s->mv_bits+= get_bits_diff(s); } + if(cbp) + put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); + s->f_count++; + } else{ + static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi + + if(s->mv_type == MV_TYPE_16X16){ + if (cbp){ // With coded bloc pattern + if (s->dquant) { + if(s->mv_dir == MV_DIR_FORWARD) + put_mb_modes(s, 6, 3, 1, 0); + else + put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0); + put_bits(&s->pb, 5, s->qscale); + } else { + put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0); + } + }else{ // No coded bloc pattern + put_bits(&s->pb, mb_type_len[s->mv_dir], 2); + if (!s->frame_pred_frame_dct) + put_bits(&s->pb, 2, 2); /* motion_type: frame */ + s->qscale -= s->dquant; + } + s->misc_bits += get_bits_diff(s); + if (s->mv_dir&MV_DIR_FORWARD){ + mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code); + mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code); + s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0]; + s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1]; + s->f_count++; + } + if (s->mv_dir&MV_DIR_BACKWARD){ + mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code); + mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code); + s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0]; + s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1]; + s->b_count++; + } + }else{ + assert(s->mv_type == MV_TYPE_FIELD); + assert(!s->frame_pred_frame_dct); + if (cbp){ // With coded bloc pattern + if (s->dquant) { + if(s->mv_dir == MV_DIR_FORWARD) + put_mb_modes(s, 6, 3, 1, 1); + else + put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1); + put_bits(&s->pb, 5, s->qscale); + } else { + put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1); + } + }else{ // No coded bloc pattern + put_bits(&s->pb, mb_type_len[s->mv_dir], 2); + put_bits(&s->pb, 2, 1); /* motion_type: field */ + s->qscale -= s->dquant; + } + s->misc_bits += get_bits_diff(s); + if (s->mv_dir&MV_DIR_FORWARD){ + for(i=0; i<2; i++){ + put_bits(&s->pb, 1, s->field_select[0][i]); + mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code); + mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code); + s->last_mv[0][i][0]= s->mv[0][i][0]; + s->last_mv[0][i][1]= 2*s->mv[0][i][1]; + } + s->f_count++; + } + if (s->mv_dir&MV_DIR_BACKWARD){ + for(i=0; i<2; i++){ + put_bits(&s->pb, 1, s->field_select[1][i]); + mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code); + mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code); + s->last_mv[1][i][0]= s->mv[1][i][0]; + s->last_mv[1][i][1]= 2*s->mv[1][i][1]; + } + s->b_count++; + } + } + s->mv_bits += get_bits_diff(s); + if(cbp) + put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); + } for(i=0;i<6;i++) { if (cbp & (1 << (5 - i))) { mpeg1_encode_block(s, block[i], i); @@ -674,18 +675,6 @@ void mpeg1_encode_mb(MpegEncContext *s, else s->p_tex_bits+= get_bits_diff(s); } - - // RAL: By this: - if (s->mv_dir & MV_DIR_FORWARD) - { - s->last_mv[0][0][0]= s->mv[0][0][0]; - s->last_mv[0][0][1]= s->mv[0][0][1]; - } - if (s->mv_dir & MV_DIR_BACKWARD) - { - s->last_mv[1][0][0]= s->mv[1][0][0]; - s->last_mv[1][0][1]= s->mv[1][0][1]; - } } // RAL: Parameter added: f_or_b_code @@ -994,14 +983,11 @@ static inline int get_dmv(MpegEncContext *s) static inline int get_qscale(MpegEncContext *s) { int qscale = get_bits(&s->gb, 5); - if (s->codec_id == CODEC_ID_MPEG2VIDEO) { - if (s->q_scale_type) { - return non_linear_qscale[qscale]; - } else { - return qscale << 1; - } + if (s->q_scale_type) { + return non_linear_qscale[qscale]; + } else { + return qscale << 1; } - return qscale; } /* motion type (for mpeg2) */ @@ -1431,7 +1417,7 @@ static inline int mpeg1_decode_block_intra(MpegEncContext *s, } else if(level != 0) { i += run; j = scantable[i]; - level= (level*qscale*quant_matrix[j])>>3; + level= (level*qscale*quant_matrix[j])>>4; level= (level-1)|1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); @@ -1449,11 +1435,11 @@ static inline int mpeg1_decode_block_intra(MpegEncContext *s, j = scantable[i]; if(level<0){ level= -level; - level= (level*qscale*quant_matrix[j])>>3; + level= (level*qscale*quant_matrix[j])>>4; level= (level-1)|1; level= -level; }else{ - level= (level*qscale*quant_matrix[j])>>3; + level= (level*qscale*quant_matrix[j])>>4; level= (level-1)|1; } } @@ -1489,7 +1475,7 @@ static inline int mpeg1_decode_block_inter(MpegEncContext *s, v= SHOW_UBITS(re, &s->gb, 2); if (v & 2) { LAST_SKIP_BITS(re, &s->gb, 2); - level= (3*qscale*quant_matrix[0])>>4; + level= (3*qscale*quant_matrix[0])>>5; level= (level-1)|1; if(v&1) level= -level; @@ -1507,7 +1493,7 @@ static inline int mpeg1_decode_block_inter(MpegEncContext *s, } else if(level != 0) { i += run; j = scantable[i]; - level= ((level*2+1)*qscale*quant_matrix[j])>>4; + level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); @@ -1525,11 +1511,11 @@ static inline int mpeg1_decode_block_inter(MpegEncContext *s, j = scantable[i]; if(level<0){ level= -level; - level= ((level*2+1)*qscale*quant_matrix[j])>>4; + level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; level= -level; }else{ - level= ((level*2+1)*qscale*quant_matrix[j])>>4; + level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; } } @@ -1718,7 +1704,9 @@ static int mpeg_decode_init(AVCodecContext *avctx) { Mpeg1Context *s = avctx->priv_data; + s->mpeg_enc_ctx.avctx= avctx; s->mpeg_enc_ctx.flags= avctx->flags; + s->mpeg_enc_ctx.flags2= avctx->flags2; common_init(&s->mpeg_enc_ctx); init_vlcs(); @@ -1758,15 +1746,14 @@ static int mpeg1_decode_picture(AVCodecContext *avctx, { Mpeg1Context *s1 = avctx->priv_data; MpegEncContext *s = &s1->mpeg_enc_ctx; - int ref, f_code; + int ref, f_code, vbv_delay; init_get_bits(&s->gb, buf, buf_size*8); ref = get_bits(&s->gb, 10); /* temporal ref */ s->pict_type = get_bits(&s->gb, 3); - dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number); - skip_bits(&s->gb, 16); + vbv_delay= get_bits(&s->gb, 16); if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { s->full_pel[0] = get_bits1(&s->gb); f_code = get_bits(&s->gb, 3); @@ -1786,6 +1773,9 @@ static int mpeg1_decode_picture(AVCodecContext *avctx, s->current_picture.pict_type= s->pict_type; s->current_picture.key_frame= s->pict_type == I_TYPE; +// if(avctx->debug & FF_DEBUG_PICT_INFO) +// av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d\n", vbv_delay, ref); + s->y_dc_scale = 8; s->c_dc_scale = 8; s->first_slice = 1; @@ -1795,7 +1785,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx, static void mpeg_decode_sequence_extension(MpegEncContext *s) { int horiz_size_ext, vert_size_ext; - int bit_rate_ext, vbv_buf_ext; + int bit_rate_ext; int frame_rate_ext_n, frame_rate_ext_d; int level, profile; @@ -1811,7 +1801,7 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s) bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400; skip_bits1(&s->gb); /* marker */ - vbv_buf_ext = get_bits(&s->gb, 8); + s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10; s->low_delay = get_bits1(&s->gb); if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1; @@ -1840,7 +1830,8 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s) } if(s->avctx->debug & FF_DEBUG_PICT_INFO) - av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d \n", profile, level); + av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n", + profile, level, s->avctx->rc_buffer_size, s->bit_rate); } static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1) @@ -1950,7 +1941,7 @@ static void mpeg_decode_picture_coding_extension(MpegEncContext *s) s->repeat_first_field = get_bits1(&s->gb); s->chroma_420_type = get_bits1(&s->gb); s->progressive_frame = get_bits1(&s->gb); - + if(s->picture_structure == PICT_FRAME) s->first_field=0; else{ @@ -1961,13 +1952,9 @@ static void mpeg_decode_picture_coding_extension(MpegEncContext *s) if(s->alternate_scan){ ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan); ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan); - ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan); - ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); }else{ ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct); ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct); - ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan); - ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); } /* composite display not parsed */ @@ -2101,10 +2088,10 @@ static int mpeg_decode_slice(AVCodecContext *avctx, s->qscale = get_qscale(s); if (s->first_slice && (s->first_field || s->picture_structure==PICT_FRAME)) { if(s->avctx->debug&FF_DEBUG_PICT_INFO){ - av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n", + av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n", s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1], s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")), - s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"", + s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"", s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors, s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :""); } @@ -2292,10 +2279,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) if (/*s->mb_y<mb_height &&*/ !s->first_field) { /* end of image */ - if(s->codec_id == CODEC_ID_MPEG2VIDEO){ - s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2; - }else - s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1; + s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2; ff_er_frame_end(s); @@ -2303,14 +2287,14 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict) if (s->pict_type == B_TYPE || s->low_delay) { *pict= *(AVFrame*)s->current_picture_ptr; - ff_print_debug_info(s, s->current_picture_ptr); + ff_print_debug_info(s, pict); } else { s->picture_number++; /* latency of 1 frame for I and P frames */ /* XXX: use another variable than picture_number */ if (s->last_picture_ptr != NULL) { *pict= *(AVFrame*)s->last_picture_ptr; - ff_print_debug_info(s, s->last_picture_ptr); + ff_print_debug_info(s, pict); } } @@ -2357,7 +2341,6 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->width = width; s->height = height; avctx->has_b_frames= 1; - s->avctx = avctx; avctx->width = width; avctx->height = height; av_reduce( @@ -2381,7 +2364,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->swap_uv = 0;//just in case vcr2 and mpeg2 stream have been concatinated } - skip_bits(&s->gb, 10); /* vbv_buffer_size */ + s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16; skip_bits(&s->gb, 1); /* get matrix */ @@ -2436,6 +2419,11 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO; avctx->sub_id = 1; /* indicates mpeg1 */ if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1; + + if(s->avctx->debug & FF_DEBUG_PICT_INFO) + av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n", + s->avctx->rc_buffer_size, s->bit_rate); + return 0; } @@ -2454,7 +2442,6 @@ static int vcr2_init_sequence(AVCodecContext *avctx) s->height = avctx->height; avctx->has_b_frames= 0; //true? s->low_delay= 1; - s->avctx = avctx; //get_format() or set_video(width,height,aspect,pix_fmt); //until then pix_fmt may be changed right after codec init @@ -2726,6 +2713,32 @@ AVCodec mpegvideo_decoder = { .flush= ff_mpeg_flush, }; +#ifdef CONFIG_ENCODERS + +AVCodec mpeg1video_encoder = { + "mpeg1video", + CODEC_TYPE_VIDEO, + CODEC_ID_MPEG1VIDEO, + sizeof(MpegEncContext), + encode_init, + MPV_encode_picture, + MPV_encode_end, +}; + +#ifdef CONFIG_RISKY + +AVCodec mpeg2video_encoder = { + "mpeg2video", + CODEC_TYPE_VIDEO, + CODEC_ID_MPEG2VIDEO, + sizeof(MpegEncContext), + encode_init, + MPV_encode_picture, + MPV_encode_end, +}; +#endif +#endif + #ifdef HAVE_XVMC static int mpeg_mc_decode_init(AVCodecContext *avctx){ Mpeg1Context *s; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12data.h b/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12data.h index 70d23dfb9b..42b3d49a1f 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12data.h +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mpeg12data.h @@ -433,3 +433,10 @@ static const AVRational mpeg2_aspect[16]={ {0,1}, }; +static const uint8_t svcd_scan_offset_placeholder[14]={ + 0x10, 0x0E, + 0x00, 0x80, 0x81, + 0x00, 0x80, 0x81, + 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, +}; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mpegaudiodec.c b/src/add-ons/media/plugins/avcodec/libavcodec/mpegaudiodec.c index e94d91900d..09e9b8cdbc 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mpegaudiodec.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mpegaudiodec.c @@ -25,6 +25,7 @@ //#define DEBUG #include "avcodec.h" #include "mpegaudio.h" +#include "dsputil.h" /* * TODO: @@ -66,6 +67,8 @@ typedef int32_t MPA_INT; #define HEADER_SIZE 4 #define BACKSTEP_SIZE 512 +struct GranuleDef; + typedef struct MPADecodeContext { uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */ int inbuf_index; @@ -93,6 +96,7 @@ typedef struct MPADecodeContext { #ifdef DEBUG int frame_count; #endif + void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g); } MPADecodeContext; /* layer 3 "granule" */ @@ -127,6 +131,9 @@ typedef struct HuffTable { #include "mpegaudiodectab.h" +static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); +static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); + /* vlc structure for decoding layer 3 huffman tables */ static VLC huff_vlc[16]; static uint8_t *huff_code_table[16]; @@ -144,7 +151,8 @@ static uint32_t *table_4_3_value; /* intensity stereo coef table */ static int32_t is_table[2][16]; static int32_t is_table_lsf[2][2][16]; -static int32_t csa_table[8][2]; +static int32_t csa_table[8][4]; +static float csa_table_float[8][4]; static int32_t mdct_win[8][36]; /* lower 2 bits: modulo 3, higher bits: shift */ @@ -310,6 +318,11 @@ static int decode_init(AVCodecContext * avctx) static int init=0; int i, j, k; + if(avctx->antialias_algo == FF_AA_INT) + s->compute_antialias= compute_antialias_integer; + else + s->compute_antialias= compute_antialias_float; + if (!init && !avctx->parse_only) { /* scale factors table for layer 1/2 */ for(i=0;i<64;i++) { @@ -462,6 +475,13 @@ static int decode_init(AVCodecContext * avctx) ca = cs * ci; csa_table[i][0] = FIX(cs); csa_table[i][1] = FIX(ca); + csa_table[i][2] = FIX(ca) + FIX(cs); + csa_table[i][3] = FIX(ca) - FIX(cs); + csa_table_float[i][0] = cs; + csa_table_float[i][1] = ca; + csa_table_float[i][2] = ca + cs; + csa_table_float[i][3] = ca - cs; +// printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca)); } /* compute mdct windows */ @@ -1892,11 +1912,11 @@ static void compute_stereo(MPADecodeContext *s, } } -static void compute_antialias(MPADecodeContext *s, +static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g) { int32_t *ptr, *p0, *p1, *csa; - int n, tmp0, tmp1, i, j; + int n, i, j; /* we antialias only "long" bands */ if (g->block_type == 2) { @@ -1912,17 +1932,85 @@ static void compute_antialias(MPADecodeContext *s, for(i = n;i > 0;i--) { p0 = ptr - 1; p1 = ptr; - csa = &csa_table[0][0]; - for(j=0;j<8;j++) { - tmp0 = *p0; - tmp1 = *p1; + csa = &csa_table[0][0]; + for(j=0;j<4;j++) { + int tmp0 = *p0; + int tmp1 = *p1; +#if 0 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1])); *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0])); - p0--; - p1++; - csa += 2; +#else + int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]); + *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2])); + *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3])); +#endif + p0--; p1++; + csa += 4; + tmp0 = *p0; + tmp1 = *p1; +#if 0 + *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1])); + *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0])); +#else + tmp2= MUL64(tmp0 + tmp1, csa[0]); + *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2])); + *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3])); +#endif + p0--; p1++; + csa += 4; } - ptr += 18; + ptr += 18; + } +} + +static void compute_antialias_float(MPADecodeContext *s, + GranuleDef *g) +{ + int32_t *ptr, *p0, *p1; + int n, i, j; + + /* we antialias only "long" bands */ + if (g->block_type == 2) { + if (!g->switch_point) + return; + /* XXX: check this for 8000Hz case */ + n = 1; + } else { + n = SBLIMIT - 1; + } + + ptr = g->sb_hybrid + 18; + for(i = n;i > 0;i--) { + float *csa = &csa_table_float[0][0]; + p0 = ptr - 1; + p1 = ptr; + for(j=0;j<4;j++) { + float tmp0 = *p0; + float tmp1 = *p1; +#if 1 + *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]); + *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]); +#else + float tmp2= (tmp0 + tmp1) * csa[0]; + *p0 = lrintf(tmp2 - tmp1 * csa[2]); + *p1 = lrintf(tmp2 + tmp0 * csa[3]); +#endif + p0--; p1++; + csa += 4; + tmp0 = *p0; + tmp1 = *p1; +#if 1 + *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]); + *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]); +#else + tmp2= (tmp0 + tmp1) * csa[0]; + *p0 = lrintf(tmp2 - tmp1 * csa[2]); + *p1 = lrintf(tmp2 + tmp0 * csa[3]); +#endif + p0--; p1++; + csa += 4; + } + ptr += 18; } } @@ -2352,7 +2440,7 @@ static int mp_decode_layer3(MPADecodeContext *s) #if defined(DEBUG) sample_dump(0, g->sb_hybrid, 576); #endif - compute_antialias(s, g); + s->compute_antialias(s, g); #if defined(DEBUG) sample_dump(1, g->sb_hybrid, 576); #endif diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/mpegvideo.c b/src/add-ons/media/plugins/avcodec/libavcodec/mpegvideo.c index 61976d7d16..7d1c5ba1ef 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/mpegvideo.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/mpegvideo.c @@ -1,6 +1,7 @@ /* * The simplest mpeg encoder (well, it was the simplest!) * Copyright (c) 2000,2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -57,6 +58,7 @@ static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w); static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); static int sse_mb(MpegEncContext *s); +static void denoise_dct_c(MpegEncContext *s, DCTELEM *block); #endif //CONFIG_ENCODERS #ifdef HAVE_XVMC @@ -219,6 +221,7 @@ int DCT_common_init(MpegEncContext *s) #ifdef CONFIG_ENCODERS s->dct_quantize= dct_quantize_c; + s->denoise_dct= denoise_dct_c; #endif #ifdef HAVE_MMX @@ -252,8 +255,13 @@ int DCT_common_init(MpegEncContext *s) /* load & permutate scantables note: only wmv uses differnt ones */ - ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct); - ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct); + if(s->alternate_scan){ + ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan); + ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan); + }else{ + ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct); + ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct); + } ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan); ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); @@ -267,6 +275,17 @@ static void copy_picture(Picture *dst, Picture *src){ dst->type= FF_BUFFER_TYPE_COPY; } +static void copy_picture_attributes(AVFrame *dst, AVFrame *src){ + dst->pict_type = src->pict_type; + dst->quality = src->quality; + dst->coded_picture_number = src->coded_picture_number; + dst->display_picture_number = src->display_picture_number; +// dst->reference = src->reference; + dst->pts = src->pts; + dst->interlaced_frame = src->interlaced_frame; + dst->top_field_first = src->top_field_first; +} + /** * allocates a Picture * The pixels are allocated/set by calling get_buffer() if shared=0 @@ -317,18 +336,18 @@ static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){ CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t)) - CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int)) + CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t)) pic->mb_type= pic->mb_type_base + s->mb_stride+1; if(s->out_format == FMT_H264){ for(i=0; i<2; i++){ - CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1) * sizeof(uint16_t)) + CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+1) * sizeof(int16_t)) pic->motion_val[i]= pic->motion_val_base[i]+1; CHECKED_ALLOCZ(pic->ref_index[i] , b8_array_size * sizeof(uint8_t)) } pic->motion_subsample_log2= 2; - }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&(FF_DEBUG_VIS_MV|FF_DEBUG_MV))){ + }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){ for(i=0; i<2; i++){ - CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(uint16_t)*2) //FIXME + CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+1) * sizeof(int16_t)*2) //FIXME pic->motion_val[i]= pic->motion_val_base[i]+1; } pic->motion_subsample_log2= 3; @@ -383,12 +402,13 @@ static void free_picture(MpegEncContext *s, Picture *pic){ /* init common structure for both encoder and decoder */ int MPV_common_init(MpegEncContext *s) { - int y_size, c_size, yc_size, i, mb_array_size, x, y; + int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; dsputil_init(&s->dsp, s->avctx); DCT_common_init(s); s->flags= s->avctx->flags; + s->flags2= s->avctx->flags2; s->mb_width = (s->width + 15) / 16; s->mb_height = (s->height + 15) / 16; @@ -396,6 +416,7 @@ int MPV_common_init(MpegEncContext *s) s->b8_stride = s->mb_width*2 + 1; s->b4_stride = s->mb_width*4 + 1; mb_array_size= s->mb_height * s->mb_stride; + mv_table_size= (s->mb_height+2) * s->mb_stride + 1; /* set default edge pos, will be overriden in decode_header if needed */ s->h_edge_pos= s->mb_width*16; @@ -416,6 +437,7 @@ int MPV_common_init(MpegEncContext *s) if (!s->encoding) s->progressive_sequence= 1; s->progressive_frame= 1; + s->coded_picture_number = 0; y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); c_size = (s->mb_width + 2) * (s->mb_height + 2); @@ -446,8 +468,6 @@ int MPV_common_init(MpegEncContext *s) s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed? if (s->encoding) { - int mv_table_size= s->mb_stride * (s->mb_height+2) + 1; - /* Allocate MV tables */ CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t)) @@ -479,7 +499,7 @@ int MPV_common_init(MpegEncContext *s) CHECKED_ALLOCZ(s->avctx->stats_out, 256); /* Allocate MB type table */ - CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding + CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t)) //needed for encoding CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int)) @@ -501,10 +521,21 @@ int MPV_common_init(MpegEncContext *s) CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t)) - if(s->codec_id==CODEC_ID_MPEG4){ + if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){ /* interlaced direct mode decoding tables */ - CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t)) - CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t)) + for(i=0; i<2; i++){ + int j, k; + for(j=0; j<2; j++){ + for(k=0; k<2; k++){ + CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t)) + s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1; + } + CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t)) + CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t)) + s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1; + } + CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t)) + } } if (s->out_format == FMT_H263) { /* ac values */ @@ -552,6 +583,11 @@ int MPV_common_init(MpegEncContext *s) } s->parse_context.state= -1; + if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){ + s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH); + s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); + s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH); + } s->context_initialized = 1; return 0; @@ -566,7 +602,7 @@ int MPV_common_init(MpegEncContext *s) /* init common structure for both encoder and decoder */ void MPV_common_end(MpegEncContext *s) { - int i; + int i, j, k; av_freep(&s->parse_context.buffer); s->parse_context.buffer_size=0; @@ -584,6 +620,18 @@ void MPV_common_end(MpegEncContext *s) s->b_bidir_forw_mv_table= NULL; s->b_bidir_back_mv_table= NULL; s->b_direct_mv_table= NULL; + for(i=0; i<2; i++){ + for(j=0; j<2; j++){ + for(k=0; k<2; k++){ + av_freep(&s->b_field_mv_table_base[i][j][k]); + s->b_field_mv_table[i][j][k]=NULL; + } + av_freep(&s->b_field_select_table[i][j]); + av_freep(&s->p_field_mv_table_base[i][j]); + s->p_field_mv_table[i][j]=NULL; + } + av_freep(&s->p_field_select_table[i]); + } av_freep(&s->dc_val[0]); av_freep(&s->ac_val[0]); @@ -601,8 +649,6 @@ void MPV_common_end(MpegEncContext *s) av_freep(&s->tex_pb_buffer); av_freep(&s->pb2_buffer); av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL; - av_freep(&s->field_mv_table); - av_freep(&s->field_select_table); av_freep(&s->avctx->stats_out); av_freep(&s->ac_stats); av_freep(&s->error_status_table); @@ -629,6 +675,9 @@ void MPV_common_end(MpegEncContext *s) s->last_picture_ptr= s->next_picture_ptr= s->current_picture_ptr= NULL; + for(i=0; i<3; i++) + if (s->visualization_buffer[i]) + av_free(s->visualization_buffer[i]); } #ifdef CONFIG_ENCODERS @@ -652,6 +701,7 @@ int MPV_encode_init(AVCodecContext *avctx) s->gop_size = avctx->gop_size; s->avctx = avctx; s->flags= avctx->flags; + s->flags2= avctx->flags2; s->max_b_frames= avctx->max_b_frames; s->codec_id= avctx->codec->id; s->luma_elim_threshold = avctx->luma_elim_threshold; @@ -672,7 +722,7 @@ int MPV_encode_init(AVCodecContext *avctx) s->me_method = avctx->me_method; /* Fixed QSCALE */ - s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE); + s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE); s->adaptive_quant= ( s->avctx->lumi_masking || s->avctx->dark_masking @@ -682,15 +732,25 @@ int MPV_encode_init(AVCodecContext *avctx) || (s->flags&CODEC_FLAG_QP_RD)) && !s->fixed_qscale; - s->obmc= (s->flags & CODEC_FLAG_OBMC); - s->loop_filter= (s->flags & CODEC_FLAG_LOOP_FILTER); + s->obmc= !!(s->flags & CODEC_FLAG_OBMC); + s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER); + s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN); + if(avctx->rc_max_rate && !avctx->rc_buffer_size){ + av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n"); + return -1; + } + + if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){ + av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isnt recommanded!\n"); + } + if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){ av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n"); return -1; } - + if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){ av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n"); return -1; @@ -731,6 +791,19 @@ int MPV_encode_init(AVCodecContext *avctx) return -1; } + if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){ + av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n"); + return -1; + } + + i= ff_gcd(avctx->frame_rate, avctx->frame_rate_base); + if(i > 1){ + av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n"); + avctx->frame_rate /= i; + avctx->frame_rate_base /= i; +// return -1; + } + if(s->codec_id==CODEC_ID_MJPEG){ s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x s->inter_quant_bias= 0; @@ -882,7 +955,7 @@ int MPV_encode_init(AVCodecContext *avctx) default: return -1; } - + { /* set up some save defaults, some codecs might override them later */ static int done=0; if(!done){ @@ -914,7 +987,9 @@ int MPV_encode_init(AVCodecContext *avctx) if(s->modified_quant) s->chroma_qscale_table= ff_h263_chroma_qscale_table; s->progressive_frame= - s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT); + s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)); + + ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp); ff_init_me(s); @@ -964,8 +1039,8 @@ int MPV_encode_init(AVCodecContext *avctx) return -1; s->picture_number = 0; + s->input_picture_number = 0; s->picture_in_gop_number = 0; - s->fake_picture_number = 0; /* motion detector init */ s->f_code = 1; s->b_code = 1; @@ -1142,8 +1217,7 @@ alloc: pic->reference= s->pict_type != B_TYPE ? 3 : 0; - if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext - pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1; + pic->coded_picture_number= s->coded_picture_number++; if( alloc_picture(s, (Picture*)pic, 0) < 0) return -1; @@ -1349,13 +1423,22 @@ static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int /** * prints debuging info for the given picture. */ -void ff_print_debug_info(MpegEncContext *s, Picture *pict){ +void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){ if(!pict || !pict->mb_type) return; if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){ int x,y; - + + av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: "); + switch (pict->pict_type) { + case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break; + case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break; + case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break; + case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break; + case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break; + case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break; + } for(y=0; ymb_height; y++){ for(x=0; xmb_width; x++){ if(s->avctx->debug&FF_DEBUG_SKIP){ @@ -1368,7 +1451,6 @@ void ff_print_debug_info(MpegEncContext *s, Picture *pict){ } if(s->avctx->debug&FF_DEBUG_MB_TYPE){ int mb_type= pict->mb_type[x + y*s->mb_stride]; - //Type & MV direction if(IS_PCM(mb_type)) av_log(s->avctx, AV_LOG_DEBUG, "P"); @@ -1421,45 +1503,76 @@ void ff_print_debug_info(MpegEncContext *s, Picture *pict){ } } - if(s->avctx->debug&(FF_DEBUG_VIS_MV|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)){ + if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){ const int shift= 1 + s->quarter_sample; int mb_y; - uint8_t *ptr= pict->data[0]; + uint8_t *ptr; + int i; + int h_chroma_shift, v_chroma_shift; s->low_delay=0; //needed to see the vectors without trashing the buffers + avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); + for(i=0; i<3; i++){ + memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*s->height:pict->linesize[i]*s->height >> v_chroma_shift); + pict->data[i]= s->visualization_buffer[i]; + } + pict->type= FF_BUFFER_TYPE_COPY; + ptr= pict->data[0]; + for(mb_y=0; mb_ymb_height; mb_y++){ int mb_x; for(mb_x=0; mb_xmb_width; mb_x++){ const int mb_index= mb_x + mb_y*s->mb_stride; - if((s->avctx->debug&FF_DEBUG_VIS_MV) && pict->motion_val){ - if(IS_8X8(pict->mb_type[mb_index])){ - int i; - for(i=0; i<4; i++){ + if((s->avctx->debug_mv) && pict->motion_val){ + int type; + for(type=0; type<3; type++){ + int direction; + switch (type) { + case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE)) + continue; + direction = 0; + break; + case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE)) + continue; + direction = 0; + break; + case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE)) + continue; + direction = 1; + break; + } + if(!USES_LIST(pict->mb_type[mb_index], direction)) + continue; + + if(IS_8X8(pict->mb_type[mb_index])){ + int i; + for(i=0; i<4; i++){ int sx= mb_x*16 + 4 + 8*(i&1); int sy= mb_y*16 + 4 + 8*(i>>1); int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2); - int mx= (pict->motion_val[0][xy][0]>>shift) + sx; - int my= (pict->motion_val[0][xy][1]>>shift) + sy; + int mx= (pict->motion_val[direction][xy][0]>>shift) + sx; + int my= (pict->motion_val[direction][xy][1]>>shift) + sy; draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100); - } - }else if(IS_16X8(pict->mb_type[mb_index])){ - int i; - for(i=0; i<2; i++){ + } + }else if(IS_16X8(pict->mb_type[mb_index])){ + int i; + for(i=0; i<2; i++){ int sx=mb_x*16 + 8; int sy=mb_y*16 + 4 + 8*i; int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2); - int mx=(pict->motion_val[0][xy][0]>>shift) + sx; - int my=(pict->motion_val[0][xy][1]>>shift) + sy; + int mx=(pict->motion_val[direction][xy][0]>>shift) + sx; + int my=(pict->motion_val[direction][xy][1]>>shift) + sy; draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100); + } + }else{ + int sx= mb_x*16 + 8; + int sy= mb_y*16 + 8; + int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2); + int mx= (pict->motion_val[direction][xy][0]>>shift) + sx; + int my= (pict->motion_val[direction][xy][1]>>shift) + sy; + draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100); } - }else{ - int sx= mb_x*16 + 8; - int sy= mb_y*16 + 8; - int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2); - int mx= (pict->motion_val[0][xy][0]>>shift) + sx; - int my= (pict->motion_val[0][xy][1]>>shift) + sy; - draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100); - } + } } if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){ uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL; @@ -1556,7 +1669,7 @@ static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int st for(y=0; ydsp.pix_abs16x16(src + offset, ref + offset, stride); + int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16); int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8; int sae = get_sae(src + offset, mean, stride); @@ -1631,15 +1744,9 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){ } } } - pic->quality= pic_arg->quality; - pic->pict_type= pic_arg->pict_type; - pic->pts = pic_arg->pts; - pic->interlaced_frame = pic_arg->interlaced_frame; - pic->top_field_first = pic_arg->top_field_first; - - if(s->input_picture[encoding_delay]) - pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1; + copy_picture_attributes(pic, pic_arg); + pic->display_picture_number= s->input_picture_number++; } /* shift buffer entries */ @@ -1653,10 +1760,6 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){ static void select_input_picture(MpegEncContext *s){ int i; - int coded_pic_num=0; - - if(s->reordered_input_picture[0]) - coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1; for(i=1; ireordered_input_picture[i-1]= s->reordered_input_picture[i]; @@ -1667,7 +1770,7 @@ static void select_input_picture(MpegEncContext *s){ if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){ s->reordered_input_picture[0]= s->input_picture[0]; s->reordered_input_picture[0]->pict_type= I_TYPE; - s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; + s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++; }else{ int b_frames; @@ -1721,19 +1824,25 @@ static void select_input_picture(MpegEncContext *s){ //static int b_count=0; //b_count+= b_frames; //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count); - + if(s->picture_in_gop_number + b_frames >= s->gop_size){ + if(s->flags & CODEC_FLAG_CLOSED_GOP) + b_frames=0; + s->input_picture[b_frames]->pict_type= I_TYPE; + } + + if( (s->flags & CODEC_FLAG_CLOSED_GOP) + && b_frames + && s->input_picture[b_frames]->pict_type== I_TYPE) + b_frames--; + s->reordered_input_picture[0]= s->input_picture[b_frames]; - if( s->picture_in_gop_number + b_frames >= s->gop_size - || s->reordered_input_picture[0]->pict_type== I_TYPE) - s->reordered_input_picture[0]->pict_type= I_TYPE; - else + if(s->reordered_input_picture[0]->pict_type != I_TYPE) s->reordered_input_picture[0]->pict_type= P_TYPE; - s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; + s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++; for(i=0; ireordered_input_picture[i+1]= s->input_picture[i]; s->reordered_input_picture[i+1]->pict_type= B_TYPE; - s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num; + s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++; } } } @@ -1754,12 +1863,8 @@ static void select_input_picture(MpegEncContext *s){ s->reordered_input_picture[0]->data[i]= NULL; s->reordered_input_picture[0]->type= 0; - //FIXME bad, copy * except - pic->pict_type = s->reordered_input_picture[0]->pict_type; - pic->quality = s->reordered_input_picture[0]->quality; - pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number; - pic->reference = s->reordered_input_picture[0]->reference; - pic->pts = s->reordered_input_picture[0]->pts; + copy_picture_attributes((AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]); + pic->reference = s->reordered_input_picture[0]->reference; alloc_picture(s, pic, 0); @@ -1806,7 +1911,6 @@ int MPV_encode_picture(AVCodecContext *avctx, /* output? */ if(s->new_picture.data[0]){ - s->pict_type= s->new_picture.pict_type; //emms_c(); //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); @@ -1835,39 +1939,56 @@ int MPV_encode_picture(AVCodecContext *avctx, for(i=0; i<4; i++){ avctx->error[i] += s->current_picture_ptr->error[i]; } - } - s->input_picture_number++; - - flush_put_bits(&s->pb); - s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; - - stuffing_count= ff_vbv_update(s, s->frame_bits); - if(stuffing_count){ - switch(s->codec_id){ - case CODEC_ID_MPEG1VIDEO: - case CODEC_ID_MPEG2VIDEO: - while(stuffing_count--){ - put_bits(&s->pb, 8, 0); - } - break; - case CODEC_ID_MPEG4: - put_bits(&s->pb, 16, 0); - put_bits(&s->pb, 16, 0x1C3); - stuffing_count -= 4; - while(stuffing_count--){ - put_bits(&s->pb, 8, 0xFF); - } - break; - default: - av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n"); - } flush_put_bits(&s->pb); s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; + + stuffing_count= ff_vbv_update(s, s->frame_bits); + if(stuffing_count){ + switch(s->codec_id){ + case CODEC_ID_MPEG1VIDEO: + case CODEC_ID_MPEG2VIDEO: + while(stuffing_count--){ + put_bits(&s->pb, 8, 0); + } + break; + case CODEC_ID_MPEG4: + put_bits(&s->pb, 16, 0); + put_bits(&s->pb, 16, 0x1C3); + stuffing_count -= 4; + while(stuffing_count--){ + put_bits(&s->pb, 8, 0xFF); + } + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n"); + } + flush_put_bits(&s->pb); + s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; + } + + /* update mpeg1/2 vbv_delay for CBR */ + if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate){ + int vbv_delay; + + assert(s->repeat_first_field==0); + + vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate); + assert(vbv_delay < 0xFFFF); + + s->vbv_delay_ptr[0] &= 0xF8; + s->vbv_delay_ptr[0] |= vbv_delay>>13; + s->vbv_delay_ptr[1] = vbv_delay>>5; + s->vbv_delay_ptr[2] &= 0x07; + s->vbv_delay_ptr[2] |= vbv_delay<<3; + } + s->total_bits += s->frame_bits; + avctx->frame_bits = s->frame_bits; + }else{ + assert((pbBufPtr(&s->pb) == s->pb.buf)); + s->frame_bits=0; } - - s->total_bits += s->frame_bits; - avctx->frame_bits = s->frame_bits; + assert((s->frame_bits&7)==0); return s->frame_bits/8; } @@ -3048,6 +3169,7 @@ static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index int i; const int maxlevel= s->max_qcoeff; const int minlevel= s->min_qcoeff; + int overflow=0; if(s->mb_intra){ i=1; //skip clipping of intra dc @@ -3058,78 +3180,21 @@ static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index const int j= s->intra_scantable.permutated[i]; int level = block[j]; - if (level>maxlevel) level=maxlevel; - else if(levelmaxlevel){ + level=maxlevel; + overflow++; + }else if(levelavctx->mb_decision == FF_MB_DECISION_SIMPLE) + av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%d\n", overflow, minlevel, maxlevel); } -#if 0 -static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize - int score=0; - int x,y; - - for(y=0; y<7; y++){ - for(x=0; x<16; x+=4){ - score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride]) - +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]); - } - s+= stride; - } - - return score; -} - -static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize - int score=0; - int x,y; - - for(y=0; y<7; y++){ - for(x=0; x<16; x++){ - score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]); - } - s1+= stride; - s2+= stride; - } - - return score; -} -#else -#define SQ(a) ((a)*(a)) - -static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize - int score=0; - int x,y; - - for(y=0; y<7; y++){ - for(x=0; x<16; x+=4){ - score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride]) - +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]); - } - s+= stride; - } - - return score; -} - -static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize - int score=0; - int x,y; - - for(y=0; y<7; y++){ - for(x=0; x<16; x++){ - score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]); - } - s1+= stride; - s2+= stride; - } - - return score; -} - -#endif - #endif //CONFIG_ENCODERS /** @@ -3248,17 +3313,21 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) if(s->flags&CODEC_FLAG_INTERLACED_DCT){ int progressive_score, interlaced_score; + + s->interlaced_dct=0; + progressive_score= s->dsp.ildct_cmp[4](s, ptr , NULL, wrap_y, 8) + +s->dsp.ildct_cmp[4](s, ptr + wrap_y*8, NULL, wrap_y, 8) - 400; + + if(progressive_score > 0){ + interlaced_score = s->dsp.ildct_cmp[4](s, ptr , NULL, wrap_y*2, 8) + +s->dsp.ildct_cmp[4](s, ptr + wrap_y , NULL, wrap_y*2, 8); + if(progressive_score > interlaced_score){ + s->interlaced_dct=1; - progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y ); - interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2); - - if(progressive_score > interlaced_score + 100){ - s->interlaced_dct=1; - - dct_offset= wrap_y; - wrap_y<<=1; - }else - s->interlaced_dct=0; + dct_offset= wrap_y; + wrap_y<<=1; + } + } } s->dsp.get_pixels(s->block[0], ptr , wrap_y); @@ -3327,19 +3396,24 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) if(s->flags&CODEC_FLAG_INTERLACED_DCT){ int progressive_score, interlaced_score; + + s->interlaced_dct=0; + progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8) + +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400; - progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y ) - + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y ); - interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2) - + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2); + if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400; + + if(progressive_score>0){ + interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8) + +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8); - if(progressive_score > interlaced_score + 600){ - s->interlaced_dct=1; + if(progressive_score > interlaced_score){ + s->interlaced_dct=1; - dct_offset= wrap_y; - wrap_y<<=1; - }else - s->interlaced_dct=0; + dct_offset= wrap_y; + wrap_y<<=1; + } + } } s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); @@ -3365,12 +3439,12 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) /* pre quantization */ if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){ //FIXME optimize - if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1; - if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1; - if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1; - if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1; - if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1; - if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1; + if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1; + if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1; + if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1; + if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1; + if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1; + if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1; #if 0 { static int stat[7]; @@ -3432,6 +3506,19 @@ static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale; } + //non c quantize code returns incorrect block_last_index FIXME + if(s->alternate_scan && s->dct_quantize != dct_quantize_c){ + for(i=0; i<6; i++){ + int j; + if(s->block_last_index[i]>0){ + for(j=63; j>0; j--){ + if(s->block[i][ s->intra_scantable.permutated[j] ]) break; + } + s->block_last_index[i]= j; + } + } + } + /* huffman encode */ switch(s->codec_id){ //FIXME funct ptr could be slightly faster case CODEC_ID_MPEG1VIDEO: @@ -3523,6 +3610,9 @@ void ff_mpeg_flush(AVCodecContext *avctx){ int i; MpegEncContext *s = avctx->priv_data; + if(s==NULL || s->picture==NULL) + return; + for(i=0; ipicture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL || s->picture[i].type == FF_BUFFER_TYPE_USER)) @@ -3672,9 +3762,9 @@ static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, in int x,y; if(w==16 && h==16) - return s->dsp.sse[0](NULL, src1, src2, stride); + return s->dsp.sse[0](NULL, src1, src2, stride, 16); else if(w==8 && h==8) - return s->dsp.sse[1](NULL, src1, src2, stride); + return s->dsp.sse[1](NULL, src1, src2, stride, 8); for(y=0; ymb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; if(w==16 && h==16) - return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize) - +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize) - +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize); + return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16) + +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8) + +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8); else return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize) +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize) @@ -3707,7 +3797,7 @@ static int sse_mb(MpegEncContext *s){ static void encode_picture(MpegEncContext *s, int picture_number) { int mb_x, mb_y, pdif = 0; - int i; + int i, j; int bits; MpegEncContext best_s, backup_s; uint8_t bit_buf[2][3000]; @@ -3755,9 +3845,9 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->me.dia_size= s->avctx->pre_dia_size; for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) { + s->mb_y = mb_y; for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) { s->mb_x = mb_x; - s->mb_y = mb_y; ff_pre_estimate_p_frame_motion(s, mb_x, mb_y); } } @@ -3767,13 +3857,13 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->me.dia_size= s->avctx->dia_size; for(mb_y=0; mb_y < s->mb_height; mb_y++) { + s->mb_y = mb_y; s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); for(mb_x=0; mb_x < s->mb_width; mb_x++) { s->mb_x = mb_x; - s->mb_y = mb_y; s->block_index[0]+=2; s->block_index[1]+=2; s->block_index[2]+=2; @@ -3788,10 +3878,8 @@ static void encode_picture(MpegEncContext *s, int picture_number) } }else /* if(s->pict_type == I_TYPE) */{ /* I-Frame */ - //FIXME do we need to zero them? - memset(s->current_picture.motion_val[0][0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); - memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2); - memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height); + for(i=0; imb_stride*s->mb_height; i++) + s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA; if(!s->fixed_qscale){ /* finding spatial complexity for I-frame rate control */ @@ -3816,32 +3904,61 @@ static void encode_picture(MpegEncContext *s, int picture_number) if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){ s->pict_type= I_TYPE; - memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height); + for(i=0; imb_stride*s->mb_height; i++) + s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA; //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum); } if(!s->umvplus){ if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) { - s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); - + s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER); + + if(s->flags & CODEC_FLAG_INTERLACED_ME){ + int a,b; + a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select + b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I); + s->f_code= FFMAX(s->f_code, FFMAX(a,b)); + } + ff_fix_long_p_mvs(s); + ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0); + if(s->flags & CODEC_FLAG_INTERLACED_ME){ + for(i=0; i<2; i++){ + for(j=0; j<2; j++) + ff_fix_long_mvs(s, s->p_field_select_table[i], j, + s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0); + } + } } if(s->pict_type==B_TYPE){ int a, b; - a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); - b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR); + a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD); + b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR); s->f_code = FFMAX(a, b); - a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD); - b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR); + a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD); + b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR); s->b_code = FFMAX(a, b); - ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD); - ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD); - ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR); - ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR); + ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1); + ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1); + ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1); + ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1); + if(s->flags & CODEC_FLAG_INTERLACED_ME){ + int dir; + for(dir=0; dir<2; dir++){ + for(i=0; i<2; i++){ + for(j=0; j<2; j++){ + int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I) + : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I); + ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j, + s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1); + } + } + } + } } } @@ -3917,6 +4034,8 @@ static void encode_picture(MpegEncContext *s, int picture_number) break; case FMT_H264: break; + default: + assert(0); } bits= get_bit_count(&s->pb); s->header_bits= bits - s->last_bits; @@ -3938,10 +4057,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->current_picture_ptr->error[i] = 0; } s->mb_skip_run = 0; - s->last_mv[0][0][0] = 0; - s->last_mv[0][0][1] = 0; - s->last_mv[1][0][0] = 0; - s->last_mv[1][0][1] = 0; + memset(s->last_mv, 0, sizeof(s->last_mv)); s->last_mv_dir = 0; @@ -3975,6 +4091,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) int mb_type= s->mb_type[xy]; // int d; int dmin= INT_MAX; + int dir; s->mb_x = mb_x; ff_update_block_index(s); @@ -4082,25 +4199,37 @@ static void encode_picture(MpegEncContext *s, int picture_number) backup_s.tex_pb= s->tex_pb; } - if(mb_type&MB_TYPE_INTER){ + if(mb_type&CANDIDATE_MB_TYPE_INTER){ s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_16X16; s->mb_intra= 0; s->mv[0][0][0] = s->p_mv_table[xy][0]; s->mv[0][0][1] = s->p_mv_table[xy][1]; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb, &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); } - if(mb_type&MB_TYPE_SKIPED){ + if(mb_type&CANDIDATE_MB_TYPE_INTER_I){ + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[0][i] = s->p_field_select_table[i][xy]; + s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0]; + s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1]; + } + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb, + &dmin, &next_block, 0, 0); + } + if(mb_type&CANDIDATE_MB_TYPE_SKIPED){ s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_16X16; s->mb_intra= 0; s->mv[0][0][0] = 0; s->mv[0][0][1] = 0; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_SKIPED, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb, &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); } - if(mb_type&MB_TYPE_INTER4V){ + if(mb_type&CANDIDATE_MB_TYPE_INTER4V){ s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_8X8; s->mb_intra= 0; @@ -4108,28 +4237,28 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0]; s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1]; } - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb, &dmin, &next_block, 0, 0); } - if(mb_type&MB_TYPE_FORWARD){ + if(mb_type&CANDIDATE_MB_TYPE_FORWARD){ s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_16X16; s->mb_intra= 0; s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb, &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); } - if(mb_type&MB_TYPE_BACKWARD){ + if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){ s->mv_dir = MV_DIR_BACKWARD; s->mv_type = MV_TYPE_16X16; s->mb_intra= 0; s->mv[1][0][0] = s->b_back_mv_table[xy][0]; s->mv[1][0][1] = s->b_back_mv_table[xy][1]; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb, &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); } - if(mb_type&MB_TYPE_BIDIR){ + if(mb_type&CANDIDATE_MB_TYPE_BIDIR){ s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; s->mv_type = MV_TYPE_16X16; s->mb_intra= 0; @@ -4137,10 +4266,10 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb, &dmin, &next_block, 0, 0); } - if(mb_type&MB_TYPE_DIRECT){ + if(mb_type&CANDIDATE_MB_TYPE_DIRECT){ int mx= s->b_direct_mv_table[xy][0]; int my= s->b_direct_mv_table[xy][1]; @@ -4149,16 +4278,54 @@ static void encode_picture(MpegEncContext *s, int picture_number) #ifdef CONFIG_RISKY ff_mpeg4_set_direct_mv(s, mx, my); #endif - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb, &dmin, &next_block, mx, my); } - if(mb_type&MB_TYPE_INTRA){ + if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){ + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[0][i] = s->b_field_select_table[0][i][xy]; + s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0]; + s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1]; + } + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb, + &dmin, &next_block, 0, 0); + } + if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){ + s->mv_dir = MV_DIR_BACKWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[1][i] = s->b_field_select_table[1][i][xy]; + s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0]; + s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1]; + } + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb, + &dmin, &next_block, 0, 0); + } + if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){ + s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(dir=0; dir<2; dir++){ + for(i=0; i<2; i++){ + j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy]; + s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0]; + s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1]; + } + } + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb, + &dmin, &next_block, 0, 0); + } + if(mb_type&CANDIDATE_MB_TYPE_INTRA){ s->mv_dir = 0; s->mv_type = MV_TYPE_16X16; s->mb_intra= 1; s->mv[0][0][0] = 0; s->mv[0][0][1] = 0; - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb, &dmin, &next_block, 0, 0); if(s->h263_pred || s->h263_aic){ if(best_s.mb_intra) @@ -4200,7 +4367,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) } } - encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, + encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb, &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]); if(best_s.qscale != qp){ if(s->mb_intra){ @@ -4260,19 +4427,30 @@ static void encode_picture(MpegEncContext *s, int picture_number) // only one MB-Type possible switch(mb_type){ - case MB_TYPE_INTRA: + case CANDIDATE_MB_TYPE_INTRA: s->mv_dir = 0; s->mb_intra= 1; motion_x= s->mv[0][0][0] = 0; motion_y= s->mv[0][0][1] = 0; break; - case MB_TYPE_INTER: + case CANDIDATE_MB_TYPE_INTER: s->mv_dir = MV_DIR_FORWARD; s->mb_intra= 0; motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; break; - case MB_TYPE_INTER4V: + case CANDIDATE_MB_TYPE_INTER_I: + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[0][i] = s->p_field_select_table[i][xy]; + s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0]; + s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1]; + } + motion_x = motion_y = 0; + break; + case CANDIDATE_MB_TYPE_INTER4V: s->mv_dir = MV_DIR_FORWARD; s->mv_type = MV_TYPE_8X8; s->mb_intra= 0; @@ -4282,7 +4460,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) } motion_x= motion_y= 0; break; - case MB_TYPE_DIRECT: + case CANDIDATE_MB_TYPE_DIRECT: s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; s->mb_intra= 0; motion_x=s->b_direct_mv_table[xy][0]; @@ -4291,7 +4469,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) ff_mpeg4_set_direct_mv(s, motion_x, motion_y); #endif break; - case MB_TYPE_BIDIR: + case CANDIDATE_MB_TYPE_BIDIR: s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; s->mb_intra= 0; motion_x=0; @@ -4301,19 +4479,54 @@ static void encode_picture(MpegEncContext *s, int picture_number) s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; break; - case MB_TYPE_BACKWARD: + case CANDIDATE_MB_TYPE_BACKWARD: s->mv_dir = MV_DIR_BACKWARD; s->mb_intra= 0; motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; break; - case MB_TYPE_FORWARD: + case CANDIDATE_MB_TYPE_FORWARD: s->mv_dir = MV_DIR_FORWARD; s->mb_intra= 0; motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; // printf(" %d %d ", motion_x, motion_y); break; + case CANDIDATE_MB_TYPE_FORWARD_I: + s->mv_dir = MV_DIR_FORWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[0][i] = s->b_field_select_table[0][i][xy]; + s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0]; + s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1]; + } + motion_x=motion_y=0; + break; + case CANDIDATE_MB_TYPE_BACKWARD_I: + s->mv_dir = MV_DIR_BACKWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(i=0; i<2; i++){ + j= s->field_select[1][i] = s->b_field_select_table[1][i][xy]; + s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0]; + s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1]; + } + motion_x=motion_y=0; + break; + case CANDIDATE_MB_TYPE_BIDIR_I: + s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; + s->mv_type = MV_TYPE_FIELD; + s->mb_intra= 0; + for(dir=0; dir<2; dir++){ + for(i=0; i<2; i++){ + j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy]; + s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0]; + s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1]; + } + } + motion_x=motion_y=0; + break; default: motion_x=motion_y=0; //gcc warning fix av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n"); @@ -4384,7 +4597,7 @@ static void encode_picture(MpegEncContext *s, int picture_number) #endif //CONFIG_ENCODERS -void ff_denoise_dct(MpegEncContext *s, DCTELEM *block){ +static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){ const int intra= s->mb_intra; int i; @@ -4415,33 +4628,31 @@ static int dct_quantize_trellis_c(MpegEncContext *s, int qscale, int *overflow){ const int *qmat; const uint8_t *scantable= s->intra_scantable.scantable; + const uint8_t *perm_scantable= s->intra_scantable.permutated; int max=0; unsigned int threshold1, threshold2; int bias=0; int run_tab[65]; int level_tab[65]; int score_tab[65]; + int survivor[65]; + int survivor_count; int last_run=0; int last_level=0; int last_score= 0; - int last_i= 0; - int not_coded_score= 0; - int coeff[3][64]; + int last_i; + int coeff[2][64]; int coeff_count[64]; int qmul, qadd, start_i, last_non_zero, i, dc; const int esc_length= s->ac_esc_length; uint8_t * length; uint8_t * last_length; - int score_limit=0; - int left_limit= 0; const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); - const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra; s->dsp.fdct (block); if(s->dct_error_sum) - ff_denoise_dct(s, block); - + s->denoise_dct(s, block); qmul= qscale*16; qadd= ((qscale-1)|1)*8; @@ -4475,37 +4686,45 @@ static int dct_quantize_trellis_c(MpegEncContext *s, length = s->inter_ac_vlc_length; last_length= s->inter_ac_vlc_last_length; } + last_i= start_i; threshold1= (1<=start_i; i--) { const int j = scantable[i]; - const int k= i-start_i; - int level = block[j]; - level = level * qmat[j]; + int level = block[j] * qmat[j]; + + if(((unsigned)(level+threshold1))>threshold2){ + last_non_zero = i; + break; + } + } + + for(i=start_i; i<=last_non_zero; i++) { + const int j = scantable[i]; + int level = block[j] * qmat[j]; // if( bias+level >= (1<<(QMAT_SHIFT - 3)) // || bias-level >= (1<<(QMAT_SHIFT - 3))){ if(((unsigned)(level+threshold1))>threshold2){ if(level>0){ level= (bias + level)>>QMAT_SHIFT; - coeff[0][k]= level; - coeff[1][k]= level-1; + coeff[0][i]= level; + coeff[1][i]= level-1; // coeff[2][k]= level-2; }else{ level= (bias - level)>>QMAT_SHIFT; - coeff[0][k]= -level; - coeff[1][k]= -level+1; + coeff[0][i]= -level; + coeff[1][i]= -level+1; // coeff[2][k]= -level+2; } - coeff_count[k]= FFMIN(level, 2); - assert(coeff_count[k]); + coeff_count[i]= FFMIN(level, 2); + assert(coeff_count[i]); max |=level; - last_non_zero = i; }else{ - coeff[0][k]= (level>>31)|1; - coeff_count[k]= 1; + coeff[0][i]= (level>>31)|1; + coeff_count[i]= 1; } } @@ -4516,73 +4735,55 @@ static int dct_quantize_trellis_c(MpegEncContext *s, return last_non_zero; } - score_tab[0]= 0; + score_tab[start_i]= 0; + survivor[0]= start_i; + survivor_count= 1; - if(patch_table){ -// length[UNI_AC_ENC_INDEX(0, 63)]= -// length[UNI_AC_ENC_INDEX(0, 65)]= 2; - } - - for(i=0; i<=last_non_zero - start_i; i++){ - int level_index, run, j; - const int dct_coeff= block[ scantable[i + start_i] ]; + for(i=start_i; i<=last_non_zero; i++){ + int level_index, j; + const int dct_coeff= ABS(block[ scantable[i] ]); const int zero_distoration= dct_coeff*dct_coeff; int best_score=256*256*256*120; - - last_score += zero_distoration; - not_coded_score += zero_distoration; for(level_index=0; level_index < coeff_count[i]; level_index++){ int distoration; int level= coeff[level_index][i]; + const int alevel= ABS(level); int unquant_coeff; assert(level); if(s->out_format == FMT_H263){ - if(level>0){ - unquant_coeff= level*qmul + qadd; - }else{ - unquant_coeff= level*qmul - qadd; - } + unquant_coeff= alevel*qmul + qadd; }else{ //MPEG1 - j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize + j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize if(s->mb_intra){ - if (level < 0) { - unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3; - unquant_coeff = -((unquant_coeff - 1) | 1); - } else { - unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3; + unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3; unquant_coeff = (unquant_coeff - 1) | 1; - } }else{ - if (level < 0) { - unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; - unquant_coeff = -((unquant_coeff - 1) | 1); - } else { - unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; + unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; unquant_coeff = (unquant_coeff - 1) | 1; - } } unquant_coeff<<= 3; } - distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff); + distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration; level+=64; if((level&(~127)) == 0){ - for(run=0; run<=i - left_limit; run++){ + for(j=survivor_count-1; j>=0; j--){ + int run= i - survivor[j]; int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda; score += score_tab[i-run]; if(score < best_score){ - best_score= - score_tab[i+1]= score; + best_score= score; run_tab[i+1]= run; level_tab[i+1]= level-64; } } if(s->out_format == FMT_H263){ - for(run=0; run<=i - left_limit; run++){ + for(j=survivor_count-1; j>=0; j--){ + int run= i - survivor[j]; int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda; score += score_tab[i-run]; if(score < last_score){ @@ -4595,19 +4796,20 @@ static int dct_quantize_trellis_c(MpegEncContext *s, } }else{ distoration += esc_length*lambda; - for(run=0; run<=i - left_limit; run++){ + for(j=survivor_count-1; j>=0; j--){ + int run= i - survivor[j]; int score= distoration + score_tab[i-run]; if(score < best_score){ - best_score= - score_tab[i+1]= score; + best_score= score; run_tab[i+1]= run; level_tab[i+1]= level-64; } } if(s->out_format == FMT_H263){ - for(run=0; run<=i - left_limit; run++){ + for(j=survivor_count-1; j>=0; j--){ + int run= i - survivor[j]; int score= distoration + score_tab[i-run]; if(score < last_score){ last_score= score; @@ -4619,26 +4821,28 @@ static int dct_quantize_trellis_c(MpegEncContext *s, } } } - - for(j=left_limit; j<=i; j++){ - score_tab[j] += zero_distoration; - } - score_limit+= zero_distoration; - if(score_tab[i+1] < score_limit) - score_limit= score_tab[i+1]; + score_tab[i+1]= best_score; + //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level - while(score_tab[ left_limit ] > score_limit + lambda) left_limit++; - - if(patch_table){ -// length[UNI_AC_ENC_INDEX(0, 63)]= -// length[UNI_AC_ENC_INDEX(0, 65)]= 3; + if(last_non_zero <= 27){ + for(; survivor_count; survivor_count--){ + if(score_tab[ survivor[survivor_count-1] ] <= best_score) + break; + } + }else{ + for(; survivor_count; survivor_count--){ + if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda) + break; + } } + + survivor[ survivor_count++ ]= i+1; } if(s->out_format != FMT_H263){ last_score= 256*256*256*120; - for(i= left_limit; i<=last_non_zero - start_i + 1; i++){ + for(i= survivor[0]; i<=last_non_zero + 1; i++){ int score= score_tab[i]; if(i) score += lambda*2; //FIXME exacter? @@ -4651,10 +4855,10 @@ static int dct_quantize_trellis_c(MpegEncContext *s, } } - s->coded_score[n] = last_score - not_coded_score; + s->coded_score[n] = last_score; - dc= block[0]; - last_non_zero= last_i - 1 + start_i; + dc= ABS(block[0]); + last_non_zero= last_i - 1; memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); if(last_non_zero < start_i) @@ -4666,32 +4870,22 @@ static int dct_quantize_trellis_c(MpegEncContext *s, for(i=0; iout_format == FMT_H263){ - if(level>0){ - unquant_coeff= (level*qmul + qadd)>>3; - }else{ - unquant_coeff= (level*qmul - qadd)>>3; - } + unquant_coeff= (alevel*qmul + qadd)>>3; }else{ //MPEG1 - if (level < 0) { - unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4; - unquant_coeff = -((unquant_coeff - 1) | 1); - } else { - unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4; - unquant_coeff = (unquant_coeff - 1) | 1; - } + unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4; + unquant_coeff = (unquant_coeff - 1) | 1; } unquant_coeff = (unquant_coeff + 4) >> 3; unquant_coeff<<= 3 + 3; - distoration= (unquant_coeff - dc) * (unquant_coeff - dc); + distortion= (unquant_coeff - dc) * (unquant_coeff - dc); level+=64; - if((level&(~127)) == 0) - score= distoration + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda; - else - score= distoration + esc_length*lambda; + if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda; + else score= distortion + esc_length*lambda; if(score < best_score){ best_score= score; @@ -4706,15 +4900,12 @@ static int dct_quantize_trellis_c(MpegEncContext *s, i= last_i; assert(last_level); -//FIXME use permutated scantable - block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level; + + block[ perm_scantable[last_non_zero] ]= last_level; i -= last_run + 1; - for(;i>0 ; i -= run_tab[i] + 1){ - const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ]; - - block[j]= level_tab[i]; - assert(block[j]); + for(; i>start_i; i -= run_tab[i] + 1){ + block[ perm_scantable[i-1] ]= level_tab[i]; } return last_non_zero; @@ -4724,7 +4915,7 @@ static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow) { - int i, j, level, last_non_zero, q; + int i, j, level, last_non_zero, q, start_i; const int *qmat; const uint8_t *scantable= s->intra_scantable.scantable; int bias; @@ -4734,7 +4925,7 @@ static int dct_quantize_c(MpegEncContext *s, s->dsp.fdct (block); if(s->dct_error_sum) - ff_denoise_dct(s, block); + s->denoise_dct(s, block); if (s->mb_intra) { if (!s->h263_aic) { @@ -4749,23 +4940,32 @@ static int dct_quantize_c(MpegEncContext *s, /* note: block[0] is assumed to be positive */ block[0] = (block[0] + (q >> 1)) / q; - i = 1; + start_i = 1; last_non_zero = 0; qmat = s->q_intra_matrix[qscale]; bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); } else { - i = 0; + start_i = 0; last_non_zero = -1; qmat = s->q_inter_matrix[qscale]; bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); } threshold1= (1<=start_i;i--) { j = scantable[i]; - level = block[j]; - level = level * qmat[j]; + level = block[j] * qmat[j]; + + if(((unsigned)(level+threshold1))>threshold2){ + last_non_zero = i; + break; + }else{ + block[j]=0; + } + } + for(i=start_i; i<=last_non_zero; i++) { + j = scantable[i]; + level = block[j] * qmat[j]; // if( bias+level >= (1<= (1<display_picture_number, shouldnt be used for/by anything else + int coded_picture_number; ///< used to set pic->coded_picture_number, shouldnt be used for/by anything else + int picture_number; //FIXME remove, unclear definition int picture_in_gop_number; ///< 0-> first pic in gop, ... int b_frames_since_non_b; ///< used for encoding, relative to not yet reordered input int mb_width, mb_height; ///< number of MBs horizontally & vertically @@ -303,6 +312,7 @@ typedef struct MpegEncContext { Picture *last_picture_ptr; ///< pointer to the previous picture. Picture *next_picture_ptr; ///< pointer to the next picture (for bidir pred) Picture *current_picture_ptr; ///< pointer to the current picture + uint8_t *visualization_buffer[3]; //< temporary buffer vor MV visualization int last_dc[3]; ///< last DC values for MPEG1 int16_t *dc_val[3]; ///< used for mpeg4 DC prediction, all 3 arrays must be continuous int16_t dc_cache[4*5]; @@ -349,12 +359,18 @@ typedef struct MpegEncContext { int16_t (*b_bidir_forw_mv_table_base)[2]; int16_t (*b_bidir_back_mv_table_base)[2]; int16_t (*b_direct_mv_table_base)[2]; + int16_t (*p_field_mv_table_base[2][2])[2]; + int16_t (*b_field_mv_table_base[2][2][2])[2]; int16_t (*p_mv_table)[2]; ///< MV table (1MV per MB) p-frame encoding int16_t (*b_forw_mv_table)[2]; ///< MV table (1MV per MB) forward mode b-frame encoding int16_t (*b_back_mv_table)[2]; ///< MV table (1MV per MB) backward mode b-frame encoding int16_t (*b_bidir_forw_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding int16_t (*b_bidir_back_mv_table)[2]; ///< MV table (1MV per MB) bidir mode b-frame encoding int16_t (*b_direct_mv_table)[2]; ///< MV table (1MV per MB) direct mode b-frame encoding + int16_t (*p_field_mv_table[2][2])[2]; ///< MV table (2MV per MB) interlaced p-frame encoding + int16_t (*b_field_mv_table[2][2][2])[2];///< MV table (4MV per MB) interlaced b-frame encoding + uint8_t (*p_field_select_table[2]); + uint8_t (*b_field_select_table[2][2]); int me_method; ///< ME algorithm int scene_change_score; int mv_dir; @@ -389,17 +405,22 @@ typedef struct MpegEncContext { int mb_x, mb_y; int mb_skip_run; int mb_intra; - uint8_t *mb_type; ///< Table for MB type FIXME remove and use picture->mb_type -#define MB_TYPE_INTRA 0x01 -#define MB_TYPE_INTER 0x02 -#define MB_TYPE_INTER4V 0x04 -#define MB_TYPE_SKIPED 0x08 + uint16_t *mb_type; ///< Table for candidate MB types for encoding +#define CANDIDATE_MB_TYPE_INTRA 0x01 +#define CANDIDATE_MB_TYPE_INTER 0x02 +#define CANDIDATE_MB_TYPE_INTER4V 0x04 +#define CANDIDATE_MB_TYPE_SKIPED 0x08 //#define MB_TYPE_GMC 0x10 -#define MB_TYPE_DIRECT 0x10 -#define MB_TYPE_FORWARD 0x20 -#define MB_TYPE_BACKWARD 0x40 -#define MB_TYPE_BIDIR 0x80 +#define CANDIDATE_MB_TYPE_DIRECT 0x10 +#define CANDIDATE_MB_TYPE_FORWARD 0x20 +#define CANDIDATE_MB_TYPE_BACKWARD 0x40 +#define CANDIDATE_MB_TYPE_BIDIR 0x80 + +#define CANDIDATE_MB_TYPE_INTER_I 0x100 +#define CANDIDATE_MB_TYPE_FORWARD_I 0x200 +#define CANDIDATE_MB_TYPE_BACKWARD_I 0x400 +#define CANDIDATE_MB_TYPE_BIDIR_I 0x800 int block_index[6]; ///< index to current MB in block based arrays with edges int block_wrap[6]; @@ -449,7 +470,6 @@ typedef struct MpegEncContext { void *opaque; ///< private data for the user /* bit rate control */ - int I_frame_bits; //FIXME used in mpeg12 ... int64_t wanted_bits; int64_t total_bits; int frame_bits; ///< bits used for the current frame @@ -549,8 +569,6 @@ typedef struct MpegEncContext { uint8_t *tex_pb_buffer; uint8_t *pb2_buffer; int mpeg_quant; - int16_t (*field_mv_table)[2][2]; ///< used for interlaced b frame decoding - int8_t (*field_select_table)[2]; ///< wtf, no really another table for interlaced b frames int t_frame; ///< time distance of first I -> B, used for interlaced b frames int padding_bug_score; ///< used to detect the VERY common padding bug in MPEG4 @@ -601,10 +619,10 @@ typedef struct MpegEncContext { GetBitContext gb; /* Mpeg1 specific */ - int fake_picture_number; ///< picture number at the bitstream frame rate int gop_picture_number; ///< index of the first picture of a GOP based on fake_pic_num & mpeg1 specific int last_mv_dir; ///< last mv_dir, used for b frame encoding int broken_link; ///< no_output_of_prior_pics_flag + uint8_t *vbv_delay_ptr; ///< pointer to vbv_delay in the bitstream /* MPEG2 specific - I wish I had not to support this mess. */ int progressive_sequence; @@ -663,6 +681,7 @@ typedef struct MpegEncContext { DCTELEM *block/*align 16*/, int n, int qscale); int (*dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow); int (*fast_dct_quantize)(struct MpegEncContext *s, DCTELEM *block/*align 16*/, int n, int qscale, int *overflow); + void (*denoise_dct)(struct MpegEncContext *s, DCTELEM *block); } MpegEncContext; @@ -703,7 +722,7 @@ void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, #define END_NOT_FOUND -100 int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size); void ff_mpeg_flush(AVCodecContext *avctx); -void ff_print_debug_info(MpegEncContext *s, Picture *pict); +void ff_print_debug_info(MpegEncContext *s, AVFrame *pict); void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix); int ff_find_unused_picture(MpegEncContext *s, int shared); void ff_denoise_dct(MpegEncContext *s, DCTELEM *block); @@ -745,7 +764,8 @@ void ff_estimate_b_frame_motion(MpegEncContext * s, int mb_x, int mb_y); int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type); void ff_fix_long_p_mvs(MpegEncContext * s); -void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type); +void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, + int16_t (*mv_table)[2], int f_code, int type, int truncate); void ff_init_me(MpegEncContext *s); int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y); diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c index 038316b67c..b7b88c38f8 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msmpeg4.c @@ -1,6 +1,7 @@ /* * MSMPEG4 backend for ffmpeg encoder and decoder * Copyright (c) 2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -730,6 +731,27 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, necessitate to modify mpegvideo.c. The problem comes from the fact they decided to store the quantized DC (which would lead to problems if Q could vary !) */ +#if defined ARCH_X86 && !defined PIC + asm volatile( + "movl %3, %%eax \n\t" + "shrl $1, %%eax \n\t" + "addl %%eax, %2 \n\t" + "addl %%eax, %1 \n\t" + "addl %0, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %0 \n\t" + "movl %1, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %1 \n\t" + "movl %2, %%eax \n\t" + "mull %4 \n\t" + "movl %%edx, %2 \n\t" + : "+b" (a), "+c" (b), "+D" (c) + : "g" (scale), "S" (inverse[scale]) + : "%eax", "%edx" + ); +#else + /* #elif defined (ARCH_ALPHA) */ /* Divisions are extremely costly on Alpha; optimize the most common case. But they are costly everywhere... */ @@ -742,6 +764,7 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n, b = FASTDIV((b + (scale >> 1)), scale); c = FASTDIV((c + (scale >> 1)), scale); } +#endif /* XXX: WARNING: they did not choose the same test as MPEG4. This is very important ! */ if(s->msmpeg4_version>3){ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c b/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c index 76649894aa..b318faa77f 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msrle.c @@ -110,8 +110,8 @@ static void msrle_decode_pal4(MsrleContext *s) FETCH_NEXT_STREAM_BYTE(); s->frame.data[0][row_ptr + pixel_ptr] = stream_byte >> 4; pixel_ptr++; - if (i + 1 == rle_code && odd_pixel) - break; + if (i + 1 == rle_code && odd_pixel) + break; if (pixel_ptr >= s->avctx->width) break; s->frame.data[0][row_ptr + pixel_ptr] = stream_byte & 0x0F; @@ -254,9 +254,9 @@ static int msrle_decode_frame(AVCodecContext *avctx, { MsrleContext *s = (MsrleContext *)avctx->priv_data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; + /* no supplementary picture */ + if (buf_size == 0) + return 0; s->buf = buf; s->size = buf_size; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c b/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c index 789d7e798b..3190efb9ef 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/msvideo1.c @@ -303,15 +303,15 @@ static int msvideo1_decode_frame(AVCodecContext *avctx, { Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; + /* no supplementary picture */ + if (buf_size == 0) + return 0; s->buf = buf; s->size = buf_size; - s->frame.reference = 1; - s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + s->frame.reference = 1; + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; if (avctx->reget_buffer(avctx, &s->frame)) { av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return -1; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/ratecontrol.c b/src/add-ons/media/plugins/avcodec/libavcodec/ratecontrol.c index 791acd8f0f..6c90b1b6cd 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/ratecontrol.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/ratecontrol.c @@ -1,7 +1,7 @@ /* * Rate control for video encoders * - * Copyright (c) 2002-2003 Michael Niedermayer + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -39,7 +39,7 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, double rate_f void ff_write_pass1_stats(MpegEncContext *s){ sprintf(s->avctx->stats_out, "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d mc-var:%d var:%d icount:%d;\n", - s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type, + s->current_picture_ptr->display_picture_number, s->current_picture_ptr->coded_picture_number, s->pict_type, s->current_picture.quality, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, s->f_code, s->b_code, s->current_picture.mc_mb_var_sum, s->current_picture.mb_var_sum, s->i_count); } @@ -198,11 +198,11 @@ static inline double bits2qp(RateControlEntry *rce, double bits){ int ff_vbv_update(MpegEncContext *s, int frame_size){ RateControlContext *rcc= &s->rc_context; const double fps= (double)s->avctx->frame_rate / (double)s->avctx->frame_rate_base; - const double buffer_size= s->avctx->rc_buffer_size; + const int buffer_size= s->avctx->rc_buffer_size; const double min_rate= s->avctx->rc_min_rate/fps; const double max_rate= s->avctx->rc_max_rate/fps; - -//printf("%f %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate); + +//printf("%d %f %d %f %f\n", buffer_size, rcc->buffer_index, frame_size, min_rate, max_rate); if(buffer_size){ int left; @@ -215,8 +215,8 @@ int ff_vbv_update(MpegEncContext *s, int frame_size){ left= buffer_size - rcc->buffer_index - 1; rcc->buffer_index += clip(left, min_rate, max_rate); - if(rcc->buffer_index > s->avctx->rc_buffer_size){ - int stuffing= ceil((rcc->buffer_index - s->avctx->rc_buffer_size)/8); + if(rcc->buffer_index > buffer_size){ + int stuffing= ceil((rcc->buffer_index - buffer_size)/8); if(stuffing < 4 && s->codec_id == CODEC_ID_MPEG4) stuffing=4; @@ -413,6 +413,7 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, /* buffer overflow/underflow protection */ if(buffer_size){ double expected_size= rcc->buffer_index; + double q_limit; if(min_rate){ double d= 2*(buffer_size - expected_size)/buffer_size; @@ -420,7 +421,13 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, else if(d<0.0001) d=0.0001; q*= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); - q= FFMIN(q, bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1))); + q_limit= bits2qp(rce, FFMAX((min_rate - buffer_size + rcc->buffer_index)*3, 1)); + if(q > q_limit){ + if(s->avctx->debug&FF_DEBUG_RC){ + av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); + } + q= q_limit; + } } if(max_rate){ @@ -429,7 +436,13 @@ static double modify_qscale(MpegEncContext *s, RateControlEntry *rce, double q, else if(d<0.0001) d=0.0001; q/= pow(d, 1.0/s->avctx->rc_buffer_aggressivity); - q= FFMAX(q, bits2qp(rce, FFMAX(rcc->buffer_index/3, 1))); + q_limit= bits2qp(rce, FFMAX(rcc->buffer_index/3, 1)); + if(q < q_limit){ + if(s->avctx->debug&FF_DEBUG_RC){ + av_log(s->avctx, AV_LOG_DEBUG, "limiting QP %f -> %f\n", q, q_limit); + } + q= q_limit; + } } } //printf("q:%f max:%f min:%f size:%f index:%d bits:%f agr:%f\n", q,max_rate, min_rate, buffer_size, rcc->buffer_index, bits, s->avctx->rc_buffer_aggressivity); @@ -507,7 +520,7 @@ static void adaptive_quantization(MpegEncContext *s, double q){ if(spat_cplx < 4) spat_cplx= 4; //FIXME finetune if(temp_cplx < 4) temp_cplx= 4; //FIXME finetune - if((s->mb_type[mb_xy]&MB_TYPE_INTRA)){//FIXME hq mode + if((s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTRA)){//FIXME hq mode cplx= spat_cplx; factor= 1.0 + p_masking; }else{ diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/rpza.c b/src/add-ons/media/plugins/avcodec/libavcodec/rpza.c index b7c09f95db..965fb729b0 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/rpza.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/rpza.c @@ -254,15 +254,15 @@ static int rpza_decode_frame(AVCodecContext *avctx, { RpzaContext *s = (RpzaContext *)avctx->priv_data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; + /* no supplementary picture */ + if (buf_size == 0) + return 0; s->buf = buf; s->size = buf_size; s->frame.reference = 1; - s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; if (avctx->reget_buffer(avctx, &s->frame)) { av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); return -1; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/rv10.c b/src/add-ons/media/plugins/avcodec/libavcodec/rv10.c index 08d59cf680..3b63464653 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/rv10.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/rv10.c @@ -1,6 +1,7 @@ /* * RV10 codec * Copyright (c) 2000,2001 Fabrice Bellard. + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -378,12 +379,17 @@ static int rv20_decode_picture_header(MpegEncContext *s) if(s->avctx->has_b_frames){ if (get_bits(&s->gb, 1)){ - av_log(s->avctx, AV_LOG_ERROR, "unknown bit3 set\n"); - return -1; +// av_log(s->avctx, AV_LOG_ERROR, "unknown bit3 set\n"); +// return -1; } seq= get_bits(&s->gb, 15); - }else + mb_pos= get_bits(&s->gb, av_log2(s->mb_num-1)+1); + s->mb_x= mb_pos % s->mb_width; + s->mb_y= mb_pos / s->mb_width; + }else{ seq= get_bits(&s->gb, 8)*128; + mb_pos= ff_h263_decode_mba(s); + } //printf("%d\n", seq); seq |= s->time &~0x7FFF; if(seq - s->time > 0x4000) seq -= 0x8000; @@ -404,7 +410,6 @@ static int rv20_decode_picture_header(MpegEncContext *s) } // printf("%d %d %d %d %d\n", seq, (int)s->time, (int)s->last_non_b_time, s->pp_time, s->pb_time); - mb_pos= ff_h263_decode_mba(s); s->no_rounding= get_bits1(&s->gb); s->f_code = 1; @@ -469,8 +474,6 @@ static int rv10_decode_init(AVCodecContext *avctx) av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", avctx->sub_id); } //printf("ver:%X\n", avctx->sub_id); - s->flags= avctx->flags; - if (MPV_common_init(s) < 0) return -1; @@ -537,11 +540,15 @@ static int rv10_decode_packet(AVCodecContext *avctx, } //if(s->pict_type == P_TYPE) return 0; - if (s->mb_x == 0 && s->mb_y == 0) { + if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) { if(MPV_frame_start(s, avctx) < 0) return -1; } + if(s->pict_type == B_TYPE){ //FIXME remove after cleaning mottion_val indexing + memset(s->current_picture.motion_val[0], 0, sizeof(int16_t)*2*(s->mb_width*2+2)*(s->mb_height*2+2)); + } + #ifdef DEBUG printf("qscale=%d\n", s->qscale); #endif @@ -570,7 +577,7 @@ static int rv10_decode_packet(AVCodecContext *avctx, s->rv10_first_dc_coded[0] = 0; s->rv10_first_dc_coded[1] = 0; s->rv10_first_dc_coded[2] = 0; - +//printf("%d %X %X\n", s->pict_type, s->current_picture.motion_val[0], s->current_picture.motion_val[1]); s->block_wrap[0]= s->block_wrap[1]= s->block_wrap[2]= @@ -595,7 +602,8 @@ static int rv10_decode_packet(AVCodecContext *avctx, av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x, s->mb_y); return -1; } - ff_h263_update_motion_val(s); + if(s->pict_type != B_TYPE) + ff_h263_update_motion_val(s); MPV_decode_mb(s, s->block); if(s->loop_filter) ff_h263_loop_filter(s); @@ -630,7 +638,7 @@ static int rv10_decode_frame(AVCodecContext *avctx, *data_size = 0; return 0; } - + if(avctx->slice_count){ for(i=0; islice_count; i++){ int offset= avctx->slice_offset[i]; @@ -648,16 +656,20 @@ static int rv10_decode_frame(AVCodecContext *avctx, if( rv10_decode_packet(avctx, buf, buf_size) < 0 ) return -1; } + + if(s->pict_type == B_TYPE){ //FIXME remove after cleaning mottion_val indexing + memset(s->current_picture.motion_val[0], 0, sizeof(int16_t)*2*(s->mb_width*2+2)*(s->mb_height*2+2)); + } if(s->mb_y>=s->mb_height){ MPV_frame_end(s); if(s->pict_type==B_TYPE || s->low_delay){ *pict= *(AVFrame*)&s->current_picture; - ff_print_debug_info(s, s->current_picture_ptr); + ff_print_debug_info(s, pict); } else { *pict= *(AVFrame*)&s->last_picture; - ff_print_debug_info(s, s->last_picture_ptr); + ff_print_debug_info(s, pict); } *data_size = sizeof(AVFrame); diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/smc.c b/src/add-ons/media/plugins/avcodec/libavcodec/smc.c index 24be54dbf1..be02b162d8 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/smc.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/smc.c @@ -453,16 +453,16 @@ static int smc_decode_frame(AVCodecContext *avctx, { SmcContext *s = (SmcContext *)avctx->priv_data; - /* no supplementary picture */ - if (buf_size == 0) - return 0; + /* no supplementary picture */ + if (buf_size == 0) + return 0; s->buf = buf; s->size = buf_size; s->frame.reference = 1; - s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | - FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE; + s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | + FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE; if (avctx->reget_buffer(avctx, &s->frame)) { printf ("reget_buffer() failed\n"); return -1; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/svq1.c b/src/add-ons/media/plugins/avcodec/libavcodec/svq1.c index 9881ab5f09..6a15270b76 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/svq1.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/svq1.c @@ -579,7 +579,7 @@ static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) { seed = string_table[out[0]]; - for (i=1; i < out[0]; i++) { + for (i=1; i <= out[0]; i++) { out[i] = get_bits (bitbuf, 8) ^ seed; seed = string_table[out[i] ^ seed]; } @@ -789,7 +789,6 @@ static int svq1_decode_init(AVCodecContext *avctx) s->codec_id= avctx->codec->id; avctx->pix_fmt = PIX_FMT_YUV410P; avctx->has_b_frames= 1; // not true, but DP frames and these behave like unidirectional b frames - s->flags= avctx->flags; if (MPV_common_init(s) < 0) return -1; init_vlc(&svq1_block_type, 2, 4, diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/svq3.c b/src/add-ons/media/plugins/avcodec/libavcodec/svq3.c index e5d7bb644b..c8720c07aa 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/svq3.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/svq3.c @@ -697,8 +697,10 @@ static int svq3_decode_slice_header (H264Context *h) { h->next_slice_index = s->gb.index + 8*show_bits (&s->gb, 8*length) + 8*length; - if (h->next_slice_index > s->gb.size_in_bits) + if (h->next_slice_index > s->gb.size_in_bits){ + av_log(h->s.avctx, AV_LOG_ERROR, "slice after bitstream end\n"); return -1; + } s->gb.size_in_bits = h->next_slice_index - 8*(length - 1); s->gb.index += 8; @@ -709,8 +711,10 @@ static int svq3_decode_slice_header (H264Context *h) { } } - if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3) + if ((i = svq3_get_ue_golomb (&s->gb)) == INVALID_VLC || i >= 3){ + av_log(h->s.avctx, AV_LOG_ERROR, "illegal slice type %d \n", i); return -1; + } h->slice_type = golomb_to_pict_type[i]; @@ -766,7 +770,9 @@ static int svq3_decode_frame (AVCodecContext *avctx, *data_size = 0; s->flags = avctx->flags; - + s->flags2 = avctx->flags2; + s->unrestricted_mv = 1; + if (!s->context_initialized) { s->width = avctx->width; s->height = avctx->height; diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/utils.c b/src/add-ons/media/plugins/avcodec/libavcodec/utils.c index 1f95c55290..0f123b7b32 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/utils.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/utils.c @@ -2,6 +2,7 @@ * utils for libavcodec * Copyright (c) 2001 Fabrice Bellard. * Copyright (c) 2003 Michel Bardiaux for the av_log API + * Copyright (c) 2002-2004 Michael Niedermayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -354,6 +355,7 @@ void avcodec_get_context_defaults(AVCodecContext *s){ s->lmin= FF_QP2LAMBDA * s->qmin; s->lmax= FF_QP2LAMBDA * s->qmax; s->sample_aspect_ratio= (AVRational){0,1}; + s->ildct_cmp= FF_CMP_VSAD; s->intra_quant_bias= FF_DEFAULT_QUANT_BIAS; s->inter_quant_bias= FF_DEFAULT_QUANT_BIAS; @@ -818,9 +820,9 @@ static void av_log_default_callback(AVCodecContext* avctx, int level, const char if(level>av_log_level) return; if(avctx && print_prefix) - fprintf(stderr, "[%s @ %p]", avctx->codec->name, avctx); + fprintf(stderr, "[%s @ %p]", avctx->codec ? avctx->codec->name : "?", avctx); - print_prefix= (int)strstr(fmt, "\n"); + print_prefix= strstr(fmt, "\n") != NULL; vfprintf(stderr, fmt, vl); } diff --git a/src/add-ons/media/plugins/avcodec/libavcodec/wmadec.c b/src/add-ons/media/plugins/avcodec/libavcodec/wmadec.c index 15b15f23f0..25498c4d28 100644 --- a/src/add-ons/media/plugins/avcodec/libavcodec/wmadec.c +++ b/src/add-ons/media/plugins/avcodec/libavcodec/wmadec.c @@ -1187,6 +1187,11 @@ static int wma_decode_superframe(AVCodecContext *avctx, tprintf("***decode_superframe:\n"); + if(buf_size==0){ + s->last_superframe_len = 0; + return 0; + } + samples = data; init_get_bits(&s->gb, buf, buf_size*8);