changeset 810:4174d2783be5 trunk

[svn] - delete a lot of stuff
author nenolod
date Mon, 12 Mar 2007 11:37:35 -0700
parents 29ae10725c4c
children 3afe5701df41
files ChangeLog src/ffmpeg/libavcodec/Makefile src/ffmpeg/libavcodec/a52dec.c src/ffmpeg/libavcodec/ac3dec.c src/ffmpeg/libavcodec/amr.c src/ffmpeg/libavcodec/faad.c src/ffmpeg/libavcodec/jpeg_ls.c src/ffmpeg/libavcodec/libgsm.c src/ffmpeg/libavcodec/mdec.c src/ffmpeg/libavcodec/motion_est.c src/ffmpeg/libavcodec/motion_est_template.c src/ffmpeg/libavcodec/mp3lameaudio.c src/ffmpeg/libavcodec/mpeg12.c src/ffmpeg/libavcodec/msmpeg4.c src/ffmpeg/libavcodec/wmv2.c src/ffmpeg/libavcodec/x264.c src/ffmpeg/libavcodec/xvid_internal.h src/ffmpeg/libavcodec/xvid_rc.c src/ffmpeg/libavcodec/xvidff.c
diffstat 19 files changed, 28 insertions(+), 13467 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Mar 12 11:31:14 2007 -0700
+++ b/ChangeLog	Mon Mar 12 11:37:35 2007 -0700
@@ -1,3 +1,12 @@
+2007-03-12 18:31:14 +0000  Yoshiki Yazawa <yaz@cc.rim.or.jp>
+  revision [1718]
+  - make use of new millisecond seek API.
+  - watchdog_func has been implemented as a thread. this thread will wake up every 10msec and will check changes of track while playing.
+  
+  trunk/src/cue/cuesheet.c |  236 ++++++++++++++++++++++++++++++++++++-----------
+  1 file changed, 181 insertions(+), 55 deletions(-)
+
+
 2007-03-12 18:18:54 +0000  William Pitcock <nenolod@sacredspiral.co.uk>
   revision [1716]
   - add ffmpeg
--- a/src/ffmpeg/libavcodec/Makefile	Mon Mar 12 11:31:14 2007 -0700
+++ b/src/ffmpeg/libavcodec/Makefile	Mon Mar 12 11:37:35 2007 -0700
@@ -6,60 +6,59 @@
 SOURCES = \
 4xm.c                     h261.c                    raw.c		\
 8bps.c                    h263.c                    resample.c	\
-a52dec.c                  h263dec.c                 resample2.c	\
+h263dec.c                 resample2.c	\
 aasc.c                    h264.c                    roqvideo.c	\
-ac3dec.c                  h264idct.c                rpza.c	\
+h264idct.c                rpza.c	\
 ac3enc.c                  huffyuv.c                 rtjpeg.c	\
 adpcm.c                   idcinvideo.c              rv10.c	\
 adx.c                     imgconvert.c              shorten.c	\
 alac.c                    imgresample.c             simple_idct.c \
 allcodecs.c               indeo2.c                  smacker.c	\
-amr.c                     indeo3.c                  smc.c	\
+indeo3.c                  smc.c	\
 apiexample.c              interplayvideo.c          snow.c	\
 asv1.c                    jfdctfst.c                sonic.c	\
 audioconvert.c            jfdctint.c                svq1.c	\
-avs.c                     jpeg_ls.c                 svq3.c	\
+avs.c                     \
 jrevdct.c                 targa.c	\
 bitstream.c               kmvc.c                    tiertexseqv.c \
 bitstream_filter.c        lcl.c                     tiff.c	\
-bmp.c                     libgsm.c                  truemotion1.c	\
+bmp.c                     truemotion1.c	\
 cabac.c                   loco.c                    truemotion2.c	\
 cavs.c                    lzo.c                     truespeech.c	\
 cavsdsp.c                 mace.c                    tscc.c	\
 cinepak.c                 mdct.c                    tta.c	\
-cljr.c                    mdec.c                    ulti.c	\
-cook.c                    mjpeg.c                   utils.c	\
+cljr.c                    ulti.c	\
+cook.c                    utils.c	\
 cscd.c                    mmvideo.c                 vc1.c	\
-cyuv.c                    motion_est.c              vc1dsp.c	\
-dct-test.c                motion_est_template.c     vcr1.c	\
-dpcm.c                    motion_test.c             vmdav.c	\
-dsicinav.c                mp3lameaudio.c            vmnc.c	\
-dsputil.c                 mpeg12.c                  vorbis.c	\
-dtsdec.c                  mpegaudio.c               vorbis_data.c \
+cyuv.c                    vc1dsp.c	\
+dct-test.c                vcr1.c	\
+dpcm.c                    vmdav.c	\
+dsicinav.c                vmnc.c	\
+dsputil.c                 vorbis.c	\
+mpegaudio.c               vorbis_data.c \
 dv.c                      mpegaudiodec.c            vorbis_enc.c \
 dvbsub.c                  mpegvideo.c               vp3.c	\
-dvbsubdec.c               msmpeg4.c                 vp3dsp.c	\
+dvbsubdec.c               vp3dsp.c	\
 dvdsub.c                  msrle.c                   vp5.c	\
 dvdsubenc.c               msvideo1.c                vp56.c	\
 error_resilience.c        nuv.c                     vp56data.c	\
 eval.c                    oggvorbis.c               vp6.c	\
 faac.c                    opt.c                     vqavideo.c	\
-faad.c                    \
 faandct.c                 parser.c                  wavpack.c	\
 fdctref.c                 pcm.c                     wmadec.c	\
-fft-test.c                png.c                     wmv2.c	\
+fft-test.c                png.c                     \
 fft.c                     pnm.c                     wnv1.c	\
 ffv1.c                    pthread.c                 ws-snd1.c	\
-flac.c                    qdm2.c                    x264.c	\
+flac.c                    qdm2.c                    \
 flacenc.c                 qdrw.c                    xan.c	\
 flashsv.c                 qpeg.c                    xl.c	\
-flicvideo.c               qtrle.c                   xvid_rc.c	\
-fraps.c                   ra144.c                   xvidff.c	\
+flicvideo.c               qtrle.c                   \
+fraps.c                   ra144.c                   \
 g726.c                    ra288.c                   xvmcvideo.c	\
 gifdec.c                  rangecoder.c              zmbv.c	\
 golomb.c                  ratecontrol.c
 
-CFLAGS+=	$(PICFLAGS) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -c -I../../.. -I../libavformat -I../libavutil -I..
+CFLAGS+=	$(PICFLAGS) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -c -I../../.. -I../libavformat -I../libavutil -I.. -DHAVE_AV_CONFIG_H
 
 OBJECTS=${SOURCES:.c=.o}
 
--- a/src/ffmpeg/libavcodec/a52dec.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,257 +0,0 @@
-/*
- * A52 decoder
- * Copyright (c) 2001 Fabrice Bellard.
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file a52dec.c
- * A52 decoder.
- */
-
-#include "avcodec.h"
-#include "liba52/a52.h"
-
-#ifdef CONFIG_A52BIN
-#include <dlfcn.h>
-static const char* liba52name = "liba52.so.0";
-#endif
-
-/**
- * liba52 - Copyright (C) Aaron Holtzman
- * released under the GPL license.
- */
-typedef struct AC3DecodeState {
-    uint8_t inbuf[4096]; /* input buffer */
-    uint8_t *inbuf_ptr;
-    int frame_size;
-    int flags;
-    int channels;
-    a52_state_t* state;
-    sample_t* samples;
-
-    /*
-     * virtual method table
-     *
-     * using this function table so the liba52 doesn't
-     * have to be really linked together with ffmpeg
-     * and might be linked in runtime - this allows binary
-     * distribution of ffmpeg library which doens't depend
-     * on liba52 library - but if user has it installed
-     * it will be used - user might install such library
-     * separately
-     */
-    void* handle;
-    a52_state_t* (*a52_init)(uint32_t mm_accel);
-    sample_t* (*a52_samples)(a52_state_t * state);
-    int (*a52_syncinfo)(uint8_t * buf, int * flags,
-                          int * sample_rate, int * bit_rate);
-    int (*a52_frame)(a52_state_t * state, uint8_t * buf, int * flags,
-                       sample_t * level, sample_t bias);
-    void (*a52_dynrng)(a52_state_t * state,
-                         sample_t (* call) (sample_t, void *), void * data);
-    int (*a52_block)(a52_state_t * state);
-    void (*a52_free)(a52_state_t * state);
-
-} AC3DecodeState;
-
-#ifdef CONFIG_A52BIN
-static void* dlsymm(void* handle, const char* symbol)
-{
-    void* f = dlsym(handle, symbol);
-    if (!f)
-        av_log( NULL, AV_LOG_ERROR, "A52 Decoder - function '%s' can't be resolved\n", symbol);
-    return f;
-}
-#endif
-
-static int a52_decode_init(AVCodecContext *avctx)
-{
-    AC3DecodeState *s = avctx->priv_data;
-
-#ifdef CONFIG_A52BIN
-    s->handle = dlopen(liba52name, RTLD_LAZY);
-    if (!s->handle)
-    {
-        av_log( avctx, AV_LOG_ERROR, "A52 library %s could not be opened! \n%s\n", liba52name, dlerror());
-        return -1;
-    }
-    s->a52_init = (a52_state_t* (*)(uint32_t)) dlsymm(s->handle, "a52_init");
-    s->a52_samples = (sample_t* (*)(a52_state_t*)) dlsymm(s->handle, "a52_samples");
-    s->a52_syncinfo = (int (*)(uint8_t*, int*, int*, int*)) dlsymm(s->handle, "a52_syncinfo");
-    s->a52_frame = (int (*)(a52_state_t*, uint8_t*, int*, sample_t*, sample_t)) dlsymm(s->handle, "a52_frame");
-    s->a52_block = (int (*)(a52_state_t*)) dlsymm(s->handle, "a52_block");
-    s->a52_free = (void (*)(a52_state_t*)) dlsymm(s->handle, "a52_free");
-    if (!s->a52_init || !s->a52_samples || !s->a52_syncinfo
-        || !s->a52_frame || !s->a52_block || !s->a52_free)
-    {
-        dlclose(s->handle);
-        return -1;
-    }
-#else
-    /* static linked version */
-    s->handle = 0;
-    s->a52_init = a52_init;
-    s->a52_samples = a52_samples;
-    s->a52_syncinfo = a52_syncinfo;
-    s->a52_frame = a52_frame;
-    s->a52_block = a52_block;
-    s->a52_free = a52_free;
-#endif
-    s->state = s->a52_init(0); /* later use CPU flags */
-    s->samples = s->a52_samples(s->state);
-    s->inbuf_ptr = s->inbuf;
-    s->frame_size = 0;
-
-    return 0;
-}
-
-/**** the following two functions comes from a52dec */
-static inline int blah (int32_t i)
-{
-    if (i > 0x43c07fff)
-        return 32767;
-    else if (i < 0x43bf8000)
-        return -32768;
-    return i - 0x43c00000;
-}
-
-static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
-{
-    int i, j, c;
-    int32_t * f = (int32_t *) _f;       // XXX assumes IEEE float format
-
-    j = 0;
-    nchannels *= 256;
-    for (i = 0; i < 256; i++) {
-        for (c = 0; c < nchannels; c += 256)
-            s16[j++] = blah (f[i + c]);
-    }
-}
-
-/**** end */
-
-#define HEADER_SIZE 7
-
-static int a52_decode_frame(AVCodecContext *avctx,
-                            void *data, int *data_size,
-                            uint8_t *buf, int buf_size)
-{
-    AC3DecodeState *s = avctx->priv_data;
-    uint8_t *buf_ptr;
-    int flags, i, len;
-    int sample_rate, bit_rate;
-    short *out_samples = data;
-    float level;
-    static const int ac3_channels[8] = {
-        2, 1, 2, 3, 3, 4, 4, 5
-    };
-
-    buf_ptr = buf;
-    while (buf_size > 0) {
-        len = s->inbuf_ptr - s->inbuf;
-        if (s->frame_size == 0) {
-            /* no header seen : find one. We need at least 7 bytes to parse it */
-            len = HEADER_SIZE - len;
-            if (len > buf_size)
-                len = buf_size;
-            memcpy(s->inbuf_ptr, buf_ptr, len);
-            buf_ptr += len;
-            s->inbuf_ptr += len;
-            buf_size -= len;
-            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
-                len = s->a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
-                if (len == 0) {
-                    /* no sync found : move by one byte (inefficient, but simple!) */
-                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
-                    s->inbuf_ptr--;
-                } else {
-                    s->frame_size = len;
-                    /* update codec info */
-                    avctx->sample_rate = sample_rate;
-                    s->channels = ac3_channels[s->flags & 7];
-                    if (s->flags & A52_LFE)
-                        s->channels++;
-                    if (avctx->channels == 0)
-                        /* No specific number of channel requested */
-                        avctx->channels = s->channels;
-                    else if (s->channels < avctx->channels) {
-                        av_log(avctx, AV_LOG_ERROR, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
-                        avctx->channels = s->channels;
-                    }
-                    avctx->bit_rate = bit_rate;
-                }
-            }
-        } else if (len < s->frame_size) {
-            len = s->frame_size - len;
-            if (len > buf_size)
-                len = buf_size;
-
-            memcpy(s->inbuf_ptr, buf_ptr, len);
-            buf_ptr += len;
-            s->inbuf_ptr += len;
-            buf_size -= len;
-        } else {
-            flags = s->flags;
-            if (avctx->channels == 1)
-                flags = A52_MONO;
-            else if (avctx->channels == 2)
-                flags = A52_STEREO;
-            else
-                flags |= A52_ADJUST_LEVEL;
-            level = 1;
-            if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) {
-            fail:
-                s->inbuf_ptr = s->inbuf;
-                s->frame_size = 0;
-                continue;
-            }
-            for (i = 0; i < 6; i++) {
-                if (s->a52_block(s->state))
-                    goto fail;
-                float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels);
-            }
-            s->inbuf_ptr = s->inbuf;
-            s->frame_size = 0;
-            *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
-            break;
-        }
-    }
-    return buf_ptr - buf;
-}
-
-static int a52_decode_end(AVCodecContext *avctx)
-{
-    AC3DecodeState *s = avctx->priv_data;
-    s->a52_free(s->state);
-#ifdef CONFIG_A52BIN
-    dlclose(s->handle);
-#endif
-    return 0;
-}
-
-AVCodec ac3_decoder = {
-    "ac3",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AC3,
-    sizeof(AC3DecodeState),
-    a52_decode_init,
-    NULL,
-    a52_decode_end,
-    a52_decode_frame,
-};
--- a/src/ffmpeg/libavcodec/ac3dec.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,184 +0,0 @@
-/*
- * AC3 decoder
- * Copyright (c) 2001 Fabrice Bellard.
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file ac3dec.c
- * AC3 decoder.
- */
-
-//#define DEBUG
-
-#include "avcodec.h"
-#include "libac3/ac3.h"
-
-/* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
-   released under the GPL license. I may reimplement it someday... */
-typedef struct AC3DecodeState {
-    uint8_t inbuf[4096]; /* input buffer */
-    uint8_t *inbuf_ptr;
-    int frame_size;
-    int flags;
-    int channels;
-    ac3_state_t state;
-} AC3DecodeState;
-
-static int ac3_decode_init(AVCodecContext *avctx)
-{
-    AC3DecodeState *s = avctx->priv_data;
-
-    ac3_init ();
-    s->inbuf_ptr = s->inbuf;
-    s->frame_size = 0;
-    return 0;
-}
-
-stream_samples_t samples;
-
-/**** the following two functions comes from ac3dec */
-static inline int blah (int32_t i)
-{
-    if (i > 0x43c07fff)
-        return 32767;
-    else if (i < 0x43bf8000)
-        return -32768;
-    else
-        return i - 0x43c00000;
-}
-
-static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
-{
-    int i, j, c;
-    int32_t * f = (int32_t *) _f;       // XXX assumes IEEE float format
-
-    j = 0;
-    nchannels *= 256;
-    for (i = 0; i < 256; i++) {
-        for (c = 0; c < nchannels; c += 256)
-            s16[j++] = blah (f[i + c]);
-    }
-}
-
-/**** end */
-
-#define HEADER_SIZE 7
-
-static int ac3_decode_frame(AVCodecContext *avctx,
-                            void *data, int *data_size,
-                            uint8_t *buf, int buf_size)
-{
-    AC3DecodeState *s = avctx->priv_data;
-    uint8_t *buf_ptr;
-    int flags, i, len;
-    int sample_rate, bit_rate;
-    short *out_samples = data;
-    float level;
-    static const int ac3_channels[8] = {
-        2, 1, 2, 3, 3, 4, 4, 5
-    };
-
-    buf_ptr = buf;
-    while (buf_size > 0) {
-        len = s->inbuf_ptr - s->inbuf;
-        if (s->frame_size == 0) {
-            /* no header seen : find one. We need at least 7 bytes to parse it */
-            len = HEADER_SIZE - len;
-            if (len > buf_size)
-                len = buf_size;
-            memcpy(s->inbuf_ptr, buf_ptr, len);
-            buf_ptr += len;
-            s->inbuf_ptr += len;
-            buf_size -= len;
-            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
-                len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
-                if (len == 0) {
-                    /* no sync found : move by one byte (inefficient, but simple!) */
-                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
-                    s->inbuf_ptr--;
-                } else {
-                    s->frame_size = len;
-                    /* update codec info */
-                    avctx->sample_rate = sample_rate;
-                    s->channels = ac3_channels[s->flags & 7];
-                    if (s->flags & AC3_LFE)
-                        s->channels++;
-                    if (avctx->channels == 0)
-                        /* No specific number of channel requested */
-                        avctx->channels = s->channels;
-                    else if (s->channels < avctx->channels) {
-                        av_log( avctx, AV_LOG_INFO, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
-                        avctx->channels = s->channels;
-                    }
-                    avctx->bit_rate = bit_rate;
-                }
-            }
-        } else if (len < s->frame_size) {
-            len = s->frame_size - len;
-            if (len > buf_size)
-                len = buf_size;
-
-            memcpy(s->inbuf_ptr, buf_ptr, len);
-            buf_ptr += len;
-            s->inbuf_ptr += len;
-            buf_size -= len;
-        } else {
-            flags = s->flags;
-            if (avctx->channels == 1)
-                flags = AC3_MONO;
-            else if (avctx->channels == 2)
-                flags = AC3_STEREO;
-            else
-                flags |= AC3_ADJUST_LEVEL;
-            level = 1;
-            if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
-            fail:
-                s->inbuf_ptr = s->inbuf;
-                s->frame_size = 0;
-                continue;
-            }
-            for (i = 0; i < 6; i++) {
-                if (ac3_block (&s->state))
-                    goto fail;
-                float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
-            }
-            s->inbuf_ptr = s->inbuf;
-            s->frame_size = 0;
-            *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
-            break;
-        }
-    }
-    return buf_ptr - buf;
-}
-
-static int ac3_decode_end(AVCodecContext *s)
-{
-    return 0;
-}
-
-AVCodec ac3_decoder = {
-    "ac3",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AC3,
-    sizeof(AC3DecodeState),
-    ac3_decode_init,
-    NULL,
-    ac3_decode_end,
-    ac3_decode_frame,
-};
--- a/src/ffmpeg/libavcodec/amr.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,666 +0,0 @@
-/*
- * AMR Audio decoder stub
- * Copyright (c) 2003 the ffmpeg project
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /*
-    This code implements amr-nb and amr-wb audio encoder/decoder through external reference
-    code from www.3gpp.org. The licence of the code from 3gpp is unclear so you
-    have to download the code separately. Two versions exists: One fixed-point
-    and one with floats. For some reason the float-encoder is significant faster
-    atleast on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip at MR102).
-    Both float and fixed point is supported for amr-nb, but only float for
-    amr-wb.
-
-    --AMR-NB--
-    The fixed-point (TS26.073) can be downloaded from:
-    http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip
-    Extract the soure into ffmpeg/libavcodec/amr
-    To use the fixed version run "./configure" with "--enable-amr_nb-fixed"
-
-    The float version (default) can be downloaded from:
-    http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip
-    Extract the soure into ffmpeg/libavcodec/amr_float
-
-    The specification for amr-nb can be found in TS 26.071
-    (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
-    info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm
-
-    --AMR-WB--
-    The reference code can be downloaded from:
-    http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip
-    It should be extracted to "libavcodec/amrwb_float". Enable it with
-    "--enable-amr_wb".
-
-    The specification for amr-wb can be downloaded from:
-    http://www.3gpp.org/ftp/Specs/archive/26_series/26.171/26171-500.zip
-
-    If someone want to use the fixed point version it can be downloaded
-    from: http://www.3gpp.org/ftp/Specs/archive/26_series/26.173/26173-571.zip
-
- */
-
-#include "avcodec.h"
-
-#ifdef CONFIG_AMR_NB_FIXED
-
-#define MMS_IO
-
-#include "amr/sp_dec.h"
-#include "amr/d_homing.h"
-#include "amr/typedef.h"
-#include "amr/sp_enc.h"
-#include "amr/sid_sync.h"
-#include "amr/e_homing.h"
-
-#else
-#include "amr_float/interf_dec.h"
-#include "amr_float/interf_enc.h"
-#endif
-
-/* Common code for fixed and float version*/
-typedef struct AMR_bitrates
-{
-    int startrate;
-    int stoprate;
-    enum Mode mode;
-
-} AMR_bitrates;
-
-/* Match desired bitrate with closest one*/
-static enum Mode getBitrateMode(int bitrate)
-{
-    /* Adjusted so that all bitrates can be used from commandline where
-       only a multiple of 1000 can be specified*/
-    AMR_bitrates rates[]={ {0,4999,MR475}, //4
-                           {5000,5899,MR515},//5
-                           {5900,6699,MR59},//6
-                           {6700,7000,MR67},//7
-                           {7001,7949,MR74},//8
-                           {7950,9999,MR795},//9
-                           {10000,11999,MR102},//10
-                           {12000,64000,MR122},//12
-
-                         };
-    int i;
-    for(i=0;i<8;i++)
-    {
-        if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
-        {
-            return(rates[i].mode);
-        }
-    }
-    /*Return highest possible*/
-    return(MR122);
-}
-
-#ifdef CONFIG_AMR_NB_FIXED
-/* fixed point version*/
-/* frame size in serial bitstream file (frame type + serial stream + flags) */
-#define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
-
-typedef struct AMRContext {
-    int frameCount;
-    Speech_Decode_FrameState *speech_decoder_state;
-    enum RXFrameType rx_type;
-    enum Mode mode;
-    Word16 reset_flag;
-    Word16 reset_flag_old;
-
-    enum Mode enc_bitrate;
-    Speech_Encode_FrameState *enstate;
-    sid_syncState *sidstate;
-    enum TXFrameType tx_frametype;
-
-
-} AMRContext;
-
-static int amr_nb_decode_init(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    s->frameCount=0;
-    s->speech_decoder_state=NULL;
-    s->rx_type = (enum RXFrameType)0;
-    s->mode= (enum Mode)0;
-    s->reset_flag=0;
-    s->reset_flag_old=1;
-
-    if(Speech_Decode_Frame_init(&s->speech_decoder_state, "Decoder"))
-    {
-        av_log(avctx, AV_LOG_ERROR, "Speech_Decode_Frame_init error\n");
-        return -1;
-    }
-    return 0;
-}
-
-static int amr_nb_encode_init(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    s->frameCount=0;
-    s->speech_decoder_state=NULL;
-    s->rx_type = (enum RXFrameType)0;
-    s->mode= (enum Mode)0;
-    s->reset_flag=0;
-    s->reset_flag_old=1;
-
-    if(avctx->sample_rate!=8000)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n");
-        }
-        return -1;
-    }
-
-    if(avctx->channels!=1)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
-        }
-        return -1;
-    }
-
-    avctx->frame_size=160;
-    avctx->coded_frame= avcodec_alloc_frame();
-
-    if(Speech_Encode_Frame_init(&s->enstate, 0, "encoder") || sid_sync_init (&s->sidstate))
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Speech_Encode_Frame_init error\n");
-        }
-        return -1;
-    }
-
-    s->enc_bitrate=getBitrateMode(avctx->bit_rate);
-
-    return 0;
-}
-
-static int amr_nb_encode_close(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    Speech_Encode_Frame_exit(&s->enstate);
-    sid_sync_exit (&s->sidstate);
-    av_freep(&avctx->coded_frame);
-    return 0;
-}
-
-static int amr_nb_decode_close(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    Speech_Decode_Frame_exit(&s->speech_decoder_state);
-    return 0;
-}
-
-static int amr_nb_decode_frame(AVCodecContext * avctx,
-            void *data, int *data_size,
-            uint8_t * buf, int buf_size)
-{
-    AMRContext *s = avctx->priv_data;
-
-    uint8_t*amrData=buf;
-    int offset=0;
-
-    UWord8 toc, q, ft;
-
-    Word16 serial[SERIAL_FRAMESIZE];   /* coded bits */
-    Word16 *synth;
-    UWord8 *packed_bits;
-
-    static Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
-    int i;
-
-    //printf("amr_decode_frame data_size=%i buf=0x%X buf_size=%d frameCount=%d!!\n",*data_size,buf,buf_size,s->frameCount);
-
-    synth=data;
-
-//    while(offset<buf_size)
-    {
-        toc=amrData[offset];
-        /* read rest of the frame based on ToC byte */
-        q  = (toc >> 2) & 0x01;
-        ft = (toc >> 3) & 0x0F;
-
-        //printf("offset=%d, packet_size=%d amrData= 0x%X %X %X %X\n",offset,packed_size[ft],amrData[offset],amrData[offset+1],amrData[offset+2],amrData[offset+3]);
-
-        offset++;
-
-        packed_bits=amrData+offset;
-
-        offset+=packed_size[ft];
-
-        //Unsort and unpack bits
-        s->rx_type = UnpackBits(q, ft, packed_bits, &s->mode, &serial[1]);
-
-        //We have a new frame
-        s->frameCount++;
-
-        if (s->rx_type == RX_NO_DATA)
-        {
-            s->mode = s->speech_decoder_state->prev_mode;
-        }
-        else {
-            s->speech_decoder_state->prev_mode = s->mode;
-        }
-
-        /* if homed: check if this frame is another homing frame */
-        if (s->reset_flag_old == 1)
-        {
-            /* only check until end of first subframe */
-            s->reset_flag = decoder_homing_frame_test_first(&serial[1], s->mode);
-        }
-        /* produce encoder homing frame if homed & input=decoder homing frame */
-        if ((s->reset_flag != 0) && (s->reset_flag_old != 0))
-        {
-            for (i = 0; i < L_FRAME; i++)
-            {
-                synth[i] = EHF_MASK;
-            }
-        }
-        else
-        {
-            /* decode frame */
-            Speech_Decode_Frame(s->speech_decoder_state, s->mode, &serial[1], s->rx_type, synth);
-        }
-
-        //Each AMR-frame results in 160 16-bit samples
-        *data_size+=160*2;
-        synth+=160;
-
-        /* if not homed: check whether current frame is a homing frame */
-        if (s->reset_flag_old == 0)
-        {
-            /* check whole frame */
-            s->reset_flag = decoder_homing_frame_test(&serial[1], s->mode);
-        }
-        /* reset decoder if current frame is a homing frame */
-        if (s->reset_flag != 0)
-        {
-            Speech_Decode_Frame_reset(s->speech_decoder_state);
-        }
-        s->reset_flag_old = s->reset_flag;
-
-    }
-    return offset;
-}
-
-
-static int amr_nb_encode_frame(AVCodecContext *avctx,
-                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
-{
-    short serial_data[250] = {0};
-
-    AMRContext *s = avctx->priv_data;
-    int written;
-
-    s->reset_flag = encoder_homing_frame_test(data);
-
-    Speech_Encode_Frame(s->enstate, s->enc_bitrate, data, &serial_data[1], &s->mode);
-
-    /* add frame type and mode */
-    sid_sync (s->sidstate, s->mode, &s->tx_frametype);
-
-    written = PackBits(s->mode, s->enc_bitrate, s->tx_frametype, &serial_data[1], frame);
-
-    if (s->reset_flag != 0)
-    {
-        Speech_Encode_Frame_reset(s->enstate);
-        sid_sync_reset(s->sidstate);
-    }
-    return written;
-}
-
-
-#elif defined(CONFIG_AMR_NB) /* Float point version*/
-
-typedef struct AMRContext {
-    int frameCount;
-    void * decState;
-    int *enstate;
-    enum Mode enc_bitrate;
-} AMRContext;
-
-static int amr_nb_decode_init(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    s->frameCount=0;
-    s->decState=Decoder_Interface_init();
-    if(!s->decState)
-    {
-        av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
-        return -1;
-    }
-    return 0;
-}
-
-static int amr_nb_encode_init(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    s->frameCount=0;
-
-    if(avctx->sample_rate!=8000)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only 8000Hz sample rate supported\n");
-        }
-        return -1;
-    }
-
-    if(avctx->channels!=1)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
-        }
-        return -1;
-    }
-
-    avctx->frame_size=160;
-    avctx->coded_frame= avcodec_alloc_frame();
-
-    s->enstate=Encoder_Interface_init(0);
-    if(!s->enstate)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Encoder_Interface_init error\n");
-        }
-        return -1;
-    }
-
-    s->enc_bitrate=getBitrateMode(avctx->bit_rate);
-
-    return 0;
-}
-
-static int amr_nb_decode_close(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    Decoder_Interface_exit(s->decState);
-    return 0;
-}
-
-static int amr_nb_encode_close(AVCodecContext * avctx)
-{
-    AMRContext *s = avctx->priv_data;
-    Encoder_Interface_exit(s->enstate);
-    av_freep(&avctx->coded_frame);
-    return 0;
-}
-
-static int amr_nb_decode_frame(AVCodecContext * avctx,
-            void *data, int *data_size,
-            uint8_t * buf, int buf_size)
-{
-    AMRContext *s = (AMRContext*)avctx->priv_data;
-
-    uint8_t*amrData=buf;
-    static short block_size[16]={ 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
-    enum Mode dec_mode;
-    int packet_size;
-
-    /* av_log(NULL,AV_LOG_DEBUG,"amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",buf,buf_size,s->frameCount); */
-
-    if(buf_size==0) {
-        /* nothing to do */
-        return 0;
-    }
-
-    dec_mode = (buf[0] >> 3) & 0x000F;
-    packet_size = block_size[dec_mode]+1;
-
-    if(packet_size > buf_size) {
-        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size);
-        return -1;
-    }
-
-    s->frameCount++;
-    /* av_log(NULL,AV_LOG_DEBUG,"packet_size=%d amrData= 0x%X %X %X %X\n",packet_size,amrData[0],amrData[1],amrData[2],amrData[3]); */
-    /* call decoder */
-    Decoder_Interface_Decode(s->decState, amrData, data, 0);
-    *data_size=160*2;
-
-    return packet_size;
-}
-
-static int amr_nb_encode_frame(AVCodecContext *avctx,
-                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
-{
-    AMRContext *s = (AMRContext*)avctx->priv_data;
-    int written;
-
-    written = Encoder_Interface_Encode(s->enstate,
-        s->enc_bitrate,
-        data,
-        frame,
-        0);
-    /* av_log(NULL,AV_LOG_DEBUG,"amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",written, s->enc_bitrate, frame[0] ); */
-
-    return written;
-}
-
-#endif
-
-#if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED)
-
-AVCodec amr_nb_decoder =
-{
-    "amr_nb",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AMR_NB,
-    sizeof(AMRContext),
-    amr_nb_decode_init,
-    NULL,
-    amr_nb_decode_close,
-    amr_nb_decode_frame,
-};
-
-AVCodec amr_nb_encoder =
-{
-    "amr_nb",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AMR_NB,
-    sizeof(AMRContext),
-    amr_nb_encode_init,
-    amr_nb_encode_frame,
-    amr_nb_encode_close,
-    NULL,
-};
-
-#endif
-
-/* -----------AMR wideband ------------*/
-#ifdef CONFIG_AMR_WB
-
-#ifdef _TYPEDEF_H
-//To avoid duplicate typedefs from typdef in amr-nb
-#define typedef_h
-#endif
-
-#include "amrwb_float/enc_if.h"
-#include "amrwb_float/dec_if.h"
-
-/* Common code for fixed and float version*/
-typedef struct AMRWB_bitrates
-{
-    int startrate;
-    int stoprate;
-    int mode;
-
-} AMRWB_bitrates;
-
-static int getWBBitrateMode(int bitrate)
-{
-    /* Adjusted so that all bitrates can be used from commandline where
-       only a multiple of 1000 can be specified*/
-    AMRWB_bitrates rates[]={ {0,7999,0}, //6.6kHz
-                           {8000,9999,1},//8.85
-                           {10000,13000,2},//12.65
-                           {13001,14999,3},//14.25
-                           {15000,17000,4},//15.85
-                           {17001,18000,5},//18.25
-                           {18001,22000,6},//19.85
-                           {22001,23000,7},//23.05
-                           {23001,24000,8},//23.85
-
-                         };
-    int i;
-
-    for(i=0;i<9;i++)
-    {
-        if(rates[i].startrate<=bitrate && rates[i].stoprate>=bitrate)
-        {
-            return(rates[i].mode);
-        }
-    }
-    /*Return highest possible*/
-    return(8);
-}
-
-
-typedef struct AMRWBContext {
-    int frameCount;
-    void *state;
-    int mode;
-    Word16 allow_dtx;
-} AMRWBContext;
-
-static int amr_wb_encode_init(AVCodecContext * avctx)
-{
-    AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
-    s->frameCount=0;
-
-    if(avctx->sample_rate!=16000)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only 16000Hz sample rate supported\n");
-        }
-        return -1;
-    }
-
-    if(avctx->channels!=1)
-    {
-        if(avctx->debug)
-        {
-            av_log(avctx, AV_LOG_DEBUG, "Only mono supported\n");
-        }
-        return -1;
-    }
-
-    avctx->frame_size=320;
-    avctx->coded_frame= avcodec_alloc_frame();
-
-    s->state = E_IF_init();
-    s->mode=getWBBitrateMode(avctx->bit_rate);
-    s->allow_dtx=0;
-
-    return 0;
-}
-
-static int amr_wb_encode_close(AVCodecContext * avctx)
-{
-    AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
-    E_IF_exit(s->state);
-    av_freep(&avctx->coded_frame);
-    s->frameCount++;
-    return 0;
-}
-
-static int amr_wb_encode_frame(AVCodecContext *avctx,
-                            unsigned char *frame/*out*/, int buf_size, void *data/*in*/)
-{
-    AMRWBContext *s = (AMRWBContext*) avctx->priv_data;
-    int size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
-    return size;
-}
-
-static int amr_wb_decode_init(AVCodecContext * avctx)
-{
-    AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
-    s->frameCount=0;
-    s->state = D_IF_init();
-    return 0;
-}
-
-extern const UWord8 block_size[];
-
-static int amr_wb_decode_frame(AVCodecContext * avctx,
-            void *data, int *data_size,
-            uint8_t * buf, int buf_size)
-{
-    AMRWBContext *s = (AMRWBContext*)avctx->priv_data;
-
-    uint8_t*amrData=buf;
-    int mode;
-    int packet_size;
-
-    if(buf_size==0) {
-        /* nothing to do */
-        return 0;
-    }
-
-    mode = (amrData[0] >> 3) & 0x000F;
-    packet_size = block_size[mode];
-
-    if(packet_size > buf_size) {
-        av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n", buf_size, packet_size+1);
-        return -1;
-    }
-
-    s->frameCount++;
-    D_IF_decode( s->state, amrData, data, _good_frame);
-    *data_size=320*2;
-    return packet_size;
-}
-
-static int amr_wb_decode_close(AVCodecContext * avctx)
-{
-    AMRWBContext *s = (AMRWBContext *)avctx->priv_data;
-    D_IF_exit(s->state);
-    return 0;
-}
-
-AVCodec amr_wb_decoder =
-{
-    "amr_wb",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AMR_WB,
-    sizeof(AMRWBContext),
-    amr_wb_decode_init,
-    NULL,
-    amr_wb_decode_close,
-    amr_wb_decode_frame,
-};
-
-AVCodec amr_wb_encoder =
-{
-    "amr_wb",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_AMR_WB,
-    sizeof(AMRWBContext),
-    amr_wb_encode_init,
-    amr_wb_encode_frame,
-    amr_wb_encode_close,
-    NULL,
-};
-
-#endif //CONFIG_AMR_WB
--- a/src/ffmpeg/libavcodec/faad.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,332 +0,0 @@
-/*
- * Faad decoder
- * Copyright (c) 2003 Zdenek Kabelac.
- * Copyright (c) 2004 Thomas Raivio.
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file faad.c
- * AAC decoder.
- *
- * still a bit unfinished - but it plays something
- */
-
-#include "avcodec.h"
-#include "faad.h"
-
-#ifndef FAADAPI
-#define FAADAPI
-#endif
-
-/*
- * when CONFIG_FAADBIN is defined the libfaad will be opened at runtime
- */
-//#undef CONFIG_FAADBIN
-//#define CONFIG_FAADBIN
-
-#ifdef CONFIG_FAADBIN
-#include <dlfcn.h>
-static const char* libfaadname = "libfaad.so.0";
-#else
-#define dlopen(a)
-#define dlclose(a)
-#endif
-
-typedef struct {
-    void* handle;               /* dlopen handle */
-    void* faac_handle;          /* FAAD library handle */
-    int sample_size;
-    int init;
-
-    /* faad calls */
-    faacDecHandle FAADAPI (*faacDecOpen)(void);
-    faacDecConfigurationPtr FAADAPI (*faacDecGetCurrentConfiguration)(faacDecHandle hDecoder);
-#ifndef FAAD2_VERSION
-        int FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
-                                           faacDecConfigurationPtr config);
-        int FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
-                                unsigned char *buffer,
-                                unsigned long *samplerate,
-                                unsigned long *channels);
-        int FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
-                                unsigned long SizeOfDecoderSpecificInfo,
-                                unsigned long *samplerate, unsigned long *channels);
-        int FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
-                                unsigned char *buffer,
-                                unsigned long *bytesconsumed,
-                                short *sample_buffer,
-                                unsigned long *samples);
-#else
-        unsigned char FAADAPI (*faacDecSetConfiguration)(faacDecHandle hDecoder,
-                                                     faacDecConfigurationPtr config);
-        long FAADAPI (*faacDecInit)(faacDecHandle hDecoder,
-                                   unsigned char *buffer,
-                                 unsigned long buffer_size,
-                                 unsigned long *samplerate,
-                                 unsigned char *channels);
-        char FAADAPI (*faacDecInit2)(faacDecHandle hDecoder, unsigned char *pBuffer,
-                                 unsigned long SizeOfDecoderSpecificInfo,
-                                 unsigned long *samplerate, unsigned char *channels);
-        void *FAADAPI (*faacDecDecode)(faacDecHandle hDecoder,
-                                         faacDecFrameInfo *hInfo,
-                                         unsigned char *buffer,
-                                                                 unsigned long buffer_size);
-        char* FAADAPI (*faacDecGetErrorMessage)(unsigned char errcode);
-#endif
-
-    void FAADAPI (*faacDecClose)(faacDecHandle hDecoder);
-
-
-} FAACContext;
-
-static const unsigned long faac_srates[] =
-{
-    96000, 88200, 64000, 48000, 44100, 32000,
-    24000, 22050, 16000, 12000, 11025, 8000
-};
-
-static int faac_init_mp4(AVCodecContext *avctx)
-{
-    FAACContext *s = (FAACContext *) avctx->priv_data;
-    unsigned long samplerate;
-#ifndef FAAD2_VERSION
-    unsigned long channels;
-#else
-    unsigned char channels;
-#endif
-    int r = 0;
-
-    if (avctx->extradata){
-        r = s->faacDecInit2(s->faac_handle, (uint8_t*) avctx->extradata,
-                            avctx->extradata_size,
-                            &samplerate, &channels);
-        if (r < 0){
-            av_log(avctx, AV_LOG_ERROR,
-                   "faacDecInit2 failed r:%d   sr:%ld  ch:%ld  s:%d\n",
-                   r, samplerate, (long)channels, avctx->extradata_size);
-        } else {
-            avctx->sample_rate = samplerate;
-            avctx->channels = channels;
-            s->init = 1;
-        }
-    }
-
-    return r;
-}
-
-static int faac_decode_frame(AVCodecContext *avctx,
-                             void *data, int *data_size,
-                             uint8_t *buf, int buf_size)
-{
-    FAACContext *s = (FAACContext *) avctx->priv_data;
-#ifndef FAAD2_VERSION
-    unsigned long bytesconsumed;
-    short *sample_buffer = NULL;
-    unsigned long samples;
-    int out;
-#else
-    faacDecFrameInfo frame_info;
-    void *out;
-#endif
-    if(buf_size == 0)
-        return 0;
-#ifndef FAAD2_VERSION
-    out = s->faacDecDecode(s->faac_handle,
-                           (unsigned char*)buf,
-                           &bytesconsumed,
-                           data,
-                           &samples);
-    samples *= s->sample_size;
-    if (data_size)
-        *data_size = samples;
-    return (buf_size < (int)bytesconsumed)
-        ? buf_size : (int)bytesconsumed;
-#else
-
-    if(!s->init){
-        unsigned long srate;
-        unsigned char channels;
-        int r = s->faacDecInit(s->faac_handle, buf, buf_size, &srate, &channels);
-        if(r < 0){
-            av_log(avctx, AV_LOG_ERROR, "faac: codec init failed: %s\n",
-                   s->faacDecGetErrorMessage(frame_info.error));
-            return -1;
-        }
-        avctx->sample_rate = srate;
-        avctx->channels = channels;
-        s->init = 1;
-    }
-
-    out = s->faacDecDecode(s->faac_handle, &frame_info, (unsigned char*)buf, (unsigned long)buf_size);
-
-    if (frame_info.error > 0) {
-        av_log(avctx, AV_LOG_ERROR, "faac: frame decoding failed: %s\n",
-                s->faacDecGetErrorMessage(frame_info.error));
-        return -1;
-    }
-
-    frame_info.samples *= s->sample_size;
-    memcpy(data, out, frame_info.samples); // CHECKME - can we cheat this one
-
-    if (data_size)
-        *data_size = frame_info.samples;
-
-    return (buf_size < (int)frame_info.bytesconsumed)
-        ? buf_size : (int)frame_info.bytesconsumed;
-#endif
-}
-
-static int faac_decode_end(AVCodecContext *avctx)
-{
-    FAACContext *s = (FAACContext *) avctx->priv_data;
-
-    if (s->faacDecClose)
-        s->faacDecClose(s->faac_handle);
-
-    dlclose(s->handle);
-    return 0;
-}
-
-static int faac_decode_init(AVCodecContext *avctx)
-{
-    FAACContext *s = (FAACContext *) avctx->priv_data;
-    faacDecConfigurationPtr faac_cfg;
-
-#ifdef CONFIG_FAADBIN
-    const char* err = 0;
-
-    s->handle = dlopen(libfaadname, RTLD_LAZY);
-    if (!s->handle)
-    {
-        av_log(avctx, AV_LOG_ERROR, "FAAD library: %s could not be opened! \n%s\n",
-                libfaadname, dlerror());
-        return -1;
-    }
-#define dfaac(a, b) \
-    do { static const char* n = "faacDec" #a; \
-    if ((s->faacDec ## a = b dlsym( s->handle, n )) == NULL) { err = n; break; } } while(0)
-    for(;;) {
-#else  /* !CONFIG_FAADBIN */
-#define dfaac(a, b)     s->faacDec ## a = faacDec ## a
-#endif /* CONFIG_FAADBIN */
-
-        // resolve all needed function calls
-        dfaac(Open, (faacDecHandle FAADAPI (*)(void)));
-        dfaac(GetCurrentConfiguration, (faacDecConfigurationPtr
-                                        FAADAPI (*)(faacDecHandle)));
-#ifndef FAAD2_VERSION
-        dfaac(SetConfiguration, (int FAADAPI (*)(faacDecHandle,
-                                                           faacDecConfigurationPtr)));
-
-        dfaac(Init, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                                     unsigned long*, unsigned long*)));
-    dfaac(Init2, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                                       unsigned long, unsigned long*,
-                                       unsigned long*)));
-    dfaac(Close, (void FAADAPI (*)(faacDecHandle hDecoder)));
-        dfaac(Decode, (int FAADAPI (*)(faacDecHandle, unsigned char*,
-                             unsigned long*, short*, unsigned long*)));
-#else
-        dfaac(SetConfiguration, (unsigned char FAADAPI (*)(faacDecHandle,
-                                                           faacDecConfigurationPtr)));
-        dfaac(Init, (long FAADAPI (*)(faacDecHandle, unsigned char*,
-                                     unsigned long, unsigned long*, unsigned char*)));
-        dfaac(Init2, (char FAADAPI (*)(faacDecHandle, unsigned char*,
-                                       unsigned long, unsigned long*,
-                                       unsigned char*)));
-        dfaac(Decode, (void *FAADAPI (*)(faacDecHandle, faacDecFrameInfo*,
-                             unsigned char*, unsigned long)));
-        dfaac(GetErrorMessage, (char* FAADAPI (*)(unsigned char)));
-#endif
-#undef dfacc
-
-#ifdef CONFIG_FAADBIN
-        break;
-    }
-    if (err) {
-        dlclose(s->handle);
-        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot resolve %s in %s!\n",
-                err, libfaadname);
-        return -1;
-    }
-#endif
-
-    s->faac_handle = s->faacDecOpen();
-    if (!s->faac_handle) {
-        av_log(avctx, AV_LOG_ERROR, "FAAD library: cannot create handler!\n");
-        faac_decode_end(avctx);
-        return -1;
-    }
-
-
-    faac_cfg = s->faacDecGetCurrentConfiguration(s->faac_handle);
-
-    if (faac_cfg) {
-        switch (avctx->bits_per_sample) {
-        case 8: av_log(avctx, AV_LOG_ERROR, "FAADlib unsupported bps %d\n", avctx->bits_per_sample); break;
-        default:
-        case 16:
-#ifdef FAAD2_VERSION
-            faac_cfg->outputFormat = FAAD_FMT_16BIT;
-#endif
-            s->sample_size = 2;
-            break;
-        case 24:
-#ifdef FAAD2_VERSION
-            faac_cfg->outputFormat = FAAD_FMT_24BIT;
-#endif
-            s->sample_size = 3;
-            break;
-        case 32:
-#ifdef FAAD2_VERSION
-            faac_cfg->outputFormat = FAAD_FMT_32BIT;
-#endif
-            s->sample_size = 4;
-            break;
-        }
-
-        faac_cfg->defSampleRate = (!avctx->sample_rate) ? 44100 : avctx->sample_rate;
-        faac_cfg->defObjectType = LC;
-    }
-
-    s->faacDecSetConfiguration(s->faac_handle, faac_cfg);
-
-    faac_init_mp4(avctx);
-
-    return 0;
-}
-
-#define AAC_CODEC(id, name)     \
-AVCodec name ## _decoder = {    \
-    #name,                      \
-    CODEC_TYPE_AUDIO,           \
-    id,                         \
-    sizeof(FAACContext),        \
-    faac_decode_init,           \
-    NULL,                       \
-    faac_decode_end,            \
-    faac_decode_frame,          \
-}
-
-// FIXME - raw AAC files - maybe just one entry will be enough
-AAC_CODEC(CODEC_ID_AAC, aac);
-// If it's mp4 file - usually embeded into Qt Mov
-AAC_CODEC(CODEC_ID_MPEG4AAC, mpeg4aac);
-
-#undef AAC_CODEC
--- a/src/ffmpeg/libavcodec/jpeg_ls.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,847 +0,0 @@
-/*
- * JPEG-LS encoder and decoder
- * Copyright (c) 2003 Michael Niedermayer
- * Copyright (c) 2006 Konstantin Shishkov
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "golomb.h"
-
-/**
- * @file jpeg_ls.c
- * JPEG-LS encoder and decoder.
- */
-
-typedef struct JpeglsContext{
-    AVCodecContext *avctx;
-    AVFrame picture;
-}JpeglsContext;
-
-typedef struct JLSState{
-    int T1, T2, T3;
-    int A[367], B[367], C[365], N[367];
-    int limit, reset, bpp, qbpp, maxval, range;
-    int near, twonear;
-    int run_index[3];
-}JLSState;
-
-static const uint8_t log2_run[32]={
- 0, 0, 0, 0, 1, 1, 1, 1,
- 2, 2, 2, 2, 3, 3, 3, 3,
- 4, 4, 5, 5, 6, 6, 7, 7,
- 8, 9,10,11,12,13,14,15
-};
-
-/*
-* Uncomment this to significantly speed up decoding of broken JPEG-LS
-* (or test broken JPEG-LS decoder) and slow down ordinary decoding a bit.
-*
-* There is no Golomb code with length >= 32 bits possible, so check and
-* avoid situation of 32 zeros, FFmpeg Golomb decoder is painfully slow
-* on this errors.
-*/
-//#define JLS_BROKEN
-
-/********** Functions for both encoder and decoder **********/
-
-/**
- * Calculate initial JPEG-LS parameters
- */
-static void ls_init_state(JLSState *state){
-    int i;
-
-    state->twonear = state->near * 2 + 1;
-    state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
-
-    // QBPP = ceil(log2(RANGE))
-    for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
-
-    if(state->bpp < 8)
-        state->limit = 16 + 2 * state->bpp - state->qbpp;
-    else
-        state->limit = (4 * state->bpp) - state->qbpp;
-
-    for(i = 0; i < 367; i++) {
-        state->A[i] = (state->range + 32) >> 6;
-        if(state->A[i] < 2)
-            state->A[i] = 2;
-        state->N[i] = 1;
-    }
-
-}
-
-/**
- * Calculate quantized gradient value, used for context determination
- */
-static inline int quantize(JLSState *s, int v){ //FIXME optimize
-    if(v==0) return 0;
-    if(v < 0){
-        if(v <= -s->T3) return -4;
-        if(v <= -s->T2) return -3;
-        if(v <= -s->T1) return -2;
-        if(v <  -s->near) return -1;
-        return 0;
-    }else{
-        if(v <= s->near) return 0;
-        if(v <  s->T1) return 1;
-        if(v <  s->T2) return 2;
-        if(v <  s->T3) return 3;
-        return 4;
-    }
-}
-
-/**
- * Custom value clipping function used in T1, T2, T3 calculation
- */
-static inline int iso_clip(int v, int vmin, int vmax){
-    if(v > vmax || v < vmin) return vmin;
-    else                     return v;
-}
-
-/**
- * Calculate JPEG-LS codec values
- */
-static void reset_ls_coding_parameters(JLSState *s, int reset_all){
-    const int basic_t1= 3;
-    const int basic_t2= 7;
-    const int basic_t3= 21;
-    int factor;
-
-    if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
-
-    if(s->maxval >=128){
-        factor= (FFMIN(s->maxval, 4095) + 128)>>8;
-
-        if(s->T1==0     || reset_all)
-            s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
-        if(s->T2==0     || reset_all)
-            s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
-        if(s->T3==0     || reset_all)
-            s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
-    }else{
-        factor= 256 / (s->maxval + 1);
-
-        if(s->T1==0     || reset_all)
-            s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
-        if(s->T2==0     || reset_all)
-            s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
-        if(s->T3==0     || reset_all)
-            s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
-    }
-
-    if(s->reset==0  || reset_all) s->reset= 64;
-//    av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
-}
-
-
-/********** Decoder-specific functions **********/
-
-/**
- * Decode LSE block with initialization parameters
- */
-static int decode_lse(MJpegDecodeContext *s)
-{
-    int len, id;
-
-    /* XXX: verify len field validity */
-    len = get_bits(&s->gb, 16);
-    id = get_bits(&s->gb, 8);
-
-    switch(id){
-    case 1:
-        s->maxval= get_bits(&s->gb, 16);
-        s->t1= get_bits(&s->gb, 16);
-        s->t2= get_bits(&s->gb, 16);
-        s->t3= get_bits(&s->gb, 16);
-        s->reset= get_bits(&s->gb, 16);
-
-//        reset_ls_coding_parameters(s, 0);
-        //FIXME quant table?
-        break;
-    case 2:
-    case 3:
-        av_log(s->avctx, AV_LOG_ERROR, "palette not supported\n");
-        return -1;
-    case 4:
-        av_log(s->avctx, AV_LOG_ERROR, "oversize image not supported\n");
-        return -1;
-    default:
-        av_log(s->avctx, AV_LOG_ERROR, "invalid id %d\n", id);
-        return -1;
-    }
-//    av_log(s->avctx, AV_LOG_DEBUG, "ID=%i, T=%i,%i,%i\n", id, s->t1, s->t2, s->t3);
-
-    return 0;
-}
-
-
-/**
- * Get context-dependent Golomb code, decode it and update context
- */
-static inline int ls_get_code_regular(GetBitContext *gb, JLSState *state, int Q){
-    int k, ret;
-
-    for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
-
-#ifdef JLS_BROKEN
-    if(!show_bits_long(gb, 32))return -1;
-#endif
-    ret = get_ur_golomb_jpegls(gb, k, state->limit, state->qbpp);
-
-    /* decode mapped error */
-    if(ret & 1)
-        ret = -((ret + 1) >> 1);
-    else
-        ret >>= 1;
-
-    /* for NEAR=0, k=0 and 2*B[Q] <= - N[Q] mapping is reversed */
-    if(!state->near && !k && (2 * state->B[Q] <= -state->N[Q]))
-        ret = -(ret + 1);
-
-    state->A[Q] += FFABS(ret);
-    ret *= state->twonear;
-    state->B[Q] += ret;
-
-    if(state->N[Q] == state->reset) {
-        state->A[Q] >>= 1;
-        state->B[Q] >>= 1;
-        state->N[Q] >>= 1;
-    }
-    state->N[Q]++;
-
-    if(state->B[Q] <= -state->N[Q]) {
-        state->B[Q] += state->N[Q];
-        if(state->C[Q] > -128)
-            state->C[Q]--;
-        if(state->B[Q] <= -state->N[Q])
-            state->B[Q] = -state->N[Q] + 1;
-    }else if(state->B[Q] > 0){
-        state->B[Q] -= state->N[Q];
-        if(state->C[Q] < 127)
-            state->C[Q]++;
-        if(state->B[Q] > 0)
-            state->B[Q] = 0;
-    }
-
-    return ret;
-}
-
-/**
- * Get Golomb code, decode it and update state for run termination
- */
-static inline int ls_get_code_runterm(GetBitContext *gb, JLSState *state, int RItype, int limit_add){
-    int k, ret, temp, map;
-    int Q = 365 + RItype;
-
-    if(!RItype)
-        temp = state->A[Q];
-    else
-        temp = state->A[Q] + (state->N[Q] >> 1);
-
-    for(k = 0; (state->N[Q] << k) < temp; k++);
-
-#ifdef JLS_BROKEN
-    if(!show_bits_long(gb, 32))return -1;
-#endif
-    ret = get_ur_golomb_jpegls(gb, k, state->limit - limit_add - 1, state->qbpp);
-
-    /* decode mapped error */
-    map = 0;
-    if(!k && (RItype || ret) && (2 * state->B[Q] < state->N[Q]))
-        map = 1;
-    ret += RItype + map;
-
-    if(ret & 1){
-        ret = map - ((ret + 1) >> 1);
-        state->B[Q]++;
-    } else {
-        ret = ret >> 1;
-    }
-
-    /* update state */
-    state->A[Q] += FFABS(ret) - RItype;
-    ret *= state->twonear;
-    if(state->N[Q] == state->reset){
-        state->A[Q] >>=1;
-        state->B[Q] >>=1;
-        state->N[Q] >>=1;
-    }
-    state->N[Q]++;
-
-    return ret;
-}
-
-/**
- * Decode one line of image
- */
-static inline void ls_decode_line(JLSState *state, MJpegDecodeContext *s, uint8_t *last, uint8_t *dst, int last2, int w, int stride, int comp){
-    int i, x = 0;
-    int Ra, Rb, Rc, Rd;
-    int D0, D1, D2;
-
-    while(x < w) {
-        int err, pred;
-
-        /* compute gradients */
-        Ra = x ? dst[x - stride] : last[x];
-        Rb = last[x];
-        Rc = x ? last[x - stride] : last2;
-        Rd = (x >= w - stride) ? last[x] : last[x + stride];
-        D0 = Rd - Rb;
-        D1 = Rb - Rc;
-        D2 = Rc - Ra;
-        /* run mode */
-        if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
-            int r;
-            int RItype;
-
-            /* decode full runs while available */
-            while(get_bits1(&s->gb)) {
-                int r;
-                r = 1 << log2_run[state->run_index[comp]];
-                if(x + r * stride > w) {
-                    r = (w - x) / stride;
-                }
-                for(i = 0; i < r; i++) {
-                    dst[x] = Ra;
-                    x += stride;
-                }
-                /* if EOL reached, we stop decoding */
-                if(r != (1 << log2_run[state->run_index[comp]]))
-                    return;
-                if(state->run_index[comp] < 31)
-                    state->run_index[comp]++;
-                if(x + stride > w)
-                    return;
-            }
-            /* decode aborted run */
-            r = log2_run[state->run_index[comp]];
-            if(r)
-                r = get_bits_long(&s->gb, r);
-            for(i = 0; i < r; i++) {
-                dst[x] = Ra;
-                x += stride;
-            }
-
-            /* decode run termination value */
-            Rb = last[x];
-            RItype = (FFABS(Ra - Rb) <= state->near) ? 1 : 0;
-            err = ls_get_code_runterm(&s->gb, state, RItype, log2_run[state->run_index[comp]]);
-            if(state->run_index[comp])
-                state->run_index[comp]--;
-
-            if(state->near && RItype){
-                pred = Ra + err;
-            } else {
-                if(Rb < Ra)
-                    pred = Rb - err;
-                else
-                    pred = Rb + err;
-            }
-
-            if(state->near){
-                if(pred < -state->near)
-                    pred += state->range * state->twonear;
-                else if(pred > state->maxval + state->near)
-                    pred -= state->range * state->twonear;
-                pred = clip(pred, 0, state->maxval);
-            }
-
-            dst[x] = pred;
-            x += stride;
-        } else { /* regular mode */
-            int context, sign;
-
-            context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
-            pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
-
-            if(context < 0){
-                context = -context;
-                sign = 1;
-            }else{
-                sign = 0;
-            }
-
-            if(sign){
-                pred = clip(pred - state->C[context], 0, state->maxval);
-                err = -ls_get_code_regular(&s->gb, state, context);
-            } else {
-                pred = clip(pred + state->C[context], 0, state->maxval);
-                err = ls_get_code_regular(&s->gb, state, context);
-            }
-
-            /* we have to do something more for near-lossless coding */
-            pred += err;
-            if(state->near) {
-                if(pred < -state->near)
-                    pred += state->range * state->twonear;
-                else if(pred > state->maxval + state->near)
-                    pred -= state->range * state->twonear;
-                pred = clip(pred, 0, state->maxval);
-            }
-
-            dst[x] = pred;
-            x += stride;
-        }
-    }
-}
-
-static int ls_decode_picture(MJpegDecodeContext *s, int near, int point_transform, int ilv){
-    int i, t = 0;
-    uint8_t *zero, *last, *cur;
-    JLSState *state;
-    int off, stride, width;
-
-    zero = av_mallocz(s->picture.linesize[0]);
-    last = zero;
-    cur = s->picture.data[0];
-
-    state = av_mallocz(sizeof(JLSState));
-    /* initialize JPEG-LS state from JPEG parameters */
-    state->near = near;
-    state->bpp = (s->bits < 2) ? 2 : s->bits;
-    state->maxval = s->maxval;
-    state->T1 = s->t1;
-    state->T2 = s->t2;
-    state->T3 = s->t3;
-    state->reset = s->reset;
-    reset_ls_coding_parameters(state, 0);
-    ls_init_state(state);
-
-//    av_log(s->avctx, AV_LOG_DEBUG, "JPEG-LS params: %ix%i NEAR=%i MV=%i T(%i,%i,%i) RESET=%i, LIMIT=%i, qbpp=%i, RANGE=%i\n",s->width,s->height,state->near,state->maxval,state->T1,state->T2,state->T3,state->reset,state->limit,state->qbpp, state->range);
-//    av_log(s->avctx, AV_LOG_DEBUG, "JPEG params: ILV=%i Pt=%i BPP=%i, scan = %i\n", ilv, point_transform, s->bits, s->cur_scan);
-    if(ilv == 0) { /* separate planes */
-        off = s->cur_scan - 1;
-        stride = (s->nb_components > 1) ? 3 : 1;
-        width = s->width * stride;
-        cur += off;
-        for(i = 0; i < s->height; i++) {
-            ls_decode_line(state, s, last, cur, t, width, stride, off);
-            t = last[0];
-            last = cur;
-            cur += s->picture.linesize[0];
-
-            if (s->restart_interval && !--s->restart_count) {
-                align_get_bits(&s->gb);
-                skip_bits(&s->gb, 16); /* skip RSTn */
-            }
-        }
-    } else if(ilv == 1) { /* line interleaving */
-        int j;
-        int Rc[3] = {0, 0, 0};
-        memset(cur, 0, s->picture.linesize[0]);
-        width = s->width * 3;
-        for(i = 0; i < s->height; i++) {
-            for(j = 0; j < 3; j++) {
-                ls_decode_line(state, s, last + j, cur + j, Rc[j], width, 3, j);
-                Rc[j] = last[j];
-
-                if (s->restart_interval && !--s->restart_count) {
-                    align_get_bits(&s->gb);
-                    skip_bits(&s->gb, 16); /* skip RSTn */
-                }
-            }
-            last = cur;
-            cur += s->picture.linesize[0];
-        }
-    } else if(ilv == 2) { /* sample interleaving */
-        av_log(s->avctx, AV_LOG_ERROR, "Sample interleaved images are not supported.\n");
-        av_free(state);
-        av_free(zero);
-        return -1;
-    }
-
-    av_free(state);
-    av_free(zero);
-
-    return 0;
-}
-
-#if defined(CONFIG_ENCODERS) && defined(CONFIG_JPEGLS_ENCODER)
-/********** Encoder-specific functions **********/
-
-/**
- * Encode error from regular symbol
- */
-static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
-    int k;
-    int val;
-    int map;
-
-    for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
-
-    map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
-
-    if(err < 0)
-        err += state->range;
-    if(err >= ((state->range + 1) >> 1)) {
-        err -= state->range;
-        val = 2 * FFABS(err) - 1 - map;
-    } else
-        val = 2 * err + map;
-
-    set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
-
-    state->A[Q] += FFABS(err);
-    state->B[Q] += err * state->twonear;
-
-    if(state->N[Q] == state->reset) {
-        state->A[Q] >>= 1;
-        state->B[Q] >>= 1;
-        state->N[Q] >>= 1;
-    }
-    state->N[Q]++;
-
-    if(state->B[Q] <= -state->N[Q]) {
-        state->B[Q] += state->N[Q];
-        if(state->C[Q] > -128)
-            state->C[Q]--;
-        if(state->B[Q] <= -state->N[Q])
-            state->B[Q] = -state->N[Q] + 1;
-    }else if(state->B[Q] > 0){
-        state->B[Q] -= state->N[Q];
-        if(state->C[Q] < 127)
-            state->C[Q]++;
-        if(state->B[Q] > 0)
-            state->B[Q] = 0;
-    }
-}
-
-/**
- * Encode error from run termination
- */
-static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
-    int k;
-    int val, map;
-    int Q = 365 + RItype;
-    int temp;
-
-    temp = state->A[Q];
-    if(RItype)
-        temp += state->N[Q] >> 1;
-    for(k = 0; (state->N[Q] << k) < temp; k++);
-    map = 0;
-    if(!k && err && (2 * state->B[Q] < state->N[Q]))
-        map = 1;
-
-    if(err < 0)
-        val = - (2 * err) - 1 - RItype + map;
-    else
-        val = 2 * err - RItype - map;
-    set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
-
-    if(err < 0)
-        state->B[Q]++;
-    state->A[Q] += (val + 1 - RItype) >> 1;
-
-    if(state->N[Q] == state->reset) {
-        state->A[Q] >>= 1;
-        state->B[Q] >>= 1;
-        state->N[Q] >>= 1;
-    }
-    state->N[Q]++;
-}
-
-/**
- * Encode run value as specified by JPEG-LS standard
- */
-static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
-    while(run >= (1 << log2_run[state->run_index[comp]])){
-        put_bits(pb, 1, 1);
-        run -= 1 << log2_run[state->run_index[comp]];
-        if(state->run_index[comp] < 31)
-            state->run_index[comp]++;
-    }
-    /* if hit EOL, encode another full run, else encode aborted run */
-    if(!trail && run) {
-        put_bits(pb, 1, 1);
-    }else if(trail){
-        put_bits(pb, 1, 0);
-        if(log2_run[state->run_index[comp]])
-            put_bits(pb, log2_run[state->run_index[comp]], run);
-    }
-}
-
-/**
- * Encode one line of image
- */
-static inline void ls_encode_line(JLSState *state, PutBitContext *pb, uint8_t *last, uint8_t *cur, int last2, int w, int stride, int comp){
-    int x = 0;
-    int Ra, Rb, Rc, Rd;
-    int D0, D1, D2;
-
-    while(x < w) {
-        int err, pred, sign;
-
-        /* compute gradients */
-        Ra = x ? cur[x - stride] : last[x];
-        Rb = last[x];
-        Rc = x ? last[x - stride] : last2;
-        Rd = (x >= w - stride) ? last[x] : last[x + stride];
-        D0 = Rd - Rb;
-        D1 = Rb - Rc;
-        D2 = Rc - Ra;
-
-        /* run mode */
-        if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
-            int RUNval, RItype, run;
-
-            run = 0;
-            RUNval = Ra;
-            while(x < w && (FFABS(cur[x] - RUNval) <= state->near)){
-                run++;
-                cur[x] = Ra;
-                x += stride;
-            }
-            ls_encode_run(state, pb, run, comp, x < w);
-            if(x >= w)
-                return;
-            Rb = last[x];
-            RItype = (FFABS(Ra - Rb) <= state->near);
-            pred = RItype ? Ra : Rb;
-            err = cur[x] - pred;
-
-            if(!RItype && Ra > Rb)
-                err = -err;
-
-            if(state->near){
-                if(err > 0)
-                    err = (state->near + err) / state->twonear;
-                else
-                    err = -(state->near - err) / state->twonear;
-
-                if(RItype || (Rb >= Ra))
-                    Ra = clip(pred + err * state->twonear, 0, state->maxval);
-                else
-                    Ra = clip(pred - err * state->twonear, 0, state->maxval);
-                cur[x] = Ra;
-            }
-            if(err < 0)
-                err += state->range;
-            if(err >= ((state->range + 1) >> 1))
-                err -= state->range;
-
-            ls_encode_runterm(state, pb, RItype, err, log2_run[state->run_index[comp]]);
-
-            if(state->run_index[comp] > 0)
-                state->run_index[comp]--;
-            x += stride;
-        } else { /* regular mode */
-            int context;
-
-            context = quantize(state, D0) * 81 + quantize(state, D1) * 9 + quantize(state, D2);
-            pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
-
-            if(context < 0){
-                context = -context;
-                sign = 1;
-                pred = clip(pred - state->C[context], 0, state->maxval);
-                err = pred - cur[x];
-            }else{
-                sign = 0;
-                pred = clip(pred + state->C[context], 0, state->maxval);
-                err = cur[x] - pred;
-            }
-
-            if(state->near){
-                if(err > 0)
-                    err = (state->near + err) / state->twonear;
-                else
-                    err = -(state->near - err) / state->twonear;
-                if(!sign)
-                    Ra = clip(pred + err * state->twonear, 0, state->maxval);
-                else
-                    Ra = clip(pred - err * state->twonear, 0, state->maxval);
-                cur[x] = Ra;
-            }
-
-            ls_encode_regular(state, pb, context, err);
-            x += stride;
-        }
-    }
-}
-
-static void ls_store_lse(JLSState *state, PutBitContext *pb){
-    /* Test if we have default params and don't need to store LSE */
-    JLSState state2;
-    memset(&state2, 0, sizeof(JLSState));
-    state2.bpp = 8;
-    state2.near = state->near;
-    reset_ls_coding_parameters(&state2, 1);
-    if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
-        return;
-    /* store LSE type 1 */
-    put_marker(pb, LSE);
-    put_bits(pb, 16, 13);
-    put_bits(pb, 8,   1);
-    put_bits(pb, 16, state->maxval);
-    put_bits(pb, 16, state->T1);
-    put_bits(pb, 16, state->T2);
-    put_bits(pb, 16, state->T3);
-    put_bits(pb, 16, state->reset);
-}
-
-static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
-    JpeglsContext * const s = avctx->priv_data;
-    AVFrame *pict = data;
-    AVFrame * const p= (AVFrame*)&s->picture;
-    const int near = avctx->prediction_method;
-    PutBitContext pb, pb2;
-    GetBitContext gb;
-    uint8_t *buf2, *zero, *cur, *last;
-    JLSState *state;
-    int i, size;
-    int comps;
-
-    buf2 = av_malloc(buf_size);
-
-    init_put_bits(&pb, buf, buf_size);
-    init_put_bits(&pb2, buf2, buf_size);
-
-    *p = *pict;
-    p->pict_type= FF_I_TYPE;
-    p->key_frame= 1;
-
-    comps = (avctx->pix_fmt == PIX_FMT_GRAY8) ? 1 : 3;
-
-    /* write our own JPEG header, can't use mjpeg_picture_header */
-    put_marker(&pb, SOI);
-    put_marker(&pb, SOF48);
-    put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
-    put_bits(&pb,  8, 8);             // bpp
-    put_bits(&pb, 16, avctx->height);
-    put_bits(&pb, 16, avctx->width);
-    put_bits(&pb,  8, comps);         // components
-    for(i = 1; i <= comps; i++) {
-        put_bits(&pb,  8, i);    // component ID
-        put_bits(&pb,  8, 0x11); // subsampling: none
-        put_bits(&pb,  8, 0);    // Tiq, used by JPEG-LS ext
-    }
-
-    put_marker(&pb, SOS);
-    put_bits(&pb, 16, 6 + comps * 2);
-    put_bits(&pb,  8, comps);
-    for(i = 1; i <= comps; i++) {
-        put_bits(&pb,  8, i);  // component ID
-        put_bits(&pb,  8, 0);  // mapping index: none
-    }
-    put_bits(&pb,  8, near);
-    put_bits(&pb,  8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
-    put_bits(&pb,  8, 0); // point transform: none
-
-    state = av_mallocz(sizeof(JLSState));
-    /* initialize JPEG-LS state from JPEG parameters */
-    state->near = near;
-    state->bpp = 8;
-    reset_ls_coding_parameters(state, 0);
-    ls_init_state(state);
-
-    ls_store_lse(state, &pb);
-
-    zero = av_mallocz(p->linesize[0]);
-    last = zero;
-    cur = p->data[0];
-    if(avctx->pix_fmt == PIX_FMT_GRAY8){
-        int t = 0;
-
-        for(i = 0; i < avctx->height; i++) {
-            ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0);
-            t = last[0];
-            last = cur;
-            cur += p->linesize[0];
-        }
-    }else if(avctx->pix_fmt == PIX_FMT_RGB24){
-        int j, width;
-        int Rc[3] = {0, 0, 0};
-
-        width = avctx->width * 3;
-        for(i = 0; i < avctx->height; i++) {
-            for(j = 0; j < 3; j++) {
-                ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
-                Rc[j] = last[j];
-            }
-            last = cur;
-            cur += s->picture.linesize[0];
-        }
-    }else if(avctx->pix_fmt == PIX_FMT_BGR24){
-        int j, width;
-        int Rc[3] = {0, 0, 0};
-
-        width = avctx->width * 3;
-        for(i = 0; i < avctx->height; i++) {
-            for(j = 2; j >= 0; j--) {
-                ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j);
-                Rc[j] = last[j];
-            }
-            last = cur;
-            cur += s->picture.linesize[0];
-        }
-    }
-
-    av_free(zero);
-    av_free(state);
-
-    flush_put_bits(&pb2);
-    /* do escape coding */
-    size = put_bits_count(&pb2) >> 3;
-    init_get_bits(&gb, buf2, size);
-    while(get_bits_count(&gb) < size * 8){
-        int v;
-        v = get_bits(&gb, 8);
-        put_bits(&pb, 8, v);
-        if(v == 0xFF){
-            v = get_bits(&gb, 7);
-            put_bits(&pb, 8, v);
-        }
-    }
-    align_put_bits(&pb);
-    av_free(buf2);
-
-    /* End of image */
-    put_marker(&pb, EOI);
-    flush_put_bits(&pb);
-
-    emms_c();
-
-    return put_bits_count(&pb) >> 3;
-}
-
-static int encode_init_ls(AVCodecContext *ctx) {
-    JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
-
-    c->avctx = ctx;
-    ctx->coded_frame = &c->picture;
-
-    if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
-        av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
-        return -1;
-    }
-    return 0;
-}
-
-AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
-    "jpegls",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_JPEGLS,
-    sizeof(JpeglsContext),
-    encode_init_ls,
-    encode_picture_ls,
-    NULL,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, -1},
-};
-#endif
--- a/src/ffmpeg/libavcodec/libgsm.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-/*
- * Interface to libgsm for gsm encoding/decoding
- * Copyright (c) 2005 Alban Bedel <albeu@free.fr>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file libgsm.c
- * Interface to libgsm for gsm encoding/decoding
- */
-
-#include "avcodec.h"
-#include <gsm.h>
-
-// gsm.h miss some essential constants
-#define GSM_BLOCK_SIZE 33
-#define GSM_FRAME_SIZE 160
-
-static int libgsm_init(AVCodecContext *avctx) {
-    if (avctx->channels > 1 || avctx->sample_rate != 8000)
-        return -1;
-
-    avctx->frame_size = GSM_FRAME_SIZE;
-    avctx->block_align = GSM_BLOCK_SIZE;
-
-    avctx->priv_data = gsm_create();
-
-    avctx->coded_frame= avcodec_alloc_frame();
-    avctx->coded_frame->key_frame= 1;
-
-    return 0;
-}
-
-static int libgsm_close(AVCodecContext *avctx) {
-    gsm_destroy(avctx->priv_data);
-    avctx->priv_data = NULL;
-    return 0;
-}
-
-static int libgsm_encode_frame(AVCodecContext *avctx,
-                               unsigned char *frame, int buf_size, void *data) {
-    // we need a full block
-    if(buf_size < GSM_BLOCK_SIZE) return 0;
-
-    gsm_encode(avctx->priv_data,data,frame);
-
-    return GSM_BLOCK_SIZE;
-}
-
-
-AVCodec libgsm_encoder = {
-    "gsm",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_GSM,
-    0,
-    libgsm_init,
-    libgsm_encode_frame,
-    libgsm_close,
-};
-
-static int libgsm_decode_frame(AVCodecContext *avctx,
-                               void *data, int *data_size,
-                               uint8_t *buf, int buf_size) {
-
-    if(buf_size < GSM_BLOCK_SIZE) return 0;
-
-    if(gsm_decode(avctx->priv_data,buf,data)) return -1;
-
-    *data_size = GSM_FRAME_SIZE*2;
-    return GSM_BLOCK_SIZE;
-}
-
-AVCodec libgsm_decoder = {
-    "gsm",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_GSM,
-    0,
-    libgsm_init,
-    NULL,
-    libgsm_close,
-    libgsm_decode_frame,
-};
--- a/src/ffmpeg/libavcodec/mdec.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,270 +0,0 @@
-/*
- * PSX MDEC codec
- * Copyright (c) 2003 Michael Niedermayer
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * based upon code from Sebastian Jedruszkiewicz <elf@frogger.rules.pl>
- */
-
-/**
- * @file mdec.c
- * PSX MDEC codec.
- * This is very similar to intra only MPEG1.
- */
-
-#include "avcodec.h"
-#include "dsputil.h"
-#include "mpegvideo.h"
-
-//#undef NDEBUG
-//#include <assert.h>
-
-typedef struct MDECContext{
-    AVCodecContext *avctx;
-    DSPContext dsp;
-    AVFrame picture;
-    PutBitContext pb;
-    GetBitContext gb;
-    ScanTable scantable;
-    int version;
-    int qscale;
-    int last_dc[3];
-    int mb_width;
-    int mb_height;
-    int mb_x, mb_y;
-    DECLARE_ALIGNED_8(DCTELEM, block[6][64]);
-    DECLARE_ALIGNED_8(uint16_t, intra_matrix[64]);
-    DECLARE_ALIGNED_8(int, q_intra_matrix[64]);
-    uint8_t *bitstream_buffer;
-    unsigned int bitstream_buffer_size;
-    int block_last_index[6];
-} MDECContext;
-
-//very similar to mpeg1
-static inline int mdec_decode_block_intra(MDECContext *a, DCTELEM *block, int n)
-{
-    int level, diff, i, j, run;
-    int component;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= a->scantable.permutated;
-    const uint16_t *quant_matrix= ff_mpeg1_default_intra_matrix;
-    const int qscale= a->qscale;
-
-    /* DC coef */
-    if(a->version==2){
-        block[0]= 2*get_sbits(&a->gb, 10) + 1024;
-    }else{
-        component = (n <= 3 ? 0 : n - 4 + 1);
-        diff = decode_dc(&a->gb, component);
-        if (diff >= 0xffff)
-            return -1;
-        a->last_dc[component]+= diff;
-        block[0] = a->last_dc[component]<<3;
-    }
-
-    i = 0;
-    {
-        OPEN_READER(re, &a->gb);
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            UPDATE_CACHE(re, &a->gb);
-            GET_RL_VLC(level, run, re, &a->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level == 127){
-                break;
-            } else if(level != 0) {
-                i += run;
-                j = scantable[i];
-                level= (level*qscale*quant_matrix[j])>>3;
-//                level= (level-1)|1;
-                level = (level ^ SHOW_SBITS(re, &a->gb, 1)) - SHOW_SBITS(re, &a->gb, 1);
-                LAST_SKIP_BITS(re, &a->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &a->gb, 6)+1; LAST_SKIP_BITS(re, &a->gb, 6);
-                UPDATE_CACHE(re, &a->gb);
-                level = SHOW_SBITS(re, &a->gb, 10); SKIP_BITS(re, &a->gb, 10);
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= -level;
-                    level= (level*qscale*quant_matrix[j])>>3;
-                    level= (level-1)|1;
-                    level= -level;
-                }else{
-                    level= (level*qscale*quant_matrix[j])>>3;
-                    level= (level-1)|1;
-                }
-            }
-            if (i > 63){
-                av_log(a->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", a->mb_x, a->mb_y);
-                return -1;
-            }
-
-            block[j] = level;
-        }
-        CLOSE_READER(re, &a->gb);
-    }
-    a->block_last_index[n] = i;
-    return 0;
-}
-
-static inline int decode_mb(MDECContext *a, DCTELEM block[6][64]){
-    int i;
-    const int block_index[6]= {5,4,0,1,2,3};
-
-    a->dsp.clear_blocks(block[0]);
-
-    for(i=0; i<6; i++){
-        if( mdec_decode_block_intra(a, block[ block_index[i] ], block_index[i]) < 0)
-            return -1;
-    }
-    return 0;
-}
-
-static inline void idct_put(MDECContext *a, int mb_x, int mb_y){
-    DCTELEM (*block)[64]= a->block;
-    int linesize= a->picture.linesize[0];
-
-    uint8_t *dest_y  = a->picture.data[0] + (mb_y * 16* linesize              ) + mb_x * 16;
-    uint8_t *dest_cb = a->picture.data[1] + (mb_y * 8 * a->picture.linesize[1]) + mb_x * 8;
-    uint8_t *dest_cr = a->picture.data[2] + (mb_y * 8 * a->picture.linesize[2]) + mb_x * 8;
-
-    a->dsp.idct_put(dest_y                 , linesize, block[0]);
-    a->dsp.idct_put(dest_y              + 8, linesize, block[1]);
-    a->dsp.idct_put(dest_y + 8*linesize    , linesize, block[2]);
-    a->dsp.idct_put(dest_y + 8*linesize + 8, linesize, block[3]);
-
-    if(!(a->avctx->flags&CODEC_FLAG_GRAY)){
-        a->dsp.idct_put(dest_cb, a->picture.linesize[1], block[4]);
-        a->dsp.idct_put(dest_cr, a->picture.linesize[2], block[5]);
-    }
-}
-
-static int decode_frame(AVCodecContext *avctx,
-                        void *data, int *data_size,
-                        uint8_t *buf, int buf_size)
-{
-    MDECContext * const a = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame * const p= (AVFrame*)&a->picture;
-    int i;
-
-    if(p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference= 0;
-    if(avctx->get_buffer(avctx, p) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
-    p->pict_type= I_TYPE;
-    p->key_frame= 1;
-    a->last_dc[0]=
-    a->last_dc[1]=
-    a->last_dc[2]= 0;
-
-    a->bitstream_buffer= av_fast_realloc(a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    for(i=0; i<buf_size; i+=2){
-        a->bitstream_buffer[i]  = buf[i+1];
-        a->bitstream_buffer[i+1]= buf[i  ];
-    }
-    init_get_bits(&a->gb, a->bitstream_buffer, buf_size*8);
-
-    /* skip over 4 preamble bytes in stream (typically 0xXX 0xXX 0x00 0x38) */
-    skip_bits(&a->gb, 32);
-
-    a->qscale=  get_bits(&a->gb, 16);
-    a->version= get_bits(&a->gb, 16);
-
-//    printf("qscale:%d (0x%X), version:%d (0x%X)\n", a->qscale, a->qscale, a->version, a->version);
-
-    for(a->mb_x=0; a->mb_x<a->mb_width; a->mb_x++){
-        for(a->mb_y=0; a->mb_y<a->mb_height; a->mb_y++){
-            if( decode_mb(a, a->block) <0)
-                return -1;
-
-            idct_put(a, a->mb_x, a->mb_y);
-        }
-    }
-
-//    p->quality= (32 + a->inv_qscale/2)/a->inv_qscale;
-//    memset(p->qscale_table, p->quality, p->qstride*a->mb_height);
-
-    *picture= *(AVFrame*)&a->picture;
-    *data_size = sizeof(AVPicture);
-
-    emms_c();
-
-    return (get_bits_count(&a->gb)+31)/32*4;
-}
-
-static void mdec_common_init(AVCodecContext *avctx){
-    MDECContext * const a = avctx->priv_data;
-
-    dsputil_init(&a->dsp, avctx);
-
-    a->mb_width   = (avctx->coded_width  + 15) / 16;
-    a->mb_height  = (avctx->coded_height + 15) / 16;
-
-    avctx->coded_frame= (AVFrame*)&a->picture;
-    a->avctx= avctx;
-}
-
-static int decode_init(AVCodecContext *avctx){
-    MDECContext * const a = avctx->priv_data;
-    AVFrame *p= (AVFrame*)&a->picture;
-
-    mdec_common_init(avctx);
-    init_vlcs();
-    ff_init_scantable(a->dsp.idct_permutation, &a->scantable, ff_zigzag_direct);
-/*
-    for(i=0; i<64; i++){
-        int index= ff_zigzag_direct[i];
-        a->intra_matrix[i]= 64*ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
-    }
-*/
-    p->qstride= a->mb_width;
-    p->qscale_table= av_mallocz( p->qstride * a->mb_height);
-    avctx->pix_fmt= PIX_FMT_YUV420P;
-
-    return 0;
-}
-
-static int decode_end(AVCodecContext *avctx){
-    MDECContext * const a = avctx->priv_data;
-
-    av_freep(&a->bitstream_buffer);
-    av_freep(&a->picture.qscale_table);
-    a->bitstream_buffer_size=0;
-
-    return 0;
-}
-
-AVCodec mdec_decoder = {
-    "mdec",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MDEC,
-    sizeof(MDECContext),
-    decode_init,
-    NULL,
-    decode_end,
-    decode_frame,
-    CODEC_CAP_DR1,
-};
-
--- a/src/ffmpeg/libavcodec/motion_est.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2117 +0,0 @@
-/*
- * Motion estimation
- * Copyright (c) 2000,2001 Fabrice Bellard.
- * Copyright (c) 2002-2004 Michael Niedermayer
- *
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
- */
-
-/**
- * @file motion_est.c
- * Motion estimation.
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <limits.h>
-#include "avcodec.h"
-#include "dsputil.h"
-#include "mpegvideo.h"
-
-#undef NDEBUG
-#include <assert.h>
-
-#define SQ(a) ((a)*(a))
-
-#define P_LEFT P[1]
-#define P_TOP P[2]
-#define P_TOPRIGHT P[3]
-#define P_MEDIAN P[4]
-#define P_MV1 P[9]
-
-static inline int sad_hpel_motion_search(MpegEncContext * s,
-                                  int *mx_ptr, int *my_ptr, int dmin,
-                                  int src_index, int ref_index,
-                                  int size, int h);
-
-static inline int update_map_generation(MotionEstContext *c)
-{
-    c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
-    if(c->map_generation==0){
-        c->map_generation= 1<<(ME_MAP_MV_BITS*2);
-        memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
-    }
-    return c->map_generation;
-}
-
-/* shape adaptive search stuff */
-typedef struct Minima{
-    int height;
-    int x, y;
-    int checked;
-}Minima;
-
-static int minima_cmp(const void *a, const void *b){
-    const Minima *da = (const Minima *) a;
-    const Minima *db = (const Minima *) b;
-
-    return da->height - db->height;
-}
-
-#define FLAG_QPEL   1 //must be 1
-#define FLAG_CHROMA 2
-#define FLAG_DIRECT 4
-
-static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
-    const int offset[3]= {
-          y*c->  stride + x,
-        ((y*c->uvstride + x)>>1),
-        ((y*c->uvstride + x)>>1),
-    };
-    int i;
-    for(i=0; i<3; i++){
-        c->src[0][i]= src [i] + offset[i];
-        c->ref[0][i]= ref [i] + offset[i];
-    }
-    if(ref_index){
-        for(i=0; i<3; i++){
-            c->ref[ref_index][i]= ref2[i] + offset[i];
-        }
-    }
-}
-
-static int get_flags(MotionEstContext *c, int direct, int chroma){
-    return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
-           + (direct ? FLAG_DIRECT : 0)
-           + (chroma ? FLAG_CHROMA : 0);
-}
-
-static always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
-                      const int size, const int h, int ref_index, int src_index,
-                      me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
-    MotionEstContext * const c= &s->me;
-    const int stride= c->stride;
-    const int uvstride= c->uvstride;
-    const int qpel= flags&FLAG_QPEL;
-    const int chroma= flags&FLAG_CHROMA;
-    const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
-    const int hx= subx + (x<<(1+qpel));
-    const int hy= suby + (y<<(1+qpel));
-    uint8_t * const * const ref= c->ref[ref_index];
-    uint8_t * const * const src= c->src[src_index];
-    int d;
-    //FIXME check chroma 4mv, (no crashes ...)
-    if(flags&FLAG_DIRECT){
-        if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
-            const int time_pp= s->pp_time;
-            const int time_pb= s->pb_time;
-            const int mask= 2*qpel+1;
-            if(s->mv_type==MV_TYPE_8X8){
-                int i;
-                for(i=0; i<4; i++){
-                    int fx = c->direct_basis_mv[i][0] + hx;
-                    int fy = c->direct_basis_mv[i][1] + hy;
-                    int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
-                    int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
-                    int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
-                    int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
-
-                    uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
-                    if(qpel){
-                        c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
-                        c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
-                    }else{
-                        c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
-                        c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
-                    }
-                }
-            }else{
-                int fx = c->direct_basis_mv[0][0] + hx;
-                int fy = c->direct_basis_mv[0][1] + hy;
-                int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
-                int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
-                int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
-                int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
-
-                if(qpel){
-                    c->qpel_put[1][fxy](c->temp               , ref[0] + (fx>>2) + (fy>>2)*stride               , stride);
-                    c->qpel_put[1][fxy](c->temp + 8           , ref[0] + (fx>>2) + (fy>>2)*stride + 8           , stride);
-                    c->qpel_put[1][fxy](c->temp     + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride     + 8*stride, stride);
-                    c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
-                    c->qpel_avg[1][bxy](c->temp               , ref[8] + (bx>>2) + (by>>2)*stride               , stride);
-                    c->qpel_avg[1][bxy](c->temp + 8           , ref[8] + (bx>>2) + (by>>2)*stride + 8           , stride);
-                    c->qpel_avg[1][bxy](c->temp     + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride     + 8*stride, stride);
-                    c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
-                }else{
-                    assert((fx>>1) + 16*s->mb_x >= -16);
-                    assert((fy>>1) + 16*s->mb_y >= -16);
-                    assert((fx>>1) + 16*s->mb_x <= s->width);
-                    assert((fy>>1) + 16*s->mb_y <= s->height);
-                    assert((bx>>1) + 16*s->mb_x >= -16);
-                    assert((by>>1) + 16*s->mb_y >= -16);
-                    assert((bx>>1) + 16*s->mb_x <= s->width);
-                    assert((by>>1) + 16*s->mb_y <= s->height);
-
-                    c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
-                    c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
-                }
-            }
-            d = cmp_func(s, c->temp, src[0], stride, 16);
-        }else
-            d= 256*256*256*32;
-    }else{
-        int uvdxy;              /* no, it might not be used uninitialized */
-        if(dxy){
-            if(qpel){
-                c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
-                if(chroma){
-                    int cx= hx/2;
-                    int cy= hy/2;
-                    cx= (cx>>1)|(cx&1);
-                    cy= (cy>>1)|(cy&1);
-                    uvdxy= (cx&1) + 2*(cy&1);
-                    //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
-                }
-            }else{
-                c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
-                if(chroma)
-                    uvdxy= dxy | (x&1) | (2*(y&1));
-            }
-            d = cmp_func(s, c->temp, src[0], stride, h);
-        }else{
-            d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
-            if(chroma)
-                uvdxy= (x&1) + 2*(y&1);
-        }
-        if(chroma){
-            uint8_t * const uvtemp= c->temp + 16*stride;
-            c->hpel_put[size+1][uvdxy](uvtemp  , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
-            c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
-            d += chroma_cmp_func(s, uvtemp  , src[1], uvstride, h>>1);
-            d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
-        }
-    }
-#if 0
-    if(full_pel){
-        const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
-        score_map[index]= d;
-    }
-
-    d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;
-#endif
-    return d;
-}
-
-#include "motion_est_template.c"
-
-static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
-    return 0;
-}
-
-static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
-}
-
-void ff_init_me(MpegEncContext *s){
-    MotionEstContext * const c= &s->me;
-    c->avctx= s->avctx;
-
-    ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
-    ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
-
-    c->flags    = get_flags(c, 0, c->avctx->me_cmp    &FF_CMP_CHROMA);
-    c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
-    c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
-
-/*FIXME s->no_rounding b_type*/
-    if(s->flags&CODEC_FLAG_QPEL){
-        c->sub_motion_search= qpel_motion_search;
-        c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
-        if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
-        else               c->qpel_put= s->dsp.put_qpel_pixels_tab;
-    }else{
-        if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
-            c->sub_motion_search= hpel_motion_search;
-        else if(   c->avctx->me_sub_cmp == FF_CMP_SAD
-                && c->avctx->    me_cmp == FF_CMP_SAD
-                && c->avctx->    mb_cmp == FF_CMP_SAD)
-            c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
-        else
-            c->sub_motion_search= hpel_motion_search;
-    }
-    c->hpel_avg= s->dsp.avg_pixels_tab;
-    if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
-    else               c->hpel_put= s->dsp.put_pixels_tab;
-
-    if(s->linesize){
-        c->stride  = s->linesize;
-        c->uvstride= s->uvlinesize;
-    }else{
-        c->stride  = 16*s->mb_width + 32;
-        c->uvstride=  8*s->mb_width + 16;
-    }
-
-    // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
-    if(s->codec_id != CODEC_ID_SNOW){
-        if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
-            s->dsp.me_cmp[2]= zero_cmp;
-        }
-        if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
-            s->dsp.me_sub_cmp[2]= zero_cmp;
-        }
-        c->hpel_put[2][0]= c->hpel_put[2][1]=
-        c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
-    }
-
-    if(s->codec_id == CODEC_ID_H261){
-        c->sub_motion_search= no_sub_motion_search;
-    }
-
-    c->temp= c->scratchpad;
-}
-
-#if 0
-static int pix_dev(uint8_t * pix, int line_size, int mean)
-{
-    int s, i, j;
-
-    s = 0;
-    for (i = 0; i < 16; i++) {
-        for (j = 0; j < 16; j += 8) {
-            s += FFABS(pix[0]-mean);
-            s += FFABS(pix[1]-mean);
-            s += FFABS(pix[2]-mean);
-            s += FFABS(pix[3]-mean);
-            s += FFABS(pix[4]-mean);
-            s += FFABS(pix[5]-mean);
-            s += FFABS(pix[6]-mean);
-            s += FFABS(pix[7]-mean);
-            pix += 8;
-        }
-        pix += line_size - 16;
-    }
-    return s;
-}
-#endif
-
-static inline void no_motion_search(MpegEncContext * s,
-                                    int *mx_ptr, int *my_ptr)
-{
-    *mx_ptr = 16 * s->mb_x;
-    *my_ptr = 16 * s->mb_y;
-}
-
-#if 0  /* the use of these functions is inside #if 0 */
-static int full_motion_search(MpegEncContext * s,
-                              int *mx_ptr, int *my_ptr, int range,
-                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y;
-    int mx, my, dmin, d;
-    uint8_t *pix;
-
-    xx = 16 * s->mb_x;
-    yy = 16 * s->mb_y;
-    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
-    if (x1 < xmin)
-        x1 = xmin;
-    x2 = xx + range - 1;
-    if (x2 > xmax)
-        x2 = xmax;
-    y1 = yy - range + 1;
-    if (y1 < ymin)
-        y1 = ymin;
-    y2 = yy + range - 1;
-    if (y2 > ymax)
-        y2 = ymax;
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    dmin = 0x7fffffff;
-    mx = 0;
-    my = 0;
-    for (y = y1; y <= y2; y++) {
-        for (x = x1; x <= x2; x++) {
-            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;
-                my = y;
-            }
-        }
-    }
-
-    *mx_ptr = mx;
-    *my_ptr = my;
-
-#if 0
-    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
-        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
-        av_log(NULL, AV_LOG_ERROR, "error %d %d\n", *mx_ptr, *my_ptr);
-    }
-#endif
-    return dmin;
-}
-
-
-static int log_motion_search(MpegEncContext * s,
-                             int *mx_ptr, int *my_ptr, int range,
-                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y;
-    int mx, my, dmin, d;
-    uint8_t *pix;
-
-    xx = s->mb_x << 4;
-    yy = s->mb_y << 4;
-
-    /* Left limit */
-    x1 = xx - range;
-    if (x1 < xmin)
-        x1 = xmin;
-
-    /* Right limit */
-    x2 = xx + range;
-    if (x2 > xmax)
-        x2 = xmax;
-
-    /* Upper limit */
-    y1 = yy - range;
-    if (y1 < ymin)
-        y1 = ymin;
-
-    /* Lower limit */
-    y2 = yy + range;
-    if (y2 > ymax)
-        y2 = ymax;
-
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    dmin = 0x7fffffff;
-    mx = 0;
-    my = 0;
-
-    do {
-        for (y = y1; y <= y2; y += range) {
-            for (x = x1; x <= x2; x += range) {
-                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;
-                    my = y;
-                }
-            }
-        }
-
-        range = range >> 1;
-
-        x1 = mx - range;
-        if (x1 < xmin)
-            x1 = xmin;
-
-        x2 = mx + range;
-        if (x2 > xmax)
-            x2 = xmax;
-
-        y1 = my - range;
-        if (y1 < ymin)
-            y1 = ymin;
-
-        y2 = my + range;
-        if (y2 > ymax)
-            y2 = ymax;
-
-    } while (range >= 1);
-
-#ifdef DEBUG
-    av_log(s->avctx, AV_LOG_DEBUG, "log       - MX: %d\tMY: %d\n", mx, my);
-#endif
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return dmin;
-}
-
-static int phods_motion_search(MpegEncContext * s,
-                               int *mx_ptr, int *my_ptr, int range,
-                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
-    int mx, my, dminx, dminy;
-    uint8_t *pix;
-
-    xx = s->mb_x << 4;
-    yy = s->mb_y << 4;
-
-    /* Left limit */
-    x1 = xx - range;
-    if (x1 < xmin)
-        x1 = xmin;
-
-    /* Right limit */
-    x2 = xx + range;
-    if (x2 > xmax)
-        x2 = xmax;
-
-    /* Upper limit */
-    y1 = yy - range;
-    if (y1 < ymin)
-        y1 = ymin;
-
-    /* Lower limit */
-    y2 = yy + range;
-    if (y2 > ymax)
-        y2 = ymax;
-
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    mx = 0;
-    my = 0;
-
-    x = xx;
-    y = yy;
-    do {
-        dminx = 0x7fffffff;
-        dminy = 0x7fffffff;
-
-        lastx = x;
-        for (x = x1; x <= x2; x += range) {
-            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;
-            }
-        }
-
-        x = lastx;
-        for (y = y1; y <= y2; y += range) {
-            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;
-            }
-        }
-
-        range = range >> 1;
-
-        x = mx;
-        y = my;
-        x1 = mx - range;
-        if (x1 < xmin)
-            x1 = xmin;
-
-        x2 = mx + range;
-        if (x2 > xmax)
-            x2 = xmax;
-
-        y1 = my - range;
-        if (y1 < ymin)
-            y1 = ymin;
-
-        y2 = my + range;
-        if (y2 > ymax)
-            y2 = ymax;
-
-    } while (range >= 1);
-
-#ifdef DEBUG
-    av_log(s->avctx, AV_LOG_DEBUG, "phods     - MX: %d\tMY: %d\n", mx, my);
-#endif
-
-    /* half pixel search */
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return dminy;
-}
-#endif /* 0 */
-
-#define Z_THRESHOLD 256
-
-#define CHECK_SAD_HALF_MV(suffix, x, y) \
-{\
-    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 src_index, int ref_index,
-                                  int size, int h)
-{
-    MotionEstContext * const c= &s->me;
-    const int penalty_factor= c->sub_penalty_factor;
-    int mx, my, dminh;
-    uint8_t *pix, *ptr;
-    int stride= c->stride;
-    const int flags= c->sub_flags;
-    LOAD_COMMON
-
-    assert(flags == 0);
-
-    if(c->skip){
-//    printf("S");
-        *mx_ptr = 0;
-        *my_ptr = 0;
-        return dmin;
-    }
-//    printf("N");
-
-    pix = c->src[src_index][0];
-
-    mx = *mx_ptr;
-    my = *my_ptr;
-    ptr = c->ref[ref_index][0] + (my * stride) + mx;
-
-    dminh = dmin;
-
-    if (mx > xmin && mx < xmax &&
-        my > ymin && my < ymax) {
-        int dx=0, dy=0;
-        int d, pen_x, pen_y;
-        const int index= (my<<ME_MAP_SHIFT) + mx;
-        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
-        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)];
-        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)];
-        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
-        mx<<=1;
-        my<<=1;
-
-
-        pen_x= pred_x + mx;
-        pen_y= pred_y + my;
-
-        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+= stride;
-                }else{
-                    ptr+= stride;
-                    CHECK_SAD_HALF_MV(xy2, -1, +1)
-                }
-                CHECK_SAD_HALF_MV(x2 , -1,  0)
-            }else{
-                CHECK_SAD_HALF_MV(xy2, +1, -1)
-                if(t+l<=b+r){
-                    CHECK_SAD_HALF_MV(xy2, -1, -1)
-                    ptr+= stride;
-                }else{
-                    ptr+= stride;
-                    CHECK_SAD_HALF_MV(xy2, +1, +1)
-                }
-                CHECK_SAD_HALF_MV(x2 , +1,  0)
-            }
-        }else{
-            if(l<=r){
-                if(t+l<=b+r){
-                    CHECK_SAD_HALF_MV(xy2, -1, -1)
-                    ptr+= stride;
-                }else{
-                    ptr+= stride;
-                    CHECK_SAD_HALF_MV(xy2, +1, +1)
-                }
-                CHECK_SAD_HALF_MV(x2 , -1,  0)
-                CHECK_SAD_HALF_MV(xy2, -1, +1)
-            }else{
-                if(t+r<=b+l){
-                    CHECK_SAD_HALF_MV(xy2, +1, -1)
-                    ptr+= stride;
-                }else{
-                    ptr+= stride;
-                    CHECK_SAD_HALF_MV(xy2, -1, +1)
-                }
-                CHECK_SAD_HALF_MV(x2 , +1,  0)
-                CHECK_SAD_HALF_MV(xy2, +1, +1)
-            }
-            CHECK_SAD_HALF_MV(y2 ,  0, +1)
-        }
-        mx+=dx;
-        my+=dy;
-
-    }else{
-        mx<<=1;
-        my<<=1;
-    }
-
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return dminh;
-}
-
-static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
-{
-    const int xy= s->mb_x + s->mb_y*s->mb_stride;
-
-    s->p_mv_table[xy][0] = mx;
-    s->p_mv_table[xy][1] = my;
-
-    /* has already been set to the 4 MV if 4MV is done */
-    if(mv4){
-        int mot_xy= s->block_index[0];
-
-        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
-        s->current_picture.motion_val[0][mot_xy  ][1]= my;
-        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
-        s->current_picture.motion_val[0][mot_xy+1][1]= my;
-
-        mot_xy += s->b8_stride;
-        s->current_picture.motion_val[0][mot_xy  ][0]= mx;
-        s->current_picture.motion_val[0][mot_xy  ][1]= my;
-        s->current_picture.motion_val[0][mot_xy+1][0]= mx;
-        s->current_picture.motion_val[0][mot_xy+1][1]= my;
-    }
-}
-
-/**
- * get fullpel ME search limits.
- */
-static inline void get_limits(MpegEncContext *s, int x, int y)
-{
-    MotionEstContext * const c= &s->me;
-/*
-    if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
-    else                   c->range= 16;
-*/
-    if (s->unrestricted_mv) {
-        c->xmin = - x - 16;
-        c->ymin = - y - 16;
-        c->xmax = - x + s->mb_width *16;
-        c->ymax = - y + s->mb_height*16;
-    } else if (s->out_format == FMT_H261){
-        // Search range of H261 is different from other codec standards
-        c->xmin = (x > 15) ? - 15 : 0;
-        c->ymin = (y > 15) ? - 15 : 0;
-        c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
-        c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
-    } else {
-        c->xmin = - x;
-        c->ymin = - y;
-        c->xmax = - x + s->mb_width *16 - 16;
-        c->ymax = - y + s->mb_height*16 - 16;
-    }
-}
-
-static inline void init_mv4_ref(MotionEstContext *c){
-    const int stride= c->stride;
-
-    c->ref[1][0] = c->ref[0][0] + 8;
-    c->ref[2][0] = c->ref[0][0] + 8*stride;
-    c->ref[3][0] = c->ref[2][0] + 8;
-    c->src[1][0] = c->src[0][0] + 8;
-    c->src[2][0] = c->src[0][0] + 8*stride;
-    c->src[3][0] = c->src[2][0] + 8;
-}
-
-static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
-{
-    MotionEstContext * const c= &s->me;
-    const int size= 1;
-    const int h=8;
-    int block;
-    int P[10][2];
-    int dmin_sum=0, mx4_sum=0, my4_sum=0;
-    int same=1;
-    const int stride= c->stride;
-    uint8_t *mv_penalty= c->current_mv_penalty;
-
-    init_mv4_ref(c);
-
-    for(block=0; block<4; block++){
-        int mx4, my4;
-        int pred_x4, pred_y4;
-        int dmin4;
-        static const int off[4]= {2, 1, 1, -1};
-        const int mot_stride = s->b8_stride;
-        const int mot_xy = s->block_index[block];
-
-        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]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
-
-        /* special case for first line */
-        if (s->first_slice_line && block<2) {
-            c->pred_x= pred_x4= P_LEFT[0];
-            c->pred_y= pred_y4= P_LEFT[1];
-        } else {
-            P_TOP[0]      = s->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 + off[block]][0];
-            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
-            if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
-            if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
-            if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
-            if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
-
-            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]);
-
-            c->pred_x= pred_x4 = P_MEDIAN[0];
-            c->pred_y= pred_y4 = P_MEDIAN[1];
-        }
-        P_MV1[0]= mx;
-        P_MV1[1]= my;
-
-        dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
-
-        dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
-
-        if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
-            int dxy;
-            const int offset= ((block&1) + (block>>1)*stride)*8;
-            uint8_t *dest_y = c->scratchpad + offset;
-            if(s->quarter_sample){
-                uint8_t *ref= c->ref[block][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    , stride);
-                else
-                    s->dsp.put_qpel_pixels_tab       [1][dxy](dest_y   , ref    , stride);
-            }else{
-                uint8_t *ref= c->ref[block][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    , stride, h);
-                else
-                    s->dsp.put_pixels_tab       [1][dxy](dest_y    , ref    , stride, h);
-            }
-            dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
-        }else
-            dmin_sum+= dmin4;
-
-        if(s->quarter_sample){
-            mx4_sum+= mx4/2;
-            my4_sum+= my4/2;
-        }else{
-            mx4_sum+= mx4;
-            my4_sum+= my4;
-        }
-
-        s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
-        s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
-
-        if(mx4 != mx || my4 != my) same=0;
-    }
-
-    if(same)
-        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*stride, c->scratchpad, stride, 16);
-    }
-
-    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
-        int dxy;
-        int mx, my;
-        int offset;
-
-        mx= ff_h263_round_chroma(mx4_sum);
-        my= ff_h263_round_chroma(my4_sum);
-        dxy = ((my & 1) << 1) | (mx & 1);
-
-        offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
-
-        if(s->no_rounding){
-            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8  , s->last_picture.data[2] + offset, s->uvlinesize, 8);
-        }else{
-            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_pixels_tab       [1][dxy](c->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, c->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, c->scratchpad+8, s->uvlinesize, 8);
-    }
-
-    c->pred_x= mx;
-    c->pred_y= my;
-
-    switch(c->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*c->mb_penalty_factor;
-    }
-}
-
-static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
-    MotionEstContext * const c= &s->me;
-
-    c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
-    c->src[1][0] = c->src[0][0] + s->linesize;
-    if(c->flags & FLAG_CHROMA){
-        c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
-        c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
-        c->src[1][1] = c->src[0][1] + s->uvlinesize;
-        c->src[1][2] = c->src[0][2] + s->uvlinesize;
-    }
-}
-
-static int interlaced_search(MpegEncContext *s, int ref_index,
-                             int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
-{
-    MotionEstContext * const c= &s->me;
-    const int size=0;
-    const int h=8;
-    int block;
-    int P[10][2];
-    uint8_t * const mv_penalty= c->current_mv_penalty;
-    int same=1;
-    const int stride= 2*s->linesize;
-    int dmin_sum= 0;
-    const int mot_stride= s->mb_stride;
-    const int xy= s->mb_x + s->mb_y*mot_stride;
-
-    c->ymin>>=1;
-    c->ymax>>=1;
-    c->stride<<=1;
-    c->uvstride<<=1;
-    init_interlaced_ref(s, ref_index);
-
-    for(block=0; block<2; block++){
-        int field_select;
-        int best_dmin= INT_MAX;
-        int best_field= -1;
-
-        for(field_select=0; field_select<2; field_select++){
-            int dmin, mx_i, my_i;
-            int16_t (*mv_table)[2]= mv_tables[block][field_select];
-
-            if(user_field_select){
-                assert(field_select==0 || field_select==1);
-                assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
-                if(field_select_tables[block][xy] != field_select)
-                    continue;
-            }
-
-            P_LEFT[0] = mv_table[xy - 1][0];
-            P_LEFT[1] = mv_table[xy - 1][1];
-            if(P_LEFT[0]       > (c->xmax<<1)) P_LEFT[0]       = (c->xmax<<1);
-
-            c->pred_x= P_LEFT[0];
-            c->pred_y= P_LEFT[1];
-
-            if(!s->first_slice_line){
-                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]      > (c->ymax<<1)) P_TOP[1]     = (c->ymax<<1);
-                if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
-                if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
-                if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->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 = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
-
-            dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
-
-            mv_table[xy][0]= mx_i;
-            mv_table[xy][1]= my_i;
-
-            if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
-                int dxy;
-
-                //FIXME chroma ME
-                uint8_t *ref= c->ref[field_select+ref_index][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](c->scratchpad, ref    , stride, h);
-                }else{
-                    s->dsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
-                }
-                dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
-                dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
-            }else
-                dmin+= c->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;
-    }
-
-    c->ymin<<=1;
-    c->ymax<<=1;
-    c->stride>>=1;
-    c->uvstride>>=1;
-
-    if(same)
-        return INT_MAX;
-
-    switch(c->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*c->mb_penalty_factor;
-    }
-}
-
-static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
-    int ymax= s->me.ymax>>interlaced;
-    int ymin= s->me.ymin>>interlaced;
-
-    if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
-    if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
-    if(mv[1] <       ymin) mv[1] =       ymin;
-    if(mv[1] >       ymax) mv[1] =       ymax;
-}
-
-static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
-    MotionEstContext * const c= &s->me;
-    Picture *p= s->current_picture_ptr;
-    int mb_xy= mb_x + mb_y*s->mb_stride;
-    int xy= 2*mb_x + 2*mb_y*s->b8_stride;
-    int mb_type= s->current_picture.mb_type[mb_xy];
-    int flags= c->flags;
-    int shift= (flags&FLAG_QPEL) + 1;
-    int mask= (1<<shift)-1;
-    int x, y, i;
-    int d=0;
-    me_cmp_func cmpf= s->dsp.sse[0];
-    me_cmp_func chroma_cmpf= s->dsp.sse[1];
-
-    if(p_type && USES_LIST(mb_type, 1)){
-        av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
-        return INT_MAX/2;
-    }
-    assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
-
-    for(i=0; i<4; i++){
-        int xy= s->block_index[i];
-        clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
-        clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
-    }
-
-    if(IS_INTERLACED(mb_type)){
-        int xy2= xy  + s->b8_stride;
-        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
-        c->stride<<=1;
-        c->uvstride<<=1;
-
-        if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
-            av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
-            return INT_MAX/2;
-        }
-
-        if(USES_LIST(mb_type, 0)){
-            int field_select0= p->ref_index[0][xy ];
-            int field_select1= p->ref_index[0][xy2];
-            assert(field_select0==0 ||field_select0==1);
-            assert(field_select1==0 ||field_select1==1);
-            init_interlaced_ref(s, 0);
-
-            if(p_type){
-                s->p_field_select_table[0][mb_xy]= field_select0;
-                s->p_field_select_table[1][mb_xy]= field_select1;
-                *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
-                *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
-                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
-            }else{
-                s->b_field_select_table[0][0][mb_xy]= field_select0;
-                s->b_field_select_table[0][1][mb_xy]= field_select1;
-                *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
-                *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
-                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
-            }
-
-            x= p->motion_val[0][xy ][0];
-            y= p->motion_val[0][xy ][1];
-            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
-            x= p->motion_val[0][xy2][0];
-            y= p->motion_val[0][xy2][1];
-            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
-        }
-        if(USES_LIST(mb_type, 1)){
-            int field_select0= p->ref_index[1][xy ];
-            int field_select1= p->ref_index[1][xy2];
-            assert(field_select0==0 ||field_select0==1);
-            assert(field_select1==0 ||field_select1==1);
-            init_interlaced_ref(s, 2);
-
-            s->b_field_select_table[1][0][mb_xy]= field_select0;
-            s->b_field_select_table[1][1][mb_xy]= field_select1;
-            *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
-            *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
-            if(USES_LIST(mb_type, 0)){
-                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
-            }else{
-                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
-            }
-
-            x= p->motion_val[1][xy ][0];
-            y= p->motion_val[1][xy ][1];
-            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
-            x= p->motion_val[1][xy2][0];
-            y= p->motion_val[1][xy2][1];
-            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
-            //FIXME bidir scores
-        }
-        c->stride>>=1;
-        c->uvstride>>=1;
-    }else if(IS_8X8(mb_type)){
-        if(!(s->flags & CODEC_FLAG_4MV)){
-            av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
-            return INT_MAX/2;
-        }
-        cmpf= s->dsp.sse[1];
-        chroma_cmpf= s->dsp.sse[1];
-        init_mv4_ref(c);
-        for(i=0; i<4; i++){
-            xy= s->block_index[i];
-            x= p->motion_val[0][xy][0];
-            y= p->motion_val[0][xy][1];
-            d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
-        }
-        s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
-    }else{
-        if(USES_LIST(mb_type, 0)){
-            if(p_type){
-                *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
-                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
-            }else if(USES_LIST(mb_type, 1)){
-                *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
-                *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
-                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
-            }else{
-                *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
-                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
-            }
-            x= p->motion_val[0][xy][0];
-            y= p->motion_val[0][xy][1];
-            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
-        }else if(USES_LIST(mb_type, 1)){
-            *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
-            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
-
-            x= p->motion_val[1][xy][0];
-            y= p->motion_val[1][xy][1];
-            d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
-        }else
-            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
-    }
-    return d;
-}
-
-void ff_estimate_p_frame_motion(MpegEncContext * s,
-                                int mb_x, int mb_y)
-{
-    MotionEstContext * const c= &s->me;
-    uint8_t *pix, *ppix;
-    int sum, varc, vard, mx, my, dmin;
-    int P[10][2];
-    const int shift= 1+s->quarter_sample;
-    int mb_type=0;
-    Picture * const pic= &s->current_picture;
-
-    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
-
-    assert(s->quarter_sample==0 || s->quarter_sample==1);
-    assert(s->linesize == c->stride);
-    assert(s->uvlinesize == c->uvstride);
-
-    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
-    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
-    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
-    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
-
-    get_limits(s, 16*mb_x, 16*mb_y);
-    c->skip=0;
-
-    /* intra / predictive decision */
-    pix = c->src[0][0];
-    sum = s->dsp.pix_sum(pix, s->linesize);
-    varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
-
-    pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
-    pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
-    c->mb_var_sum_temp += (varc+128)>>8;
-
-    if(c->avctx->me_threshold){
-        vard= check_input_motion(s, mb_x, mb_y, 1);
-
-        if((vard+128)>>8 < c->avctx->me_threshold){
-            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
-            c->mc_mb_var_sum_temp += (vard+128)>>8;
-            if (vard <= 64<<8 || vard < varc) { //FIXME
-                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
-            }else{
-                c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
-            }
-            return;
-        }
-        if((vard+128)>>8 < c->avctx->mb_threshold)
-            mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
-    }
-
-    switch(s->me_method) {
-    case ME_ZERO:
-    default:
-        no_motion_search(s, &mx, &my);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        dmin = 0;
-        break;
-#if 0
-    case ME_FULL:
-        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, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_PHODS:
-        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:
-       {
-            const int mot_stride = s->b8_stride;
-            const int mot_xy = s->block_index[0];
-
-            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]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
-
-            if(!s->first_slice_line) {
-                P_TOP[0]      = s->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]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
-                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
-                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
-
-                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]);
-
-                if(s->out_format == FMT_H263){
-                    c->pred_x = P_MEDIAN[0];
-                    c->pred_y = P_MEDIAN[1];
-                }else { /* mpeg1 at least */
-                    c->pred_x= P_LEFT[0];
-                    c->pred_y= P_LEFT[1];
-                }
-            }else{
-                c->pred_x= P_LEFT[0];
-                c->pred_y= P_LEFT[1];
-            }
-
-        }
-        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
-
-        break;
-    }
-
-    /* At this point (mx,my) are full-pell and the relative displacement */
-    ppix = c->ref[0][0] + (my * s->linesize) + mx;
-
-    vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
-
-    pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
-//    pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
-    c->mc_mb_var_sum_temp += (vard+128)>>8;
-
-#if 0
-    printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
-           varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
-#endif
-    if(mb_type){
-        if (vard <= 64<<8 || vard < varc)
-            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
-        else
-            c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
-
-        if(mb_type == CANDIDATE_MB_TYPE_INTER){
-            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-            set_p_mv_tables(s, mx, my, 1);
-        }else{
-            mx <<=shift;
-            my <<=shift;
-        }
-        if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
-            h263_mv4_search(s, mx, my, shift);
-
-            set_p_mv_tables(s, mx, my, 0);
-        }
-        if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
-            interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
-        }
-    }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
-        if (vard <= 64<<8 || vard < varc)
-            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
-        else
-            c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
-
-        if (vard*2 + 200*256 > varc)
-            mb_type|= CANDIDATE_MB_TYPE_INTRA;
-        if (varc*2 + 200*256 > vard){
-            mb_type|= CANDIDATE_MB_TYPE_INTER;
-            c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-            if(s->flags&CODEC_FLAG_MV0)
-                if(mx || my)
-                    mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
-        }else{
-            mx <<=shift;
-            my <<=shift;
-        }
-        if((s->flags&CODEC_FLAG_4MV)
-           && !c->skip && varc>50<<8 && vard>10<<8){
-            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)
-           && !c->skip){ //FIXME varc/d checks
-            if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
-                mb_type |= CANDIDATE_MB_TYPE_INTER_I;
-        }
-    }else{
-        int intra_score, i;
-        mb_type= CANDIDATE_MB_TYPE_INTER;
-
-        dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-        if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
-            dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
-
-        if((s->flags&CODEC_FLAG_4MV)
-           && !c->skip && varc>50<<8 && vard>10<<8){
-            int dmin4= h263_mv4_search(s, mx, my, shift);
-            if(dmin4 < dmin){
-                mb_type= CANDIDATE_MB_TYPE_INTER4V;
-                dmin=dmin4;
-            }
-        }
-        if((s->flags&CODEC_FLAG_INTERLACED_ME)
-           && !c->skip){ //FIXME varc/d checks
-            int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
-            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!=CANDIDATE_MB_TYPE_INTER4V);
-
-        /* get intra luma score */
-        if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
-            intra_score= varc - 500;
-        }else{
-            int mean= (sum+128)>>8;
-            mean*= 0x01010101;
-
-            for(i=0; i<16; i++){
-                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
-                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
-                *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
-                *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
-            }
-
-            intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
-        }
-#if 0 //FIXME
-        /* get chroma score */
-        if(c->avctx->mb_cmp&FF_CMP_CHROMA){
-            for(i=1; i<3; i++){
-                uint8_t *dest_c;
-                int mean;
-
-                if(s->out_format == FMT_H263){
-                    mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
-                }else{
-                    mean= (s->last_dc[i] + 4)>>3;
-                }
-                dest_c = s->new_picture.data[i] + (mb_y * 8  * (s->uvlinesize)) + mb_x * 8;
-
-                mean*= 0x01010101;
-                for(i=0; i<8; i++){
-                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
-                    *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
-                }
-
-                intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
-            }
-        }
-#endif
-        intra_score += c->mb_penalty_factor*16;
-
-        if(intra_score < dmin){
-            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;
-
-        if (vard <= 64<<8 || vard < varc) { //FIXME
-            c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
-        }else{
-            c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
-        }
-    }
-
-    s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
-}
-
-int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
-                                    int mb_x, int mb_y)
-{
-    MotionEstContext * const c= &s->me;
-    int mx, my, dmin;
-    int P[10][2];
-    const int shift= 1+s->quarter_sample;
-    const int xy= mb_x + mb_y*s->mb_stride;
-    init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
-
-    assert(s->quarter_sample==0 || s->quarter_sample==1);
-
-    c->pre_penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
-    c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
-
-    get_limits(s, 16*mb_x, 16*mb_y);
-    c->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]       < (c->xmin<<shift)) P_LEFT[0]       = (c->xmin<<shift);
-
-    /* special case for first line */
-    if (s->first_slice_line) {
-        c->pred_x= P_LEFT[0];
-        c->pred_y= P_LEFT[1];
-        P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
-        P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
-    } else {
-        P_TOP[0]      = s->p_mv_table[xy + s->mb_stride    ][0];
-        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]      < (c->ymin<<shift)) P_TOP[1]     = (c->ymin<<shift);
-        if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
-        if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
-
-        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]);
-
-        c->pred_x = P_MEDIAN[0];
-        c->pred_y = P_MEDIAN[1];
-    }
-
-    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
-
-    s->p_mv_table[xy][0] = mx<<shift;
-    s->p_mv_table[xy][1] = my<<shift;
-
-    return dmin;
-}
-
-static int ff_estimate_motion_b(MpegEncContext * s,
-                       int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
-{
-    MotionEstContext * const c= &s->me;
-    int mx, my, dmin;
-    int P[10][2];
-    const int shift= 1+s->quarter_sample;
-    const int mot_stride = s->mb_stride;
-    const int mot_xy = mb_y*mot_stride + mb_x;
-    uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
-    int mv_scale;
-
-    c->penalty_factor    = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
-    c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
-    c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
-    c->current_mv_penalty= mv_penalty;
-
-    get_limits(s, 16*mb_x, 16*mb_y);
-
-    switch(s->me_method) {
-    case ME_ZERO:
-    default:
-        no_motion_search(s, &mx, &my);
-        dmin = 0;
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-#if 0
-    case ME_FULL:
-        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, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_PHODS:
-        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]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
-
-            /* special case for first line */
-            if (!s->first_slice_line) {
-                P_TOP[0] = mv_table[mot_xy - mot_stride             ][0];
-                P_TOP[1] = mv_table[mot_xy - mot_stride             ][1];
-                P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1         ][0];
-                P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1         ][1];
-                if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
-                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
-                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
-
-                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]);
-            }
-            c->pred_x= P_LEFT[0];
-            c->pred_y= P_LEFT[1];
-        }
-
-        if(mv_table == s->b_forw_mv_table){
-            mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
-        }else{
-            mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
-        }
-
-        dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
-
-        break;
-    }
-
-    dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
-
-    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
-        dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
-
-//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;
-    mv_table[mot_xy][0]= mx;
-    mv_table[mot_xy][1]= my;
-
-    return dmin;
-}
-
-static inline int check_bidir_mv(MpegEncContext * s,
-                   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 size, int h)
-{
-    //FIXME optimize?
-    //FIXME better f_code prediction (max mv & distance)
-    //FIXME pointers
-    MotionEstContext * const c= &s->me;
-    uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
-    uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
-    int stride= c->stride;
-    uint8_t *dest_y = c->scratchpad;
-    uint8_t *ptr;
-    int dxy;
-    int src_x, src_y;
-    int fbmin;
-    uint8_t **src_data= c->src[0];
-    uint8_t **ref_data= c->ref[0];
-    uint8_t **ref2_data= c->ref[2];
-
-    if(s->quarter_sample){
-        dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
-        src_x = motion_fx >> 2;
-        src_y = motion_fy >> 2;
-
-        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 = motion_bx >> 2;
-        src_y = motion_by >> 2;
-
-        ptr = ref2_data[0] + (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 = motion_fx >> 1;
-        src_y = motion_fy >> 1;
-
-        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 = motion_bx >> 1;
-        src_y = motion_by >> 1;
-
-        ptr = ref2_data[0] + (src_y * stride) + src_x;
-        s->dsp.avg_pixels_tab[size][dxy](dest_y    , ptr    , stride, h);
-    }
-
-    fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
-           +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
-           + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
-
-    if(c->avctx->mb_cmp&FF_CMP_CHROMA){
-    }
-    //FIXME CHROMA !!!
-
-    return fbmin;
-}
-
-/* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
-static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
-{
-    MotionEstContext * const c= &s->me;
-    const int mot_stride = s->mb_stride;
-    const int xy = mb_y *mot_stride + mb_x;
-    int fbmin;
-    int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
-    int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
-    int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
-    int pred_by= s->b_bidir_back_mv_table[xy-1][1];
-    int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
-    int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
-    int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
-    int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
-    const int flags= c->sub_flags;
-    const int qpel= flags&FLAG_QPEL;
-    const int shift= 1+qpel;
-    const int xmin= c->xmin<<shift;
-    const int ymin= c->ymin<<shift;
-    const int xmax= c->xmax<<shift;
-    const int ymax= c->ymax<<shift;
-    uint8_t map[8][8][8][8];
-
-    memset(map,0,sizeof(map));
-#define BIDIR_MAP(fx,fy,bx,by) \
-    map[(motion_fx+fx)&7][(motion_fy+fy)&7][(motion_bx+bx)&7][(motion_by+by)&7]
-    BIDIR_MAP(0,0,0,0) = 1;
-
-    fbmin= check_bidir_mv(s, motion_fx, motion_fy,
-                          motion_bx, motion_by,
-                          pred_fx, pred_fy,
-                          pred_bx, pred_by,
-                          0, 16);
-
-    if(s->avctx->bidir_refine){
-        int score, end;
-#define CHECK_BIDIR(fx,fy,bx,by)\
-    if( !BIDIR_MAP(fx,fy,bx,by)\
-       &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
-       &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
-        BIDIR_MAP(fx,fy,bx,by) = 1;\
-        score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\
-        if(score < fbmin){\
-            fbmin= score;\
-            motion_fx+=fx;\
-            motion_fy+=fy;\
-            motion_bx+=bx;\
-            motion_by+=by;\
-            end=0;\
-        }\
-    }
-#define CHECK_BIDIR2(a,b,c,d)\
-CHECK_BIDIR(a,b,c,d)\
-CHECK_BIDIR(-(a),-(b),-(c),-(d))
-
-#define CHECK_BIDIRR(a,b,c,d)\
-CHECK_BIDIR2(a,b,c,d)\
-CHECK_BIDIR2(b,c,d,a)\
-CHECK_BIDIR2(c,d,a,b)\
-CHECK_BIDIR2(d,a,b,c)
-
-        do{
-            end=1;
-
-            CHECK_BIDIRR( 0, 0, 0, 1)
-            if(s->avctx->bidir_refine > 1){
-                CHECK_BIDIRR( 0, 0, 1, 1)
-                CHECK_BIDIR2( 0, 1, 0, 1)
-                CHECK_BIDIR2( 1, 0, 1, 0)
-                CHECK_BIDIRR( 0, 0,-1, 1)
-                CHECK_BIDIR2( 0,-1, 0, 1)
-                CHECK_BIDIR2(-1, 0, 1, 0)
-                if(s->avctx->bidir_refine > 2){
-                    CHECK_BIDIRR( 0, 1, 1, 1)
-                    CHECK_BIDIRR( 0,-1, 1, 1)
-                    CHECK_BIDIRR( 0, 1,-1, 1)
-                    CHECK_BIDIRR( 0, 1, 1,-1)
-                    if(s->avctx->bidir_refine > 3){
-                        CHECK_BIDIR2( 1, 1, 1, 1)
-                        CHECK_BIDIRR( 1, 1, 1,-1)
-                        CHECK_BIDIR2( 1, 1,-1,-1)
-                        CHECK_BIDIR2( 1,-1,-1, 1)
-                        CHECK_BIDIR2( 1,-1, 1,-1)
-                    }
-                }
-            }
-        }while(!end);
-    }
-
-    s->b_bidir_forw_mv_table[xy][0]= motion_fx;
-    s->b_bidir_forw_mv_table[xy][1]= motion_fy;
-    s->b_bidir_back_mv_table[xy][0]= motion_bx;
-    s->b_bidir_back_mv_table[xy][1]= motion_by;
-
-    return fbmin;
-}
-
-static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
-{
-    MotionEstContext * const c= &s->me;
-    int P[10][2];
-    const int mot_stride = s->mb_stride;
-    const int mot_xy = mb_y*mot_stride + mb_x;
-    const int shift= 1+s->quarter_sample;
-    int dmin, i;
-    const int time_pp= s->pp_time;
-    const int time_pb= s->pb_time;
-    int mx, my, xmin, xmax, ymin, ymax;
-    int16_t (*mv_table)[2]= s->b_direct_mv_table;
-
-    c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
-    ymin= xmin=(-32)>>shift;
-    ymax= xmax=   31>>shift;
-
-    if(IS_8X8(s->next_picture.mb_type[mot_xy])){
-        s->mv_type= MV_TYPE_8X8;
-    }else{
-        s->mv_type= MV_TYPE_16X16;
-    }
-
-    for(i=0; i<4; i++){
-        int index= s->block_index[i];
-        int min, max;
-
-        c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
-        c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
-        c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
-        c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
-//        c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
-//        c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
-
-        max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
-        min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
-        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(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
-        min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
-        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);
-
-        if(s->mv_type == MV_TYPE_16X16) break;
-    }
-
-    assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
-
-    if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
-        s->b_direct_mv_table[mot_xy][0]= 0;
-        s->b_direct_mv_table[mot_xy][1]= 0;
-
-        return 256*256*256*64;
-    }
-
-    c->xmin= xmin;
-    c->ymin= ymin;
-    c->xmax= xmax;
-    c->ymax= ymax;
-    c->flags     |= FLAG_DIRECT;
-    c->sub_flags |= FLAG_DIRECT;
-    c->pred_x=0;
-    c->pred_y=0;
-
-    P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
-    P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
-
-    /* special case for first line */
-    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its cliped
-        P_TOP[0]      = clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
-        P_TOP[1]      = clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
-        P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
-        P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
-
-        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]);
-    }
-
-    dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
-    if(c->sub_flags&FLAG_QPEL)
-        dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-    else
-        dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
-
-    if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
-        dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
-
-    get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
-
-    s->b_direct_mv_table[mot_xy][0]= mx;
-    s->b_direct_mv_table[mot_xy][1]= my;
-    c->flags     &= ~FLAG_DIRECT;
-    c->sub_flags &= ~FLAG_DIRECT;
-
-    return dmin;
-}
-
-void ff_estimate_b_frame_motion(MpegEncContext * s,
-                             int mb_x, int mb_y)
-{
-    MotionEstContext * const c= &s->me;
-    const int penalty_factor= c->mb_penalty_factor;
-    int fmin, bmin, dmin, fbmin, bimin, fimin;
-    int type=0;
-    const int xy = mb_y*s->mb_stride + mb_x;
-    init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
-
-    get_limits(s, 16*mb_x, 16*mb_y);
-
-    c->skip=0;
-    if(c->avctx->me_threshold){
-        int vard= check_input_motion(s, mb_x, mb_y, 0);
-
-        if((vard+128)>>8 < c->avctx->me_threshold){
-//            pix = c->src[0][0];
-//            sum = s->dsp.pix_sum(pix, s->linesize);
-//            varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
-
-//            pic->mb_var   [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
-             s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
-/*            pic->mb_mean  [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
-            c->mb_var_sum_temp    += (varc+128)>>8;*/
-            c->mc_mb_var_sum_temp += (vard+128)>>8;
-/*            if (vard <= 64<<8 || vard < varc) {
-                c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
-            }else{
-                c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
-            }*/
-            return;
-        }
-        if((vard+128)>>8 < c->avctx->mb_threshold){
-            type= s->mb_type[mb_y*s->mb_stride + mb_x];
-            if(type == CANDIDATE_MB_TYPE_DIRECT){
-                direct_search(s, mb_x, mb_y);
-            }
-            if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
-                c->skip=0;
-                ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
-            }
-            if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
-                c->skip=0;
-                ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
-            }
-            if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
-                c->skip=0;
-                c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
-                interlaced_search(s, 0,
-                                        s->b_field_mv_table[0], s->b_field_select_table[0],
-                                        s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
-            }
-            if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
-                c->skip=0;
-                c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
-                interlaced_search(s, 2,
-                                        s->b_field_mv_table[1], s->b_field_select_table[1],
-                                        s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
-            }
-            return;
-        }
-    }
-
-    if (s->codec_id == CODEC_ID_MPEG4)
-        dmin= direct_search(s, mb_x, mb_y);
-    else
-        dmin= INT_MAX;
-//FIXME penalty stuff for non mpeg4
-    c->skip=0;
-    fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
-
-    c->skip=0;
-    bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
-//printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
-
-    c->skip=0;
-    fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
-//printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
-
-    if(s->flags & CODEC_FLAG_INTERLACED_ME){
-//FIXME mb type penalty
-        c->skip=0;
-        c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
-        fimin= interlaced_search(s, 0,
-                                 s->b_field_mv_table[0], s->b_field_select_table[0],
-                                 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
-        c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
-        bimin= interlaced_search(s, 2,
-                                 s->b_field_mv_table[1], s->b_field_select_table[1],
-                                 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
-    }else
-        fimin= bimin= INT_MAX;
-
-    {
-        int score= fmin;
-        type = CANDIDATE_MB_TYPE_FORWARD;
-
-        if (dmin <= score){
-            score = dmin;
-            type = CANDIDATE_MB_TYPE_DIRECT;
-        }
-        if(bmin<score){
-            score=bmin;
-            type= CANDIDATE_MB_TYPE_BACKWARD;
-        }
-        if(fbmin<score){
-            score=fbmin;
-            type= CANDIDATE_MB_TYPE_BIDIR;
-        }
-        if(fimin<score){
-            score=fimin;
-            type= CANDIDATE_MB_TYPE_FORWARD_I;
-        }
-        if(bimin<score){
-            score=bimin;
-            type= CANDIDATE_MB_TYPE_BACKWARD_I;
-        }
-
-        score= ((unsigned)(score*score + 128*256))>>16;
-        c->mc_mb_var_sum_temp += score;
-        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
-    }
-
-    if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
-        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;
-}
-
-/* find best f_code for ME which do unlimited searches */
-int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
-{
-    if(s->me_method>=ME_EPZS){
-        int score[8];
-        int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
-        uint8_t * fcode_tab= s->fcode_tab;
-        int best_fcode=-1;
-        int best_score=-10000000;
-
-        if(s->msmpeg4_version)
-            range= FFMIN(range, 16);
-        else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
-            range= FFMIN(range, 256);
-
-        for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
-
-        for(y=0; y<s->mb_height; y++){
-            int x;
-            int xy= y*s->mb_stride;
-            for(x=0; x<s->mb_width; x++){
-                if(s->mb_type[xy] & type){
-                    int mx= mv_table[xy][0];
-                    int my= mv_table[xy][1];
-                    int fcode= FFMAX(fcode_tab[mx + MAX_MV],
-                                     fcode_tab[my + MAX_MV]);
-                    int j;
-
-                        if(mx >= range || mx < -range ||
-                           my >= range || my < -range)
-                            continue;
-
-                    for(j=0; j<fcode && j<8; j++){
-                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
-                            score[j]-= 170;
-                    }
-                }
-                xy++;
-            }
-        }
-
-        for(i=1; i<8; i++){
-            if(score[i] > best_score){
-                best_score= score[i];
-                best_fcode= i;
-            }
-//            printf("%d %d\n", i, score[i]);
-        }
-
-//    printf("fcode: %d type: %d\n", i, s->pict_type);
-        return best_fcode;
-/*        for(i=0; i<=MAX_FCODE; i++){
-            printf("%d ", mv_num[i]);
-        }
-        printf("\n");*/
-    }else{
-        return 1;
-    }
-}
-
-void ff_fix_long_p_mvs(MpegEncContext * s)
-{
-    MotionEstContext * const c= &s->me;
-    const int f_code= s->f_code;
-    int y, range;
-    assert(s->pict_type==P_TYPE);
-
-    range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
-
-    assert(range <= 16 || !s->msmpeg4_version);
-    assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
-
-    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
-
-//printf("%d no:%d %d//\n", clip, noclip, f_code);
-    if(s->flags&CODEC_FLAG_4MV){
-        const int wrap= s->b8_stride;
-
-        /* clip / convert to intra 8x8 type MVs */
-        for(y=0; y<s->mb_height; y++){
-            int xy= y*2*wrap;
-            int i= y*s->mb_stride;
-            int x;
-
-            for(x=0; x<s->mb_width; x++){
-                if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
-                    int block;
-                    for(block=0; block<4; block++){
-                        int off= (block& 1) + (block>>1)*wrap;
-                        int mx= s->current_picture.motion_val[0][ xy + off ][0];
-                        int my= s->current_picture.motion_val[0][ xy + off ][1];
-
-                        if(   mx >=range || mx <-range
-                           || my >=range || my <-range){
-                            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;
-                        }
-                    }
-                }
-                xy+=2;
-                i++;
-            }
-        }
-    }
-}
-
-/**
- *
- * @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)
-{
-    MotionEstContext * const c= &s->me;
-    int y, h_range, v_range;
-
-    // RAL: 8 in MPEG-1, 16 in MPEG-4
-    int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
-
-    if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
-
-    h_range= range;
-    v_range= field_select_table ? range>>1 : range;
-
-    /* clip / convert to intra 16x16 type MVs */
-    for(y=0; y<s->mb_height; y++){
-        int x;
-        int xy= y*s->mb_stride;
-        for(x=0; x<s->mb_width; x++){
-            if (s->mb_type[xy] & type){    // RAL: "type" test added...
-                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(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;
-                        }
-                    }
-                }
-            }
-            xy++;
-        }
-    }
-}
--- a/src/ffmpeg/libavcodec/motion_est_template.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1104 +0,0 @@
-/*
- * Motion estimation
- * Copyright (c) 2002-2004 Michael Niedermayer
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
-
-/**
- * @file motion_est_template.c
- * Motion estimation template.
- */
-
-//lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
-#define LOAD_COMMON\
-    uint32_t attribute_unused * const score_map= c->score_map;\
-    const int attribute_unused xmin= c->xmin;\
-    const int attribute_unused ymin= c->ymin;\
-    const int attribute_unused xmax= c->xmax;\
-    const int attribute_unused ymax= c->ymax;\
-    uint8_t *mv_penalty= c->current_mv_penalty;\
-    const int pred_x= c->pred_x;\
-    const int pred_y= c->pred_y;\
-
-#define CHECK_HALF_MV(dx, dy, x, y)\
-{\
-    const int hx= 2*(x)+(dx);\
-    const int hy= 2*(y)+(dy);\
-    d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\
-    d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
-    COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
-}
-
-#if 0
-static int hpel_motion_search)(MpegEncContext * s,
-                                  int *mx_ptr, int *my_ptr, int dmin,
-                                  uint8_t *ref_data[3],
-                                  int size)
-{
-    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= c->sub_penalty_factor;
-
-    LOAD_COMMON
-
- //   INIT;
- //FIXME factorize
-    me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
-
-    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];
-    }else{
-        hpel_put=& s->dsp.put_pixels_tab[size];
-        chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
-    }
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-    cmp_sub= s->dsp.me_sub_cmp[size];
-    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
-
-    if(c->skip){ //FIXME somehow move up (benchmark)
-        *mx_ptr = 0;
-        *my_ptr = 0;
-        return dmin;
-    }
-
-    if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
-        CMP_HPEL(dmin, 0, 0, mx, my, size);
-        if(mx || my)
-            dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
-    }
-
-    if (mx > xmin && mx < xmax &&
-        my > ymin && my < ymax) {
-        int bx=2*mx, by=2*my;
-        int d= dmin;
-
-        CHECK_HALF_MV(1, 1, mx-1, my-1)
-        CHECK_HALF_MV(0, 1, mx  , my-1)
-        CHECK_HALF_MV(1, 1, mx  , my-1)
-        CHECK_HALF_MV(1, 0, mx-1, my  )
-        CHECK_HALF_MV(1, 0, mx  , my  )
-        CHECK_HALF_MV(1, 1, mx-1, my  )
-        CHECK_HALF_MV(0, 1, mx  , my  )
-        CHECK_HALF_MV(1, 1, mx  , my  )
-
-        assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
-
-        *mx_ptr = bx;
-        *my_ptr = by;
-    }else{
-        *mx_ptr =2*mx;
-        *my_ptr =2*my;
-    }
-
-    return dmin;
-}
-
-#else
-static int hpel_motion_search(MpegEncContext * s,
-                                  int *mx_ptr, int *my_ptr, int dmin,
-                                  int src_index, int ref_index,
-                                  int size, int h)
-{
-    MotionEstContext * const c= &s->me;
-    const int mx = *mx_ptr;
-    const int my = *my_ptr;
-    const int penalty_factor= c->sub_penalty_factor;
-    me_cmp_func cmp_sub, chroma_cmp_sub;
-    int bx=2*mx, by=2*my;
-
-    LOAD_COMMON
-    int flags= c->sub_flags;
-
- //FIXME factorize
-
-    cmp_sub= s->dsp.me_sub_cmp[size];
-    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
-
-    if(c->skip){ //FIXME move out of hpel?
-        *mx_ptr = 0;
-        *my_ptr = 0;
-        return dmin;
-    }
-
-    if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
-        dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
-        if(mx || my || size>0)
-            dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
-    }
-
-    if (mx > xmin && mx < xmax &&
-        my > ymin && my < ymax) {
-        int d= dmin;
-        const int index= (my<<ME_MAP_SHIFT) + mx;
-        const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx   - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;
-        const int l= score_map[(index- 1               )&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx-2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
-        const int r= score_map[(index+ 1               )&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx+2 - pred_x] + mv_penalty[by   - pred_y])*c->penalty_factor;
-        const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
-                     + (mv_penalty[bx   - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;
-
-#if 1
-        int key;
-        int map_generation= c->map_generation;
-#ifndef NDEBUG
-        uint32_t *map= c->map;
-#endif
-        key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
-        assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
-        key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
-        assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
-        key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
-        assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
-        key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
-        assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
-#endif
-        if(t<=b){
-            CHECK_HALF_MV(0, 1, mx  ,my-1)
-            if(l<=r){
-                CHECK_HALF_MV(1, 1, mx-1, my-1)
-                if(t+r<=b+l){
-                    CHECK_HALF_MV(1, 1, mx  , my-1)
-                }else{
-                    CHECK_HALF_MV(1, 1, mx-1, my  )
-                }
-                CHECK_HALF_MV(1, 0, mx-1, my  )
-            }else{
-                CHECK_HALF_MV(1, 1, mx  , my-1)
-                if(t+l<=b+r){
-                    CHECK_HALF_MV(1, 1, mx-1, my-1)
-                }else{
-                    CHECK_HALF_MV(1, 1, mx  , my  )
-                }
-                CHECK_HALF_MV(1, 0, mx  , my  )
-            }
-        }else{
-            if(l<=r){
-                if(t+l<=b+r){
-                    CHECK_HALF_MV(1, 1, mx-1, my-1)
-                }else{
-                    CHECK_HALF_MV(1, 1, mx  , my  )
-                }
-                CHECK_HALF_MV(1, 0, mx-1, my)
-                CHECK_HALF_MV(1, 1, mx-1, my)
-            }else{
-                if(t+r<=b+l){
-                    CHECK_HALF_MV(1, 1, mx  , my-1)
-                }else{
-                    CHECK_HALF_MV(1, 1, mx-1, my)
-                }
-                CHECK_HALF_MV(1, 0, mx  , my)
-                CHECK_HALF_MV(1, 1, mx  , my)
-            }
-            CHECK_HALF_MV(0, 1, mx  , my)
-        }
-        assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
-    }
-
-    *mx_ptr = bx;
-    *my_ptr = by;
-
-    return dmin;
-}
-#endif
-
-static int no_sub_motion_search(MpegEncContext * s,
-          int *mx_ptr, int *my_ptr, int dmin,
-                                  int src_index, int ref_index,
-                                  int size, int h)
-{
-    (*mx_ptr)<<=1;
-    (*my_ptr)<<=1;
-    return dmin;
-}
-
-inline int ff_get_mb_score(MpegEncContext * s, int mx, int my, int src_index,
-                               int ref_index, int size, int h, int add_rate)
-{
-//    const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
-    MotionEstContext * const c= &s->me;
-    const int penalty_factor= c->mb_penalty_factor;
-    const int flags= c->mb_flags;
-    const int qpel= flags & FLAG_QPEL;
-    const int mask= 1+2*qpel;
-    me_cmp_func cmp_sub, chroma_cmp_sub;
-    int d;
-
-    LOAD_COMMON
-
- //FIXME factorize
-
-    cmp_sub= s->dsp.mb_cmp[size];
-    chroma_cmp_sub= s->dsp.mb_cmp[size+1];
-
-//    assert(!c->skip);
-//    assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp);
-
-    d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
-    //FIXME check cbp before adding penalty for (0,0) vector
-    if(add_rate && (mx || my || size>0))
-        d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
-
-    return d;
-}
-
-#define CHECK_QUARTER_MV(dx, dy, x, y)\
-{\
-    const int hx= 4*(x)+(dx);\
-    const int hy= 4*(y)+(dy);\
-    d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
-    d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
-    COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
-}
-
-static int qpel_motion_search(MpegEncContext * s,
-                                  int *mx_ptr, int *my_ptr, int dmin,
-                                  int src_index, int ref_index,
-                                  int size, int h)
-{
-    MotionEstContext * const c= &s->me;
-    const int mx = *mx_ptr;
-    const int my = *my_ptr;
-    const int penalty_factor= c->sub_penalty_factor;
-    const int map_generation= c->map_generation;
-    const int subpel_quality= c->avctx->me_subpel_quality;
-    uint32_t *map= c->map;
-    me_cmp_func cmpf, chroma_cmpf;
-    me_cmp_func cmp_sub, chroma_cmp_sub;
-
-    LOAD_COMMON
-    int flags= c->sub_flags;
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME
- //FIXME factorize
-
-    cmp_sub= s->dsp.me_sub_cmp[size];
-    chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
-
-    if(c->skip){ //FIXME somehow move up (benchmark)
-        *mx_ptr = 0;
-        *my_ptr = 0;
-        return dmin;
-    }
-
-    if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
-        dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
-        if(mx || my || size>0)
-            dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
-    }
-
-    if (mx > xmin && mx < xmax &&
-        my > ymin && my < ymax) {
-        int bx=4*mx, by=4*my;
-        int d= dmin;
-        int i, nx, ny;
-        const int index= (my<<ME_MAP_SHIFT) + mx;
-        const int t= score_map[(index-(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
-        const int l= score_map[(index- 1                 )&(ME_MAP_SIZE-1)];
-        const int r= score_map[(index+ 1                 )&(ME_MAP_SIZE-1)];
-        const int b= score_map[(index+(1<<ME_MAP_SHIFT)  )&(ME_MAP_SIZE-1)];
-        const int c= score_map[(index                    )&(ME_MAP_SIZE-1)];
-        int best[8];
-        int best_pos[8][2];
-
-        memset(best, 64, sizeof(int)*8);
-#if 1
-        if(s->me.dia_size>=2){
-            const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
-            const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
-            const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
-            const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
-
-            for(ny= -3; ny <= 3; ny++){
-                for(nx= -3; nx <= 3; nx++){
-                    //FIXME this could overflow (unlikely though)
-                    const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
-                    const int64_t c2= nx*nx*( r +  l - 2*c) + 4*nx*( r- l) + 32*c;
-                    const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
-                    int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
-                    int i;
-
-                    if((nx&3)==0 && (ny&3)==0) continue;
-
-                    score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
-
-//                    if(nx&1) score-=1024*c->penalty_factor;
-//                    if(ny&1) score-=1024*c->penalty_factor;
-
-                    for(i=0; i<8; i++){
-                        if(score < best[i]){
-                            memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
-                            memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
-                            best[i]= score;
-                            best_pos[i][0]= nx + 4*mx;
-                            best_pos[i][1]= ny + 4*my;
-                            break;
-                        }
-                    }
-                }
-            }
-        }else{
-            int tl;
-            //FIXME this could overflow (unlikely though)
-            const int cx = 4*(r - l);
-            const int cx2= r + l - 2*c;
-            const int cy = 4*(b - t);
-            const int cy2= b + t - 2*c;
-            int cxy;
-
-            if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
-                tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
-            }else{
-                tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);//FIXME wrong if chroma me is different
-            }
-
-            cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
-
-            assert(16*cx2 + 4*cx + 32*c == 32*r);
-            assert(16*cx2 - 4*cx + 32*c == 32*l);
-            assert(16*cy2 + 4*cy + 32*c == 32*b);
-            assert(16*cy2 - 4*cy + 32*c == 32*t);
-            assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
-
-            for(ny= -3; ny <= 3; ny++){
-                for(nx= -3; nx <= 3; nx++){
-                    //FIXME this could overflow (unlikely though)
-                    int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
-                    int i;
-
-                    if((nx&3)==0 && (ny&3)==0) continue;
-
-                    score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
-//                    if(nx&1) score-=32*c->penalty_factor;
-  //                  if(ny&1) score-=32*c->penalty_factor;
-
-                    for(i=0; i<8; i++){
-                        if(score < best[i]){
-                            memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
-                            memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
-                            best[i]= score;
-                            best_pos[i][0]= nx + 4*mx;
-                            best_pos[i][1]= ny + 4*my;
-                            break;
-                        }
-                    }
-                }
-            }
-        }
-        for(i=0; i<subpel_quality; i++){
-            nx= best_pos[i][0];
-            ny= best_pos[i][1];
-            CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
-        }
-
-#if 0
-            const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
-            const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
-            const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
-            const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
-//            if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
-            if(tl<br){
-
-//            nx= FFMAX(4*mx - bx, bx - 4*mx);
-//            ny= FFMAX(4*my - by, by - 4*my);
-
-            static int stats[7][7], count;
-            count++;
-            stats[4*mx - bx + 3][4*my - by + 3]++;
-            if(256*256*256*64 % count ==0){
-                for(i=0; i<49; i++){
-                    if((i%7)==0) printf("\n");
-                    printf("%6d ", stats[0][i]);
-                }
-                printf("\n");
-            }
-            }
-#endif
-#else
-
-        CHECK_QUARTER_MV(2, 2, mx-1, my-1)
-        CHECK_QUARTER_MV(0, 2, mx  , my-1)
-        CHECK_QUARTER_MV(2, 2, mx  , my-1)
-        CHECK_QUARTER_MV(2, 0, mx  , my  )
-        CHECK_QUARTER_MV(2, 2, mx  , my  )
-        CHECK_QUARTER_MV(0, 2, mx  , my  )
-        CHECK_QUARTER_MV(2, 2, mx-1, my  )
-        CHECK_QUARTER_MV(2, 0, mx-1, my  )
-
-        nx= bx;
-        ny= by;
-
-        for(i=0; i<8; i++){
-            int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
-            int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
-            CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
-        }
-#endif
-#if 0
-        //outer ring
-        CHECK_QUARTER_MV(1, 3, mx-1, my-1)
-        CHECK_QUARTER_MV(1, 2, mx-1, my-1)
-        CHECK_QUARTER_MV(1, 1, mx-1, my-1)
-        CHECK_QUARTER_MV(2, 1, mx-1, my-1)
-        CHECK_QUARTER_MV(3, 1, mx-1, my-1)
-        CHECK_QUARTER_MV(0, 1, mx  , my-1)
-        CHECK_QUARTER_MV(1, 1, mx  , my-1)
-        CHECK_QUARTER_MV(2, 1, mx  , my-1)
-        CHECK_QUARTER_MV(3, 1, mx  , my-1)
-        CHECK_QUARTER_MV(3, 2, mx  , my-1)
-        CHECK_QUARTER_MV(3, 3, mx  , my-1)
-        CHECK_QUARTER_MV(3, 0, mx  , my  )
-        CHECK_QUARTER_MV(3, 1, mx  , my  )
-        CHECK_QUARTER_MV(3, 2, mx  , my  )
-        CHECK_QUARTER_MV(3, 3, mx  , my  )
-        CHECK_QUARTER_MV(2, 3, mx  , my  )
-        CHECK_QUARTER_MV(1, 3, mx  , my  )
-        CHECK_QUARTER_MV(0, 3, mx  , my  )
-        CHECK_QUARTER_MV(3, 3, mx-1, my  )
-        CHECK_QUARTER_MV(2, 3, mx-1, my  )
-        CHECK_QUARTER_MV(1, 3, mx-1, my  )
-        CHECK_QUARTER_MV(1, 2, mx-1, my  )
-        CHECK_QUARTER_MV(1, 1, mx-1, my  )
-        CHECK_QUARTER_MV(1, 0, mx-1, my  )
-#endif
-        assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
-
-        *mx_ptr = bx;
-        *my_ptr = by;
-    }else{
-        *mx_ptr =4*mx;
-        *my_ptr =4*my;
-    }
-
-    return dmin;
-}
-
-
-#define CHECK_MV(x,y)\
-{\
-    const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
-    const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
-    assert((x) >= xmin);\
-    assert((x) <= xmax);\
-    assert((y) >= ymin);\
-    assert((y) <= ymax);\
-/*printf("check_mv %d %d\n", x, y);*/\
-    if(map[index]!=key){\
-        d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
-        map[index]= key;\
-        score_map[index]= d;\
-        d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
-/*printf("score:%d\n", d);*/\
-        COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
-    }\
-}
-
-#define CHECK_CLIPED_MV(ax,ay)\
-{\
-    const int x= ax;\
-    const int y= ay;\
-    const int x2= FFMAX(xmin, FFMIN(x, xmax));\
-    const int y2= FFMAX(ymin, FFMIN(y, ymax));\
-    CHECK_MV(x2, y2)\
-}
-
-#define CHECK_MV_DIR(x,y,new_dir)\
-{\
-    const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
-    const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
-/*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
-    if(map[index]!=key){\
-        d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
-        map[index]= key;\
-        score_map[index]= d;\
-        d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
-/*printf("score:%d\n", d);*/\
-        if(d<dmin){\
-            best[0]=x;\
-            best[1]=y;\
-            dmin=d;\
-            next_dir= new_dir;\
-        }\
-    }\
-}
-
-#define check(x,y,S,v)\
-if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
-if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
-if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
-if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
-
-#define LOAD_COMMON2\
-    uint32_t *map= c->map;\
-    const int qpel= flags&FLAG_QPEL;\
-    const int shift= 1+qpel;\
-
-static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
-                                       int src_index, int ref_index, int const penalty_factor,
-                                       int size, int h, int flags)
-{
-    MotionEstContext * const c= &s->me;
-    me_cmp_func cmpf, chroma_cmpf;
-    int next_dir=-1;
-    LOAD_COMMON
-    LOAD_COMMON2
-    int map_generation= c->map_generation;
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
-        const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
-        const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
-        if(map[index]!=key){ //this will be executed only very rarey
-            score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
-            map[index]= key;
-        }
-    }
-
-    for(;;){
-        int d;
-        const int dir= next_dir;
-        const int x= best[0];
-        const int y= best[1];
-        next_dir=-1;
-
-//printf("%d", dir);
-        if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y  , 0)
-        if(dir!=3 && y>ymin) CHECK_MV_DIR(x  , y-1, 1)
-        if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y  , 2)
-        if(dir!=1 && y<ymax) CHECK_MV_DIR(x  , y+1, 3)
-
-        if(next_dir==-1){
-            return dmin;
-        }
-    }
-}
-
-static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
-                                       int src_index, int ref_index, int const penalty_factor,
-                                       int size, int h, int flags)
-{
-    MotionEstContext * const c= &s->me;
-    me_cmp_func cmpf, chroma_cmpf;
-    int dia_size;
-    LOAD_COMMON
-    LOAD_COMMON2
-    int map_generation= c->map_generation;
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    for(dia_size=1; dia_size<=4; dia_size++){
-        int dir;
-        const int x= best[0];
-        const int y= best[1];
-
-        if(dia_size&(dia_size-1)) continue;
-
-        if(   x + dia_size > xmax
-           || x - dia_size < xmin
-           || y + dia_size > ymax
-           || y - dia_size < ymin)
-           continue;
-
-        for(dir= 0; dir<dia_size; dir+=2){
-            int d;
-
-            CHECK_MV(x + dir           , y + dia_size - dir);
-            CHECK_MV(x + dia_size - dir, y - dir           );
-            CHECK_MV(x - dir           , y - dia_size + dir);
-            CHECK_MV(x - dia_size + dir, y + dir           );
-        }
-
-        if(x!=best[0] || y!=best[1])
-            dia_size=0;
-#if 0
-{
-int dx, dy, i;
-static int stats[8*8];
-dx= FFABS(x-best[0]);
-dy= FFABS(y-best[1]);
-if(dy>dx){
-    dx^=dy; dy^=dx; dx^=dy;
-}
-stats[dy*8 + dx] ++;
-if(256*256*256*64 % (stats[0]+1)==0){
-    for(i=0; i<64; i++){
-        if((i&7)==0) printf("\n");
-        printf("%8d ", stats[i]);
-    }
-    printf("\n");
-}
-}
-#endif
-    }
-    return dmin;
-}
-
-#define SAB_CHECK_MV(ax,ay)\
-{\
-    const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
-    const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
-/*printf("sab check %d %d\n", ax, ay);*/\
-    if(map[index]!=key){\
-        d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
-        map[index]= key;\
-        score_map[index]= d;\
-        d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
-/*printf("score: %d\n", d);*/\
-        if(d < minima[minima_count-1].height){\
-            int j=0;\
-            \
-            while(d >= minima[j].height) j++;\
-\
-            memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
-\
-            minima[j].checked= 0;\
-            minima[j].height= d;\
-            minima[j].x= ax;\
-            minima[j].y= ay;\
-            \
-            i=-1;\
-            continue;\
-        }\
-    }\
-}
-
-#define MAX_SAB_SIZE ME_MAP_SIZE
-static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
-                                       int src_index, int ref_index, int const penalty_factor,
-                                       int size, int h, int flags)
-{
-    MotionEstContext * const c= &s->me;
-    me_cmp_func cmpf, chroma_cmpf;
-    Minima minima[MAX_SAB_SIZE];
-    const int minima_count= FFABS(c->dia_size);
-    int i, j;
-    LOAD_COMMON
-    LOAD_COMMON2
-    int map_generation= c->map_generation;
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    for(j=i=0; i<ME_MAP_SIZE; i++){
-        uint32_t key= map[i];
-
-        key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
-
-        if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
-
-        assert(j<MAX_SAB_SIZE); //max j = number of predictors
-
-        minima[j].height= score_map[i];
-        minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
-        minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
-        minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
-        minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
-        minima[j].checked=0;
-        if(minima[j].x || minima[j].y)
-            minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
-
-        j++;
-    }
-
-    qsort(minima, j, sizeof(Minima), minima_cmp);
-
-    for(; j<minima_count; j++){
-        minima[j].height=256*256*256*64;
-        minima[j].checked=0;
-        minima[j].x= minima[j].y=0;
-    }
-
-    for(i=0; i<minima_count; i++){
-        const int x= minima[i].x;
-        const int y= minima[i].y;
-        int d;
-
-        if(minima[i].checked) continue;
-
-        if(   x >= xmax || x <= xmin
-           || y >= ymax || y <= ymin)
-           continue;
-
-        SAB_CHECK_MV(x-1, y)
-        SAB_CHECK_MV(x+1, y)
-        SAB_CHECK_MV(x  , y-1)
-        SAB_CHECK_MV(x  , y+1)
-
-        minima[i].checked= 1;
-    }
-
-    best[0]= minima[0].x;
-    best[1]= minima[0].y;
-    dmin= minima[0].height;
-
-    if(   best[0] < xmax && best[0] > xmin
-       && best[1] < ymax && best[1] > ymin){
-        int d;
-        //ensure that the refernece samples for hpel refinement are in the map
-        CHECK_MV(best[0]-1, best[1])
-        CHECK_MV(best[0]+1, best[1])
-        CHECK_MV(best[0], best[1]-1)
-        CHECK_MV(best[0], best[1]+1)
-    }
-    return dmin;
-}
-
-static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
-                                       int src_index, int ref_index, int const penalty_factor,
-                                       int size, int h, int flags)
-{
-    MotionEstContext * const c= &s->me;
-    me_cmp_func cmpf, chroma_cmpf;
-    int dia_size;
-    LOAD_COMMON
-    LOAD_COMMON2
-    int map_generation= c->map_generation;
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    for(dia_size=1; dia_size<=c->dia_size; dia_size++){
-        int dir, start, end;
-        const int x= best[0];
-        const int y= best[1];
-
-        start= FFMAX(0, y + dia_size - ymax);
-        end  = FFMIN(dia_size, xmax - x + 1);
-        for(dir= start; dir<end; dir++){
-            int d;
-
-//check(x + dir,y + dia_size - dir,0, a0)
-            CHECK_MV(x + dir           , y + dia_size - dir);
-        }
-
-        start= FFMAX(0, x + dia_size - xmax);
-        end  = FFMIN(dia_size, y - ymin + 1);
-        for(dir= start; dir<end; dir++){
-            int d;
-
-//check(x + dia_size - dir, y - dir,0, a1)
-            CHECK_MV(x + dia_size - dir, y - dir           );
-        }
-
-        start= FFMAX(0, -y + dia_size + ymin );
-        end  = FFMIN(dia_size, x - xmin + 1);
-        for(dir= start; dir<end; dir++){
-            int d;
-
-//check(x - dir,y - dia_size + dir,0, a2)
-            CHECK_MV(x - dir           , y - dia_size + dir);
-        }
-
-        start= FFMAX(0, -x + dia_size + xmin );
-        end  = FFMIN(dia_size, ymax - y + 1);
-        for(dir= start; dir<end; dir++){
-            int d;
-
-//check(x - dia_size + dir, y + dir,0, a3)
-            CHECK_MV(x - dia_size + dir, y + dir           );
-        }
-
-        if(x!=best[0] || y!=best[1])
-            dia_size=0;
-#if 0
-{
-int dx, dy, i;
-static int stats[8*8];
-dx= FFABS(x-best[0]);
-dy= FFABS(y-best[1]);
-stats[dy*8 + dx] ++;
-if(256*256*256*64 % (stats[0]+1)==0){
-    for(i=0; i<64; i++){
-        if((i&7)==0) printf("\n");
-        printf("%6d ", stats[i]);
-    }
-    printf("\n");
-}
-}
-#endif
-    }
-    return dmin;
-}
-
-static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
-                                       int src_index, int ref_index, int const penalty_factor,
-                                       int size, int h, int flags){
-    MotionEstContext * const c= &s->me;
-    if(c->dia_size==-1)
-        return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-    else if(c->dia_size<-1)
-        return   sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-    else if(c->dia_size<2)
-        return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-    else
-        return   var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-}
-
-static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
-                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
-                             int ref_mv_scale, int flags, int size, int h)
-{
-    MotionEstContext * const c= &s->me;
-    int best[2]={0, 0};
-    int d, dmin;
-    int map_generation;
-    int penalty_factor;
-    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 cmpf, chroma_cmpf;
-
-    LOAD_COMMON
-    LOAD_COMMON2
-
-    if(c->pre_pass){
-        penalty_factor= c->pre_penalty_factor;
-        cmpf= s->dsp.me_pre_cmp[size];
-        chroma_cmpf= s->dsp.me_pre_cmp[size+1];
-    }else{
-        penalty_factor= c->penalty_factor;
-        cmpf= s->dsp.me_cmp[size];
-        chroma_cmpf= s->dsp.me_cmp[size+1];
-    }
-
-    map_generation= update_map_generation(c);
-
-    assert(cmpf);
-    dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
-    map[0]= map_generation;
-    score_map[0]= dmin;
-
-    /* first line */
-    if (s->first_slice_line) {
-        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)
-    }else{
-        if(dmin<((h*h*s->avctx->mv0_threshold)>>8)
-                    && ( P_LEFT[0]    |P_LEFT[1]
-                        |P_TOP[0]     |P_TOP[1]
-                        |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
-            *mx_ptr= 0;
-            *my_ptr= 0;
-            c->skip=1;
-            return dmin;
-        }
-        CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
-        if(dmin>h*h*2){
-            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_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)
-        }
-    }
-    if(dmin>h*h*4){
-        if(c->pre_pass){
-            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)
-            if(!s->first_slice_line)
-                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)
-        }else{
-            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)
-            if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
-                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(c->avctx->last_predictor_count){
-        const int count= c->avctx->last_predictor_count;
-        const int xstart= FFMAX(0, s->mb_x - count);
-        const int ystart= FFMAX(0, s->mb_y - count);
-        const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
-        const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
-        int mb_y;
-
-        for(mb_y=ystart; mb_y<yend; mb_y++){
-            int mb_x;
-            for(mb_x=xstart; mb_x<xend; mb_x++){
-                const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
-                int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
-                int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
-
-                if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
-                CHECK_MV(mx,my)
-            }
-        }
-    }
-
-//check(best[0],best[1],0, b0)
-    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-
-//check(best[0],best[1],0, b1)
-    *mx_ptr= best[0];
-    *my_ptr= best[1];
-
-//    printf("%d %d %d \n", best[0], best[1], dmin);
-    return dmin;
-}
-
-//this function is dedicated to the braindamaged gcc
-inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,
-                             int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
-                             int ref_mv_scale, int size, int h)
-{
-    MotionEstContext * const c= &s->me;
-//FIXME convert other functions in the same way if faster
-    if(c->flags==0 && h==16 && size==0){
-        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);
-//    case FLAG_QPEL:
-//        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
-    }else{
-        return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);
-    }
-}
-
-static int epzs_motion_search4(MpegEncContext * s,
-                             int *mx_ptr, int *my_ptr, int P[10][2],
-                             int src_index, int ref_index, int16_t (*last_mv)[2],
-                             int ref_mv_scale)
-{
-    MotionEstContext * const c= &s->me;
-    int best[2]={0, 0};
-    int d, dmin;
-    int map_generation;
-    const int penalty_factor= c->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 cmpf, chroma_cmpf;
-    LOAD_COMMON
-    int flags= c->flags;
-    LOAD_COMMON2
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    map_generation= update_map_generation(c);
-
-    dmin = 1000000;
-//printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
-    /* first line */
-    if (s->first_slice_line) {
-        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)
-        if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
-            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)
-    }
-
-    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-
-    *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 epzs_motion_search2(MpegEncContext * s,
-                             int *mx_ptr, int *my_ptr, int P[10][2],
-                             int src_index, int ref_index, int16_t (*last_mv)[2],
-                             int ref_mv_scale)
-{
-    MotionEstContext * const c= &s->me;
-    int best[2]={0, 0};
-    int d, dmin;
-    int map_generation;
-    const int penalty_factor= c->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 cmpf, chroma_cmpf;
-    LOAD_COMMON
-    int flags= c->flags;
-    LOAD_COMMON2
-
-    cmpf= s->dsp.me_cmp[size];
-    chroma_cmpf= s->dsp.me_cmp[size+1];
-
-    map_generation= update_map_generation(c);
-
-    dmin = 1000000;
-//printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
-    /* first line */
-    if (s->first_slice_line) {
-        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)
-        if(s->mb_y+1<s->end_mb_y)  //FIXME replace at least with last_slice_line
-            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)
-    }
-
-    dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
-
-    *mx_ptr= best[0];
-    *my_ptr= best[1];
-
-//    printf("%d %d %d \n", best[0], best[1], dmin);
-    return dmin;
-}
--- a/src/ffmpeg/libavcodec/mp3lameaudio.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,221 +0,0 @@
-/*
- * Interface to libmp3lame for mp3 encoding
- * Copyright (c) 2002 Lennert Buytenhek <buytenh@gnu.org>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file mp3lameaudio.c
- * Interface to libmp3lame for mp3 encoding.
- */
-
-#include "avcodec.h"
-#include "mpegaudio.h"
-#include <lame/lame.h>
-
-#define BUFFER_SIZE (2*MPA_FRAME_SIZE)
-typedef struct Mp3AudioContext {
-        lame_global_flags *gfp;
-        int stereo;
-        uint8_t buffer[BUFFER_SIZE];
-        int buffer_index;
-} Mp3AudioContext;
-
-static int MP3lame_encode_init(AVCodecContext *avctx)
-{
-        Mp3AudioContext *s = avctx->priv_data;
-
-        if (avctx->channels > 2)
-                return -1;
-
-        s->stereo = avctx->channels > 1 ? 1 : 0;
-
-        if ((s->gfp = lame_init()) == NULL)
-                goto err;
-        lame_set_in_samplerate(s->gfp, avctx->sample_rate);
-        lame_set_out_samplerate(s->gfp, avctx->sample_rate);
-        lame_set_num_channels(s->gfp, avctx->channels);
-        /* lame 3.91 dies on quality != 5 */
-        lame_set_quality(s->gfp, 5);
-        /* lame 3.91 doesn't work in mono */
-        lame_set_mode(s->gfp, JOINT_STEREO);
-        lame_set_brate(s->gfp, avctx->bit_rate/1000);
-    if(avctx->flags & CODEC_FLAG_QSCALE) {
-        lame_set_brate(s->gfp, 0);
-        lame_set_VBR(s->gfp, vbr_default);
-        lame_set_VBR_q(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
-    }
-        lame_set_bWriteVbrTag(s->gfp,0);
-        if (lame_init_params(s->gfp) < 0)
-                goto err_close;
-
-        avctx->frame_size = lame_get_framesize(s->gfp);
-
-        avctx->coded_frame= avcodec_alloc_frame();
-        avctx->coded_frame->key_frame= 1;
-
-        return 0;
-
-err_close:
-        lame_close(s->gfp);
-err:
-        return -1;
-}
-
-static const int sSampleRates[3] = {
-    44100, 48000,  32000
-};
-
-static const int sBitRates[2][3][15] = {
-    {   {  0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448},
-        {  0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384},
-        {  0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320}
-    },
-    {   {  0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256},
-        {  0,  8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160},
-        {  0,  8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160}
-    },
-};
-
-static const int sSamplesPerFrame[2][3] =
-{
-    {  384,     1152,    1152 },
-    {  384,     1152,     576 }
-};
-
-static const int sBitsPerSlot[3] = {
-    32,
-    8,
-    8
-};
-
-static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
-{
-    uint8_t *dataTmp = (uint8_t *)data;
-    uint32_t header = ( (uint32_t)dataTmp[0] << 24 ) | ( (uint32_t)dataTmp[1] << 16 ) | ( (uint32_t)dataTmp[2] << 8 ) | (uint32_t)dataTmp[3];
-    int layerID = 3 - ((header >> 17) & 0x03);
-    int bitRateID = ((header >> 12) & 0x0f);
-    int sampleRateID = ((header >> 10) & 0x03);
-    int bitsPerSlot = sBitsPerSlot[layerID];
-    int isPadded = ((header >> 9) & 0x01);
-    static int const mode_tab[4]= {2,3,1,0};
-    int mode= mode_tab[(header >> 19) & 0x03];
-    int mpeg_id= mode>0;
-    int temp0, temp1, bitRate;
-
-    if ( (( header >> 21 ) & 0x7ff) != 0x7ff || mode == 3 || layerID==3 || sampleRateID==3) {
-        return -1;
-    }
-
-    if(!samplesPerFrame) samplesPerFrame= &temp0;
-    if(!sampleRate     ) sampleRate     = &temp1;
-
-//    *isMono = ((header >>  6) & 0x03) == 0x03;
-
-    *sampleRate = sSampleRates[sampleRateID]>>mode;
-    bitRate = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
-    *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
-//av_log(NULL, AV_LOG_DEBUG, "sr:%d br:%d spf:%d l:%d m:%d\n", *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
-
-    return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
-}
-
-int MP3lame_encode_frame(AVCodecContext *avctx,
-                     unsigned char *frame, int buf_size, void *data)
-{
-        Mp3AudioContext *s = avctx->priv_data;
-        int len;
-        int lame_result;
-
-        /* lame 3.91 dies on '1-channel interleaved' data */
-
-    if(data){
-        if (s->stereo) {
-            lame_result = lame_encode_buffer_interleaved(
-                s->gfp,
-                data,
-                avctx->frame_size,
-                s->buffer + s->buffer_index,
-                BUFFER_SIZE - s->buffer_index
-                );
-        } else {
-            lame_result = lame_encode_buffer(
-                s->gfp,
-                data,
-                data,
-                avctx->frame_size,
-                s->buffer + s->buffer_index,
-                BUFFER_SIZE - s->buffer_index
-                );
-        }
-    }else{
-        lame_result= lame_encode_flush(
-                s->gfp,
-                s->buffer + s->buffer_index,
-                BUFFER_SIZE - s->buffer_index
-                );
-    }
-
-    if(lame_result==-1) {
-        /* output buffer too small */
-        av_log(avctx, AV_LOG_ERROR, "lame: output buffer too small (buffer index: %d, free bytes: %d)\n", s->buffer_index, BUFFER_SIZE - s->buffer_index);
-        return 0;
-    }
-
-    s->buffer_index += lame_result;
-
-    if(s->buffer_index<4)
-        return 0;
-
-        len= mp3len(s->buffer, NULL, NULL);
-//av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len, s->buffer_index);
-        if(len <= s->buffer_index){
-            memcpy(frame, s->buffer, len);
-            s->buffer_index -= len;
-
-            memmove(s->buffer, s->buffer+len, s->buffer_index);
-            //FIXME fix the audio codec API, so we dont need the memcpy()
-/*for(i=0; i<len; i++){
-    av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
-}*/
-            return len;
-        }else
-            return 0;
-}
-
-int MP3lame_encode_close(AVCodecContext *avctx)
-{
-        Mp3AudioContext *s = avctx->priv_data;
-
-        av_freep(&avctx->coded_frame);
-
-        lame_close(s->gfp);
-        return 0;
-}
-
-
-AVCodec mp3lame_encoder = {
-    "mp3",
-    CODEC_TYPE_AUDIO,
-    CODEC_ID_MP3,
-    sizeof(Mp3AudioContext),
-    MP3lame_encode_init,
-    MP3lame_encode_frame,
-    MP3lame_encode_close,
-    .capabilities= CODEC_CAP_DELAY,
-};
--- a/src/ffmpeg/libavcodec/mpeg12.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,3316 +0,0 @@
-/*
- * MPEG1 codec / MPEG2 decoder
- * Copyright (c) 2000,2001 Fabrice Bellard.
- * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file mpeg12.c
- * MPEG1/2 codec
- */
-
-//#define DEBUG
-#include "avcodec.h"
-#include "dsputil.h"
-#include "mpegvideo.h"
-
-#include "mpeg12data.h"
-
-//#undef NDEBUG
-//#include <assert.h>
-
-
-/* Start codes. */
-#define SEQ_END_CODE            0x000001b7
-#define SEQ_START_CODE          0x000001b3
-#define GOP_START_CODE          0x000001b8
-#define PICTURE_START_CODE      0x00000100
-#define SLICE_MIN_START_CODE    0x00000101
-#define SLICE_MAX_START_CODE    0x000001af
-#define EXT_START_CODE          0x000001b5
-#define USER_START_CODE         0x000001b2
-
-#define DC_VLC_BITS 9
-#define MV_VLC_BITS 9
-#define MBINCR_VLC_BITS 9
-#define MB_PAT_VLC_BITS 9
-#define MB_PTYPE_VLC_BITS 6
-#define MB_BTYPE_VLC_BITS 6
-#define TEX_VLC_BITS 9
-
-#ifdef CONFIG_ENCODERS
-static void mpeg1_encode_block(MpegEncContext *s,
-                         DCTELEM *block,
-                         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 inline int mpeg1_decode_block_inter(MpegEncContext *s,
-                              DCTELEM *block,
-                              int n);
-static inline int mpeg1_decode_block_intra(MpegEncContext *s,
-                              DCTELEM *block,
-                              int n);
-static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
-                                        DCTELEM *block,
-                                        int n);
-static inline int mpeg2_decode_block_intra(MpegEncContext *s,
-                                    DCTELEM *block,
-                                    int n);
-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
-static void exchange_uv(MpegEncContext *s);
-
-#ifdef HAVE_XVMC
-extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
-extern int XVMC_field_end(MpegEncContext *s);
-extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
-extern void XVMC_init_block(MpegEncContext *s);//set s->block
-#endif
-
-const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
-const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
-const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
-const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
-                                           PIX_FMT_XVMC_MPEG2_IDCT,
-                                           PIX_FMT_XVMC_MPEG2_MC,
-                                           -1};
-#ifdef CONFIG_ENCODERS
-static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
-static uint8_t fcode_tab[MAX_MV*2+1];
-
-static uint8_t  uni_mpeg1_ac_vlc_len [64*64*2];
-static uint8_t  uni_mpeg2_ac_vlc_len [64*64*2];
-
-/* simple include everything table for dc, first byte is bits number next 3 are code*/
-static uint32_t mpeg1_lum_dc_uni[512];
-static uint32_t mpeg1_chr_dc_uni[512];
-
-static uint8_t mpeg1_index_run[2][64];
-static int8_t mpeg1_max_level[2][64];
-#endif //CONFIG_ENCODERS
-
-static void init_2d_vlc_rl(RLTable *rl, int use_static)
-{
-    int i;
-
-    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
-             &rl->table_vlc[0][1], 4, 2,
-             &rl->table_vlc[0][0], 4, 2, use_static);
-
-    if(use_static)
-        rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
-    else
-        rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
-
-    for(i=0; i<rl->vlc.table_size; i++){
-        int code= rl->vlc.table[i][0];
-        int len = rl->vlc.table[i][1];
-        int level, run;
-
-        if(len==0){ // illegal code
-            run= 65;
-            level= MAX_LEVEL;
-        }else if(len<0){ //more bits needed
-            run= 0;
-            level= code;
-        }else{
-            if(code==rl->n){ //esc
-                run= 65;
-                level= 0;
-            }else if(code==rl->n+1){ //eob
-                run= 0;
-                level= 127;
-            }else{
-                run=   rl->table_run  [code] + 1;
-                level= rl->table_level[code];
-            }
-        }
-        rl->rl_vlc[0][i].len= len;
-        rl->rl_vlc[0][i].level= level;
-        rl->rl_vlc[0][i].run= run;
-    }
-}
-
-#ifdef CONFIG_ENCODERS
-static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
-    int i;
-
-    for(i=0; i<128; i++){
-        int level= i-64;
-        int run;
-        for(run=0; run<64; run++){
-            int len, bits, code;
-
-            int alevel= FFABS(level);
-            int sign= (level>>31)&1;
-
-            if (alevel > rl->max_level[0][run])
-                code= 111; /*rl->n*/
-            else
-                code= rl->index_run[0][run] + alevel - 1;
-
-            if (code < 111 /* rl->n */) {
-                /* store the vlc & sign at once */
-                len=   rl->table_vlc[code][1]+1;
-                bits= (rl->table_vlc[code][0]<<1) + sign;
-            } else {
-                len=  rl->table_vlc[111/*rl->n*/][1]+6;
-                bits= rl->table_vlc[111/*rl->n*/][0]<<6;
-
-                bits|= run;
-                if (alevel < 128) {
-                    bits<<=8; len+=8;
-                    bits|= level & 0xff;
-                } else {
-                    bits<<=16; len+=16;
-                    bits|= level & 0xff;
-                    if (level < 0) {
-                        bits|= 0x8001 + level + 255;
-                    } else {
-                        bits|= level & 0xffff;
-                    }
-                }
-            }
-
-            uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
-        }
-    }
-}
-
-
-static int find_frame_rate_index(MpegEncContext *s){
-    int i;
-    int64_t dmin= INT64_MAX;
-    int64_t d;
-
-    for(i=1;i<14;i++) {
-        int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
-        int64_t n1= 1001LL*s->avctx->time_base.den;
-        if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
-
-        d = FFABS(n0 - n1);
-        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 > FF_COMPLIANCE_EXPERIMENTAL){
-            av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
-            return -1;
-        }else{
-            av_log(avctx, AV_LOG_INFO, "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n", avctx->time_base.den, avctx->time_base.num);
-        }
-    }
-
-    if(avctx->profile == FF_PROFILE_UNKNOWN)
-        avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
-
-    if(avctx->level == FF_LEVEL_UNKNOWN)
-        avctx->level = s->chroma_format == CHROMA_420 ? 8 : 5;
-
-    return 0;
-}
-
-static void put_header(MpegEncContext *s, int header)
-{
-    align_put_bits(&s->pb);
-    put_bits(&s->pb, 16, header>>16);
-    put_bits(&s->pb, 16, header&0xFFFF);
-}
-
-/* put sequence header if needed */
-static void mpeg1_encode_sequence_header(MpegEncContext *s)
-{
-        unsigned int vbv_buffer_size;
-        unsigned int fps, v;
-        int i;
-        uint64_t time_code;
-        float best_aspect_error= 1E10;
-        float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
-        int constraint_parameter_flag;
-
-        if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
-
-        if (s->current_picture.key_frame) {
-            AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
-
-            /* mpeg1 header repeated every gop */
-            put_header(s, SEQ_START_CODE);
-
-            put_bits(&s->pb, 12, s->width);
-            put_bits(&s->pb, 12, s->height);
-
-            for(i=1; i<15; i++){
-                float error= aspect_ratio;
-                if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
-                    error-= 1.0/mpeg1_aspect[i];
-                else
-                    error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
-
-                error= FFABS(error);
-
-                if(error < best_aspect_error){
-                    best_aspect_error= error;
-                    s->aspect_ratio_info= i;
-                }
-            }
-
-            put_bits(&s->pb, 4, s->aspect_ratio_info);
-            put_bits(&s->pb, 4, s->frame_rate_index);
-
-            if(s->avctx->rc_max_rate){
-                v = (s->avctx->rc_max_rate + 399) / 400;
-                if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
-                    v = 0x3ffff;
-            }else{
-                v= 0x3FFFF;
-            }
-
-            if(s->avctx->rc_buffer_size)
-                vbv_buffer_size = s->avctx->rc_buffer_size;
-            else
-                /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
-                vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
-            vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
-
-            put_bits(&s->pb, 18, v & 0x3FFFF);
-            put_bits(&s->pb, 1, 1); /* marker */
-            put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
-
-            constraint_parameter_flag=
-                s->width <= 768 && s->height <= 576 &&
-                s->mb_width * s->mb_height <= 396 &&
-                s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
-                framerate.num <= framerate.den*30 &&
-                s->avctx->me_range && s->avctx->me_range < 128 &&
-                vbv_buffer_size <= 20 &&
-                v <= 1856000/400 &&
-                s->codec_id == CODEC_ID_MPEG1VIDEO;
-
-            put_bits(&s->pb, 1, constraint_parameter_flag);
-
-            ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
-            ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
-
-            if(s->codec_id == CODEC_ID_MPEG2VIDEO){
-                put_header(s, EXT_START_CODE);
-                put_bits(&s->pb, 4, 1); //seq ext
-
-                put_bits(&s->pb, 1, s->chroma_format == CHROMA_422); //escx
-
-                put_bits(&s->pb, 3, s->avctx->profile); //profile
-                put_bits(&s->pb, 4, s->avctx->level); //level
-
-                put_bits(&s->pb, 1, s->progressive_sequence);
-                put_bits(&s->pb, 2, s->chroma_format);
-                put_bits(&s->pb, 2, 0); //horizontal size ext
-                put_bits(&s->pb, 2, 0); //vertical size ext
-                put_bits(&s->pb, 12, v>>18); //bitrate ext
-                put_bits(&s->pb, 1, 1); //marker
-                put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
-                put_bits(&s->pb, 1, s->low_delay);
-                put_bits(&s->pb, 2, 0); // frame_rate_ext_n
-                put_bits(&s->pb, 5, 0); // frame_rate_ext_d
-            }
-
-            put_header(s, GOP_START_CODE);
-            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 = (framerate.num + framerate.den/2)/ framerate.den;
-            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)));
-            put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
-            put_bits(&s->pb, 1, 0); /* broken link */
-        }
-}
-
-static inline void encode_mb_skip_run(MpegEncContext *s, int run){
-    while (run >= 33) {
-        put_bits(&s->pb, 11, 0x008);
-        run -= 33;
-    }
-    put_bits(&s->pb, mbAddrIncrTable[run][1],
-             mbAddrIncrTable[run][0]);
-}
-#endif //CONFIG_ENCODERS
-
-static void common_init(MpegEncContext *s)
-{
-
-    s->y_dc_scale_table=
-    s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
-
-}
-
-void ff_mpeg1_clean_buffers(MpegEncContext *s){
-    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
-    s->last_dc[1] = s->last_dc[0];
-    s->last_dc[2] = s->last_dc[0];
-    memset(s->last_mv, 0, sizeof(s->last_mv));
-}
-
-#ifdef CONFIG_ENCODERS
-
-void ff_mpeg1_encode_slice_header(MpegEncContext *s){
-    put_header(s, SLICE_MIN_START_CODE + s->mb_y);
-    put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
-    put_bits(&s->pb, 1, 0); /* slice extra information */
-}
-
-void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
-{
-    mpeg1_encode_sequence_header(s);
-
-    /* mpeg1 picture header */
-    put_header(s, PICTURE_START_CODE);
-    /* temporal reference */
-
-    // RAL: s->picture_number instead of s->fake_picture_number
-    put_bits(&s->pb, 10, (s->picture_number -
-                          s->gop_picture_number) & 0x3ff);
-    put_bits(&s->pb, 3, s->pict_type);
-
-    s->vbv_delay_ptr= s->pb.buf + put_bits_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) {
-        put_bits(&s->pb, 1, 0); /* half pel coordinates */
-        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
-            put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
-        else
-            put_bits(&s->pb, 3, 7); /* forward_f_code */
-    }
-
-    // RAL: Backward f_code necessary for B frames
-    if (s->pict_type == B_TYPE) {
-        put_bits(&s->pb, 1, 0); /* half pel coordinates */
-        if(s->codec_id == CODEC_ID_MPEG1VIDEO)
-            put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
-        else
-            put_bits(&s->pb, 3, 7); /* backward_f_code */
-    }
-
-    put_bits(&s->pb, 1, 0); /* extra bit picture */
-
-    s->frame_pred_frame_dct = 1;
-    if(s->codec_id == CODEC_ID_MPEG2VIDEO){
-        put_header(s, EXT_START_CODE);
-        put_bits(&s->pb, 4, 8); //pic ext
-        if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
-            put_bits(&s->pb, 4, s->f_code);
-            put_bits(&s->pb, 4, s->f_code);
-        }else{
-            put_bits(&s->pb, 8, 255);
-        }
-        if (s->pict_type == B_TYPE) {
-            put_bits(&s->pb, 4, s->b_code);
-            put_bits(&s->pb, 4, s->b_code);
-        }else{
-            put_bits(&s->pb, 8, 255);
-        }
-        put_bits(&s->pb, 2, s->intra_dc_precision);
-
-        assert(s->picture_structure == PICT_FRAME);
-        put_bits(&s->pb, 2, s->picture_structure);
-        if (s->progressive_sequence) {
-            put_bits(&s->pb, 1, 0); /* no repeat */
-        } else {
-            put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
-        }
-        /* XXX: optimize the generation of this flag with entropy
-           measures */
-        s->frame_pred_frame_dct = s->progressive_sequence;
-
-        put_bits(&s->pb, 1, s->frame_pred_frame_dct);
-        put_bits(&s->pb, 1, s->concealment_motion_vectors);
-        put_bits(&s->pb, 1, s->q_scale_type);
-        put_bits(&s->pb, 1, s->intra_vlc_format);
-        put_bits(&s->pb, 1, s->alternate_scan);
-        put_bits(&s->pb, 1, s->repeat_first_field);
-        s->progressive_frame = s->progressive_sequence;
-        put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
-        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; i<sizeof(svcd_scan_offset_placeholder); i++){
-            put_bits(&s->pb, 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 field_motion)
-{
-    put_bits(&s->pb, n, bits);
-    if (!s->frame_pred_frame_dct) {
-        if (has_mv)
-            put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
-        put_bits(&s->pb, 1, s->interlaced_dct);
-    }
-}
-
-static always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
-                                                   DCTELEM block[6][64],
-                                                   int motion_x, int motion_y,
-                                                   int mb_block_count)
-{
-    int i, cbp;
-    const int mb_x = s->mb_x;
-    const int mb_y = s->mb_y;
-    const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
-
-    /* compute cbp */
-    cbp = 0;
-    for(i=0;i<mb_block_count;i++) {
-        if (s->block_last_index[i] >= 0)
-            cbp |= 1 << (mb_block_count - 1 - i);
-    }
-
-    if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
-        (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 == 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++;
-        s->qscale -= s->dquant;
-        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);
-            encode_mb_skip_run(s, s->mb_x);
-        }else{
-            encode_mb_skip_run(s, s->mb_skip_run);
-        }
-
-        if (s->pict_type == I_TYPE) {
-            if(s->dquant && cbp){
-                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, 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, 0);
-                put_bits(&s->pb, 5, s->qscale);
-            }else{
-                put_mb_modes(s, 5, 0x03, 0, 0);
-                s->qscale -= s->dquant;
-            }
-            s->misc_bits+= get_bits_diff(s);
-            s->i_count++;
-            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|motion_y) == 0) {
-                        if(s->dquant){
-                            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, 0); /* macroblock_pattern only */
-                        }
-                        s->misc_bits+= get_bits_diff(s);
-                    } else {
-                        if(s->dquant){
-                            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, 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);
-                    }
-                } 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->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 {
-                    put_bits(&s->pb, 3, 1); /* motion only */
-                    put_bits(&s->pb, 2, 1); /* motion_type: field */
-                    s->qscale -= s->dquant;
-                }
-                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) {
-                if (s->chroma_y_shift) {
-                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
-                } else {
-                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
-                    put_bits(&s->pb, 2, cbp & 3);
-                }
-            }
-            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) {
-                if (s->chroma_y_shift) {
-                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
-                } else {
-                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
-                    put_bits(&s->pb, 2, cbp & 3);
-                }
-            }
-        }
-        for(i=0;i<mb_block_count;i++) {
-            if (cbp & (1 << (mb_block_count - 1 - i))) {
-                mpeg1_encode_block(s, block[i], i);
-            }
-        }
-        s->mb_skip_run = 0;
-        if(s->mb_intra)
-            s->i_tex_bits+= get_bits_diff(s);
-        else
-            s->p_tex_bits+= get_bits_diff(s);
-    }
-}
-
-void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
-{
-    if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
-    else                                mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
-}
-
-// RAL: Parameter added: f_or_b_code
-static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
-{
-    int code, bit_size, l, bits, range, sign;
-
-    if (val == 0) {
-        /* zero vector */
-        code = 0;
-        put_bits(&s->pb,
-                 mbMotionVectorTable[0][1],
-                 mbMotionVectorTable[0][0]);
-    } else {
-        bit_size = f_or_b_code - 1;
-        range = 1 << bit_size;
-        /* modulo encoding */
-        l= INT_BIT - 5 - bit_size;
-        val= (val<<l)>>l;
-
-        if (val >= 0) {
-            val--;
-            code = (val >> bit_size) + 1;
-            bits = val & (range - 1);
-            sign = 0;
-        } else {
-            val = -val;
-            val--;
-            code = (val >> bit_size) + 1;
-            bits = val & (range - 1);
-            sign = 1;
-        }
-
-        assert(code > 0 && code <= 16);
-
-        put_bits(&s->pb,
-                 mbMotionVectorTable[code][1],
-                 mbMotionVectorTable[code][0]);
-
-        put_bits(&s->pb, 1, sign);
-        if (bit_size > 0) {
-            put_bits(&s->pb, bit_size, bits);
-        }
-    }
-}
-
-void ff_mpeg1_encode_init(MpegEncContext *s)
-{
-    static int done=0;
-
-    common_init(s);
-
-    if(!done){
-        int f_code;
-        int mv;
-        int i;
-
-        done=1;
-        init_rl(&rl_mpeg1, 1);
-        if(s->intra_vlc_format)
-            init_rl(&rl_mpeg2, 1);
-
-        for(i=0; i<64; i++)
-        {
-                mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
-                mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
-        }
-
-        init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
-        if(s->intra_vlc_format)
-            init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
-
-        /* build unified dc encoding tables */
-        for(i=-255; i<256; i++)
-        {
-                int adiff, index;
-                int bits, code;
-                int diff=i;
-
-                adiff = FFABS(diff);
-                if(diff<0) diff--;
-                index = av_log2(2*adiff);
-
-                bits= vlc_dc_lum_bits[index] + index;
-                code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
-                mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
-
-                bits= vlc_dc_chroma_bits[index] + index;
-                code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
-                mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
-        }
-
-        mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
-
-        for(f_code=1; f_code<=MAX_FCODE; f_code++){
-            for(mv=-MAX_MV; mv<=MAX_MV; mv++){
-                int len;
-
-                if(mv==0) len= mbMotionVectorTable[0][1];
-                else{
-                    int val, bit_size, range, code;
-
-                    bit_size = f_code - 1;
-                    range = 1 << bit_size;
-
-                    val=mv;
-                    if (val < 0)
-                        val = -val;
-                    val--;
-                    code = (val >> bit_size) + 1;
-                    if(code<17){
-                        len= mbMotionVectorTable[code][1] + 1 + bit_size;
-                    }else{
-                        len= mbMotionVectorTable[16][1] + 2 + bit_size;
-                    }
-                }
-
-                mv_penalty[f_code][mv+MAX_MV]= len;
-            }
-        }
-
-
-        for(f_code=MAX_FCODE; f_code>0; f_code--){
-            for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
-                fcode_tab[mv+MAX_MV]= f_code;
-            }
-        }
-    }
-    s->me.mv_penalty= mv_penalty;
-    s->fcode_tab= fcode_tab;
-    if(s->codec_id == CODEC_ID_MPEG1VIDEO){
-        s->min_qcoeff=-255;
-        s->max_qcoeff= 255;
-    }else{
-        s->min_qcoeff=-2047;
-        s->max_qcoeff= 2047;
-    }
-    if (s->intra_vlc_format) {
-        s->intra_ac_vlc_length=
-        s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
-    } else {
-        s->intra_ac_vlc_length=
-        s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
-    }
-    s->inter_ac_vlc_length=
-    s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
-}
-
-static inline void encode_dc(MpegEncContext *s, int diff, int component)
-{
-  if(((unsigned) (diff+255)) >= 511){
-        int index;
-
-        if(diff<0){
-            index= av_log2_16bit(-2*diff);
-            diff--;
-        }else{
-            index= av_log2_16bit(2*diff);
-        }
-        if (component == 0) {
-            put_bits(
-                &s->pb,
-                vlc_dc_lum_bits[index] + index,
-                (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
-        }else{
-            put_bits(
-                &s->pb,
-                vlc_dc_chroma_bits[index] + index,
-                (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
-        }
-  }else{
-    if (component == 0) {
-        put_bits(
-            &s->pb,
-            mpeg1_lum_dc_uni[diff+255]&0xFF,
-            mpeg1_lum_dc_uni[diff+255]>>8);
-    } else {
-        put_bits(
-            &s->pb,
-            mpeg1_chr_dc_uni[diff+255]&0xFF,
-            mpeg1_chr_dc_uni[diff+255]>>8);
-    }
-  }
-}
-
-static void mpeg1_encode_block(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
-    int code, component;
-    const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
-
-    last_index = s->block_last_index[n];
-
-    /* DC coef */
-    if (s->mb_intra) {
-        component = (n <= 3 ? 0 : (n&1) + 1);
-        dc = block[0]; /* overflow is impossible */
-        diff = dc - s->last_dc[component];
-        encode_dc(s, diff, component);
-        s->last_dc[component] = dc;
-        i = 1;
-        if (s->intra_vlc_format)
-            table_vlc = rl_mpeg2.table_vlc;
-    } else {
-        /* encode the first coefficient : needs to be done here because
-           it is handled slightly differently */
-        level = block[0];
-        if (abs(level) == 1) {
-                code = ((uint32_t)level >> 31); /* the sign bit */
-                put_bits(&s->pb, 2, code | 0x02);
-                i = 1;
-        } else {
-            i = 0;
-            last_non_zero = -1;
-            goto next_coef;
-        }
-    }
-
-    /* now quantify & encode AC coefs */
-    last_non_zero = i - 1;
-
-    for(;i<=last_index;i++) {
-        j = s->intra_scantable.permutated[i];
-        level = block[j];
-    next_coef:
-#if 0
-        if (level != 0)
-            dprintf("level[%d]=%d\n", i, level);
-#endif
-        /* encode using VLC */
-        if (level != 0) {
-            run = i - last_non_zero - 1;
-
-            alevel= level;
-            MASK_ABS(sign, alevel)
-            sign&=1;
-
-            if (alevel <= mpeg1_max_level[0][run]){
-                code= mpeg1_index_run[0][run] + alevel - 1;
-                /* store the vlc & sign at once */
-                put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
-            } else {
-                /* escape seems to be pretty rare <5% so i dont optimize it */
-                put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
-                /* escape: only clip in this case */
-                put_bits(&s->pb, 6, run);
-                if(s->codec_id == CODEC_ID_MPEG1VIDEO){
-                    if (alevel < 128) {
-                        put_bits(&s->pb, 8, level & 0xff);
-                    } else {
-                        if (level < 0) {
-                            put_bits(&s->pb, 16, 0x8001 + level + 255);
-                        } else {
-                            put_bits(&s->pb, 16, level & 0xffff);
-                        }
-                    }
-                }else{
-                    put_bits(&s->pb, 12, level & 0xfff);
-                }
-            }
-            last_non_zero = i;
-        }
-    }
-    /* end of block */
-    put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
-}
-#endif //CONFIG_ENCODERS
-
-/******************************************/
-/* decoding */
-
-static VLC dc_lum_vlc;
-static VLC dc_chroma_vlc;
-static VLC mv_vlc;
-static VLC mbincr_vlc;
-static VLC mb_ptype_vlc;
-static VLC mb_btype_vlc;
-static VLC mb_pat_vlc;
-
-static void init_vlcs(void)
-{
-    static int done = 0;
-
-    if (!done) {
-        done = 1;
-
-        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
-                 vlc_dc_lum_bits, 1, 1,
-                 vlc_dc_lum_code, 2, 2, 1);
-        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12,
-                 vlc_dc_chroma_bits, 1, 1,
-                 vlc_dc_chroma_code, 2, 2, 1);
-        init_vlc(&mv_vlc, MV_VLC_BITS, 17,
-                 &mbMotionVectorTable[0][1], 2, 1,
-                 &mbMotionVectorTable[0][0], 2, 1, 1);
-        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
-                 &mbAddrIncrTable[0][1], 2, 1,
-                 &mbAddrIncrTable[0][0], 2, 1, 1);
-        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
-                 &mbPatTable[0][1], 2, 1,
-                 &mbPatTable[0][0], 2, 1, 1);
-
-        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
-                 &table_mb_ptype[0][1], 2, 1,
-                 &table_mb_ptype[0][0], 2, 1, 1);
-        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
-                 &table_mb_btype[0][1], 2, 1,
-                 &table_mb_btype[0][0], 2, 1, 1);
-        init_rl(&rl_mpeg1, 1);
-        init_rl(&rl_mpeg2, 1);
-
-        init_2d_vlc_rl(&rl_mpeg1, 1);
-        init_2d_vlc_rl(&rl_mpeg2, 1);
-    }
-}
-
-static inline int get_dmv(MpegEncContext *s)
-{
-    if(get_bits1(&s->gb))
-        return 1 - (get_bits1(&s->gb) << 1);
-    else
-        return 0;
-}
-
-static inline int get_qscale(MpegEncContext *s)
-{
-    int qscale = get_bits(&s->gb, 5);
-    if (s->q_scale_type) {
-        return non_linear_qscale[qscale];
-    } else {
-        return qscale << 1;
-    }
-}
-
-/* motion type (for mpeg2) */
-#define MT_FIELD 1
-#define MT_FRAME 2
-#define MT_16X8  2
-#define MT_DMV   3
-
-static int mpeg_decode_mb(MpegEncContext *s,
-                          DCTELEM block[12][64])
-{
-    int i, j, k, cbp, val, mb_type, motion_type;
-    const int mb_block_count = 4 + (1<< s->chroma_format);
-
-    dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
-
-    assert(s->mb_skipped==0);
-
-    if (s->mb_skip_run-- != 0) {
-        if(s->pict_type == I_TYPE){
-            av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-
-        /* skip mb */
-        s->mb_intra = 0;
-        for(i=0;i<12;i++)
-            s->block_last_index[i] = -1;
-        if(s->picture_structure == PICT_FRAME)
-            s->mv_type = MV_TYPE_16X16;
-        else
-            s->mv_type = MV_TYPE_FIELD;
-        if (s->pict_type == P_TYPE) {
-            /* if P type, zero motion vector is implied */
-            s->mv_dir = MV_DIR_FORWARD;
-            s->mv[0][0][0] = s->mv[0][0][1] = 0;
-            s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
-            s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
-            s->field_select[0][0]= s->picture_structure - 1;
-            s->mb_skipped = 1;
-            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
-        } else {
-            int mb_type;
-
-            if(s->mb_x)
-                mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
-            else
-                mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all,
-            if(IS_INTRA(mb_type))
-                return -1;
-
-            /* if B type, reuse previous vectors and directions */
-            s->mv[0][0][0] = s->last_mv[0][0][0];
-            s->mv[0][0][1] = s->last_mv[0][0][1];
-            s->mv[1][0][0] = s->last_mv[1][0][0];
-            s->mv[1][0][1] = s->last_mv[1][0][1];
-
-            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
-                mb_type | MB_TYPE_SKIP;
-//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
-
-            if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
-                s->mb_skipped = 1;
-        }
-
-        return 0;
-    }
-
-    switch(s->pict_type) {
-    default:
-    case I_TYPE:
-        if (get_bits1(&s->gb) == 0) {
-            if (get_bits1(&s->gb) == 0){
-                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
-        } else {
-            mb_type = MB_TYPE_INTRA;
-        }
-        break;
-    case P_TYPE:
-        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
-        if (mb_type < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        mb_type = ptype2mb_type[ mb_type ];
-        break;
-    case B_TYPE:
-        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
-        if (mb_type < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        mb_type = btype2mb_type[ mb_type ];
-        break;
-    }
-    dprintf("mb_type=%x\n", mb_type);
-//    motion_type = 0; /* avoid warning */
-    if (IS_INTRA(mb_type)) {
-        s->dsp.clear_blocks(s->block[0]);
-
-        if(!s->chroma_y_shift){
-            s->dsp.clear_blocks(s->block[6]);
-        }
-
-        /* compute dct type */
-        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
-            !s->frame_pred_frame_dct) {
-            s->interlaced_dct = get_bits1(&s->gb);
-        }
-
-        if (IS_QUANT(mb_type))
-            s->qscale = get_qscale(s);
-
-        if (s->concealment_motion_vectors) {
-            /* just parse them */
-            if (s->picture_structure != PICT_FRAME)
-                skip_bits1(&s->gb); /* field select */
-
-            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
-                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
-            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
-                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
-
-            skip_bits1(&s->gb); /* marker */
-        }else
-            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
-        s->mb_intra = 1;
-#ifdef HAVE_XVMC
-        //one 1 we memcpy blocks in xvmcvideo
-        if(s->avctx->xvmc_acceleration > 1){
-            XVMC_pack_pblocks(s,-1);//inter are always full blocks
-            if(s->swap_uv){
-                exchange_uv(s);
-            }
-        }
-#endif
-
-        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
-            if(s->flags2 & CODEC_FLAG2_FAST){
-                for(i=0;i<6;i++) {
-                    mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
-                }
-            }else{
-                for(i=0;i<mb_block_count;i++) {
-                    if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
-                        return -1;
-                }
-            }
-        } else {
-            for(i=0;i<6;i++) {
-                if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
-                    return -1;
-            }
-        }
-    } else {
-        if (mb_type & MB_TYPE_ZERO_MV){
-            assert(mb_type & MB_TYPE_CBP);
-
-            /* compute dct type */
-            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
-                !s->frame_pred_frame_dct) {
-                s->interlaced_dct = get_bits1(&s->gb);
-            }
-
-            if (IS_QUANT(mb_type))
-                s->qscale = get_qscale(s);
-
-            s->mv_dir = MV_DIR_FORWARD;
-            if(s->picture_structure == PICT_FRAME)
-                s->mv_type = MV_TYPE_16X16;
-            else{
-                s->mv_type = MV_TYPE_FIELD;
-                mb_type |= MB_TYPE_INTERLACED;
-                s->field_select[0][0]= s->picture_structure - 1;
-            }
-            s->last_mv[0][0][0] = 0;
-            s->last_mv[0][0][1] = 0;
-            s->last_mv[0][1][0] = 0;
-            s->last_mv[0][1][1] = 0;
-            s->mv[0][0][0] = 0;
-            s->mv[0][0][1] = 0;
-        }else{
-            assert(mb_type & MB_TYPE_L0L1);
-//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
-            /* get additionnal motion vector type */
-            if (s->frame_pred_frame_dct)
-                motion_type = MT_FRAME;
-            else{
-                motion_type = get_bits(&s->gb, 2);
-            }
-
-            /* compute dct type */
-            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
-                !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
-                s->interlaced_dct = get_bits1(&s->gb);
-            }
-
-            if (IS_QUANT(mb_type))
-                s->qscale = get_qscale(s);
-
-            /* motion vectors */
-            s->mv_dir = 0;
-            for(i=0;i<2;i++) {
-                if (USES_LIST(mb_type, i)) {
-                    s->mv_dir |= (MV_DIR_FORWARD >> i);
-                    dprintf("motion_type=%d\n", motion_type);
-                    switch(motion_type) {
-                    case MT_FRAME: /* or MT_16X8 */
-                        if (s->picture_structure == PICT_FRAME) {
-                            /* MT_FRAME */
-                            mb_type |= MB_TYPE_16x16;
-                            s->mv_type = MV_TYPE_16X16;
-                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
-                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
-                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
-                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
-                            /* full_pel: only for mpeg1 */
-                            if (s->full_pel[i]){
-                                s->mv[i][0][0] <<= 1;
-                                s->mv[i][0][1] <<= 1;
-                            }
-                        } else {
-                            /* MT_16X8 */
-                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
-                            s->mv_type = MV_TYPE_16X8;
-                            for(j=0;j<2;j++) {
-                                s->field_select[i][j] = get_bits1(&s->gb);
-                                for(k=0;k<2;k++) {
-                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
-                                                             s->last_mv[i][j][k]);
-                                    s->last_mv[i][j][k] = val;
-                                    s->mv[i][j][k] = val;
-                                }
-                            }
-                        }
-                        break;
-                    case MT_FIELD:
-                        s->mv_type = MV_TYPE_FIELD;
-                        if (s->picture_structure == PICT_FRAME) {
-                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
-                            for(j=0;j<2;j++) {
-                                s->field_select[i][j] = get_bits1(&s->gb);
-                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
-                                                         s->last_mv[i][j][0]);
-                                s->last_mv[i][j][0] = val;
-                                s->mv[i][j][0] = val;
-                                dprintf("fmx=%d\n", val);
-                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
-                                                         s->last_mv[i][j][1] >> 1);
-                                s->last_mv[i][j][1] = val << 1;
-                                s->mv[i][j][1] = val;
-                                dprintf("fmy=%d\n", val);
-                            }
-                        } else {
-                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
-                            s->field_select[i][0] = get_bits1(&s->gb);
-                            for(k=0;k<2;k++) {
-                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
-                                                         s->last_mv[i][0][k]);
-                                s->last_mv[i][0][k] = val;
-                                s->last_mv[i][1][k] = val;
-                                s->mv[i][0][k] = val;
-                            }
-                        }
-                        break;
-                    case MT_DMV:
-                        {
-                            int dmx, dmy, mx, my, m;
-
-                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
-                                                    s->last_mv[i][0][0]);
-                            s->last_mv[i][0][0] = mx;
-                            s->last_mv[i][1][0] = mx;
-                            dmx = get_dmv(s);
-                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
-                                                    s->last_mv[i][0][1] >> 1);
-                            dmy = get_dmv(s);
-                            s->mv_type = MV_TYPE_DMV;
-
-
-                            s->last_mv[i][0][1] = my<<1;
-                            s->last_mv[i][1][1] = my<<1;
-
-                            s->mv[i][0][0] = mx;
-                            s->mv[i][0][1] = my;
-                            s->mv[i][1][0] = mx;//not used
-                            s->mv[i][1][1] = my;//not used
-
-                            if (s->picture_structure == PICT_FRAME) {
-                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
-
-                                //m = 1 + 2 * s->top_field_first;
-                                m = s->top_field_first ? 1 : 3;
-
-                                /* top -> top pred */
-                                s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
-                                s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
-                                m = 4 - m;
-                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
-                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
-                            } else {
-                                mb_type |= MB_TYPE_16x16;
-
-                                s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
-                                s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
-                                if(s->picture_structure == PICT_TOP_FIELD)
-                                    s->mv[i][2][1]--;
-                                else
-                                    s->mv[i][2][1]++;
-                            }
-                        }
-                        break;
-                    default:
-                        av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
-                        return -1;
-                    }
-                }
-            }
-        }
-
-        s->mb_intra = 0;
-        if (HAS_CBP(mb_type)) {
-            s->dsp.clear_blocks(s->block[0]);
-
-            if(!s->chroma_y_shift){
-                s->dsp.clear_blocks(s->block[6]);
-            }
-
-            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
-            if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
-                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-            if(mb_block_count > 6){
-                 cbp<<= mb_block_count-6;
-                 cbp |= get_bits(&s->gb, mb_block_count-6);
-            }
-
-#ifdef HAVE_XVMC
-            //on 1 we memcpy blocks in xvmcvideo
-            if(s->avctx->xvmc_acceleration > 1){
-                XVMC_pack_pblocks(s,cbp);
-                if(s->swap_uv){
-                    exchange_uv(s);
-                }
-            }
-#endif
-
-            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
-                if(s->flags2 & CODEC_FLAG2_FAST){
-                    for(i=0;i<6;i++) {
-                        if(cbp & 32) {
-                            mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp+=cbp;
-                    }
-                }else{
-                    cbp<<= 12-mb_block_count;
-
-                    for(i=0;i<mb_block_count;i++) {
-                        if ( cbp & (1<<11) ) {
-                            if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
-                                return -1;
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp+=cbp;
-                    }
-                }
-            } else {
-                if(s->flags2 & CODEC_FLAG2_FAST){
-                    for(i=0;i<6;i++) {
-                        if (cbp & 32) {
-                            mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp+=cbp;
-                    }
-                }else{
-                    for(i=0;i<6;i++) {
-                        if (cbp & 32) {
-                            if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
-                                return -1;
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp+=cbp;
-                    }
-                }
-            }
-        }else{
-            for(i=0;i<12;i++)
-                s->block_last_index[i] = -1;
-        }
-    }
-
-    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
-
-    return 0;
-}
-
-/* as h263, but only 17 codes */
-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
-{
-    int code, sign, val, l, shift;
-
-    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
-    if (code == 0) {
-        return pred;
-    }
-    if (code < 0) {
-        return 0xffff;
-    }
-
-    sign = get_bits1(&s->gb);
-    shift = fcode - 1;
-    val = code;
-    if (shift) {
-        val = (val - 1) << shift;
-        val |= get_bits(&s->gb, shift);
-        val++;
-    }
-    if (sign)
-        val = -val;
-    val += pred;
-
-    /* modulo decoding */
-    l= INT_BIT - 5 - shift;
-    val = (val<<l)>>l;
-    return val;
-}
-
-static inline int decode_dc(GetBitContext *gb, int component)
-{
-    int code, diff;
-
-    if (component == 0) {
-        code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
-    } else {
-        code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
-    }
-    if (code < 0){
-        av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
-        return 0xffff;
-    }
-    if (code == 0) {
-        diff = 0;
-    } else {
-        diff = get_xbits(gb, code);
-    }
-    return diff;
-}
-
-static inline int mpeg1_decode_block_intra(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, dc, diff, i, j, run;
-    int component;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const uint16_t *quant_matrix= s->intra_matrix;
-    const int qscale= s->qscale;
-
-    /* DC coef */
-    component = (n <= 3 ? 0 : n - 4 + 1);
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc<<3;
-    dprintf("dc=%d diff=%d\n", dc, diff);
-    i = 0;
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level == 127){
-                break;
-            } else if(level != 0) {
-                i += run;
-                j = scantable[i];
-                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);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= -level;
-                    level= (level*qscale*quant_matrix[j])>>4;
-                    level= (level-1)|1;
-                    level= -level;
-                }else{
-                    level= (level*qscale*quant_matrix[j])>>4;
-                    level= (level-1)|1;
-                }
-            }
-            if (i > 63){
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-
-            block[j] = level;
-        }
-        CLOSE_READER(re, &s->gb);
-    }
-    s->block_last_index[n] = i;
-   return 0;
-}
-
-static inline int mpeg1_decode_block_inter(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const uint16_t *quant_matrix= s->inter_matrix;
-    const int qscale= s->qscale;
-
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        /* special case for the first coef. no need to add a second vlc table */
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level= (3*qscale*quant_matrix[0])>>5;
-            level= (level-1)|1;
-            if(GET_CACHE(re, &s->gb)&0x40000000)
-                level= -level;
-            block[0] = level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
-        }
-
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level != 0) {
-                i += run;
-                j = scantable[i];
-                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);
-                SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= -level;
-                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
-                    level= (level-1)|1;
-                    level= -level;
-                }else{
-                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
-                    level= (level-1)|1;
-                }
-            }
-            if (i > 63){
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-
-            block[j] = level;
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
-        }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
-    }
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const int qscale= s->qscale;
-
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        /* special case for the first coef. no need to add a second vlc table */
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level= (3*qscale)>>1;
-            level= (level-1)|1;
-            if(GET_CACHE(re, &s->gb)&0x40000000)
-                level= -level;
-            block[0] = level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
-        }
-
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level != 0) {
-                i += run;
-                j = scantable[i];
-                level= ((level*2+1)*qscale)>>1;
-                level= (level-1)|1;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= -level;
-                    level= ((level*2+1)*qscale)>>1;
-                    level= (level-1)|1;
-                    level= -level;
-                }else{
-                    level= ((level*2+1)*qscale)>>1;
-                    level= (level-1)|1;
-                }
-            }
-
-            block[j] = level;
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
-        }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
-    }
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-
-static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale= s->qscale;
-    int mismatch;
-
-    mismatch = 1;
-
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        if (n < 4)
-            quant_matrix = s->inter_matrix;
-        else
-            quant_matrix = s->chroma_inter_matrix;
-
-        /* special case for the first coef. no need to add a second vlc table */
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level= (3*qscale*quant_matrix[0])>>5;
-            if(GET_CACHE(re, &s->gb)&0x40000000)
-                level= -level;
-            block[0] = level;
-            mismatch ^= level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
-        }
-
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level != 0) {
-                i += run;
-                j = scantable[i];
-                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
-                    level= -level;
-                }else{
-                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
-                }
-            }
-            if (i > 63){
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-
-            mismatch ^= level;
-            block[j] = level;
-            if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
-        }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
-    }
-    block[63] ^= (mismatch & 1);
-
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &rl_mpeg1;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const int qscale= s->qscale;
-    OPEN_READER(re, &s->gb);
-    i = -1;
-
-    /* special case for the first coef. no need to add a second vlc table */
-    UPDATE_CACHE(re, &s->gb);
-    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-        level= (3*qscale)>>1;
-        if(GET_CACHE(re, &s->gb)&0x40000000)
-            level= -level;
-        block[0] = level;
-        i++;
-        SKIP_BITS(re, &s->gb, 2);
-        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-            goto end;
-    }
-
-    /* now quantify & encode AC coefs */
-    for(;;) {
-        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-        if(level != 0) {
-            i += run;
-            j = scantable[i];
-            level= ((level*2+1)*qscale)>>1;
-            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-            SKIP_BITS(re, &s->gb, 1);
-        } else {
-            /* escape */
-            run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-            UPDATE_CACHE(re, &s->gb);
-            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-
-            i += run;
-            j = scantable[i];
-            if(level<0){
-                level= ((-level*2+1)*qscale)>>1;
-                level= -level;
-            }else{
-                level= ((level*2+1)*qscale)>>1;
-            }
-        }
-
-        block[j] = level;
-        if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-            break;
-        UPDATE_CACHE(re, &s->gb);
-    }
-end:
-    LAST_SKIP_BITS(re, &s->gb, 2);
-    CLOSE_READER(re, &s->gb);
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-
-static inline int mpeg2_decode_block_intra(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, dc, diff, i, j, run;
-    int component;
-    RLTable *rl;
-    uint8_t * const scantable= s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale= s->qscale;
-    int mismatch;
-
-    /* DC coef */
-    if (n < 4){
-        quant_matrix = s->intra_matrix;
-        component = 0;
-    }else{
-        quant_matrix = s->chroma_intra_matrix;
-        component = (n&1) + 1;
-    }
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc << (3 - s->intra_dc_precision);
-    dprintf("dc=%d\n", block[0]);
-    mismatch = block[0] ^ 1;
-    i = 0;
-    if (s->intra_vlc_format)
-        rl = &rl_mpeg2;
-    else
-        rl = &rl_mpeg1;
-
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level == 127){
-                break;
-            } else if(level != 0) {
-                i += run;
-                j = scantable[i];
-                level= (level*qscale*quant_matrix[j])>>4;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-                i += run;
-                j = scantable[i];
-                if(level<0){
-                    level= (-level*qscale*quant_matrix[j])>>4;
-                    level= -level;
-                }else{
-                    level= (level*qscale*quant_matrix[j])>>4;
-                }
-            }
-            if (i > 63){
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-
-            mismatch^= level;
-            block[j] = level;
-        }
-        CLOSE_READER(re, &s->gb);
-    }
-    block[63]^= mismatch&1;
-
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
-                               DCTELEM *block,
-                               int n)
-{
-    int level, dc, diff, j, run;
-    int component;
-    RLTable *rl;
-    uint8_t * scantable= s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale= s->qscale;
-
-    /* DC coef */
-    if (n < 4){
-        quant_matrix = s->intra_matrix;
-        component = 0;
-    }else{
-        quant_matrix = s->chroma_intra_matrix;
-        component = (n&1) + 1;
-    }
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc << (3 - s->intra_dc_precision);
-    if (s->intra_vlc_format)
-        rl = &rl_mpeg2;
-    else
-        rl = &rl_mpeg1;
-
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefs */
-        for(;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if(level == 127){
-                break;
-            } else if(level != 0) {
-                scantable += run;
-                j = *scantable;
-                level= (level*qscale*quant_matrix[j])>>4;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-                scantable += run;
-                j = *scantable;
-                if(level<0){
-                    level= (-level*qscale*quant_matrix[j])>>4;
-                    level= -level;
-                }else{
-                    level= (level*qscale*quant_matrix[j])>>4;
-                }
-            }
-
-            block[j] = level;
-        }
-        CLOSE_READER(re, &s->gb);
-    }
-
-    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
-    return 0;
-}
-
-typedef struct Mpeg1Context {
-    MpegEncContext mpeg_enc_ctx;
-    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
-    int repeat_field; /* true if we must repeat the field */
-    AVPanScan pan_scan; /** some temporary storage for the panscan */
-    int slice_count;
-    int swap_uv;//indicate VCR2
-    int save_aspect_info;
-    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
-
-} Mpeg1Context;
-
-static int mpeg_decode_init(AVCodecContext *avctx)
-{
-    Mpeg1Context *s = avctx->priv_data;
-    MpegEncContext *s2 = &s->mpeg_enc_ctx;
-    int i;
-
-    //we need some parmutation to store
-    //matrixes, until MPV_common_init()
-    //set the real permutatuon
-    for(i=0;i<64;i++)
-       s2->dsp.idct_permutation[i]=i;
-
-    MPV_decode_defaults(s2);
-
-    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();
-
-    s->mpeg_enc_ctx_allocated = 0;
-    s->mpeg_enc_ctx.picture_number = 0;
-    s->repeat_field = 0;
-    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
-    return 0;
-}
-
-static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
-                                     const uint8_t *new_perm){
-    uint16_t temp_matrix[64];
-    int i;
-
-    memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
-
-    for(i=0;i<64;i++){
-        matrix[new_perm[i]] = temp_matrix[old_perm[i]];
-    }
-}
-
-//Call this function when we know all parameters
-//it may be called in different places for mpeg1 and mpeg2
-static int mpeg_decode_postinit(AVCodecContext *avctx){
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    uint8_t old_permutation[64];
-
-    if (
-        (s1->mpeg_enc_ctx_allocated == 0)||
-        avctx->coded_width  != s->width ||
-        avctx->coded_height != s->height||
-        s1->save_aspect_info != s->aspect_ratio_info||
-        0)
-    {
-
-        if (s1->mpeg_enc_ctx_allocated) {
-            ParseContext pc= s->parse_context;
-            s->parse_context.buffer=0;
-            MPV_common_end(s);
-            s->parse_context= pc;
-        }
-
-        if( (s->width == 0 )||(s->height == 0))
-            return -2;
-
-        avcodec_set_dimensions(avctx, s->width, s->height);
-        avctx->bit_rate = s->bit_rate;
-        s1->save_aspect_info = s->aspect_ratio_info;
-
-     //low_delay may be forced, in this case we will have B frames
-     //that behave like P frames
-        avctx->has_b_frames = !(s->low_delay);
-
-        if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
-            //mpeg1 fps
-            avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
-            avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
-            //mpeg1 aspect
-            avctx->sample_aspect_ratio= av_d2q(
-                    1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
-
-        }else{//mpeg2
-        //mpeg2 fps
-            av_reduce(
-                &s->avctx->time_base.den,
-                &s->avctx->time_base.num,
-                ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
-                ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
-                1<<30);
-        //mpeg2 aspect
-            if(s->aspect_ratio_info > 1){
-                if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
-                    s->avctx->sample_aspect_ratio=
-                        av_div_q(
-                         mpeg2_aspect[s->aspect_ratio_info],
-                         (AVRational){s->width, s->height}
-                         );
-                }else{
-                    s->avctx->sample_aspect_ratio=
-                        av_div_q(
-                         mpeg2_aspect[s->aspect_ratio_info],
-                         (AVRational){s1->pan_scan.width, s1->pan_scan.height}
-                        );
-                }
-            }else{
-                s->avctx->sample_aspect_ratio=
-                    mpeg2_aspect[s->aspect_ratio_info];
-            }
-        }//mpeg2
-
-        if(avctx->xvmc_acceleration){
-            avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
-        }else{
-            if(s->chroma_format <  2){
-                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
-            }else
-            if(s->chroma_format == 2){
-                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
-            }else
-            if(s->chroma_format >  2){
-                avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
-            }
-        }
-        //until then pix_fmt may be changed right after codec init
-        if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
-            if( avctx->idct_algo == FF_IDCT_AUTO )
-                avctx->idct_algo = FF_IDCT_SIMPLE;
-
-        //quantization matrixes may need reordering
-        //if dct permutation is changed
-        memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
-
-        if (MPV_common_init(s) < 0)
-            return -2;
-
-        quant_matrix_rebuild(s->intra_matrix,       old_permutation,s->dsp.idct_permutation);
-        quant_matrix_rebuild(s->inter_matrix,       old_permutation,s->dsp.idct_permutation);
-        quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
-        quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
-
-        s1->mpeg_enc_ctx_allocated = 1;
-    }
-    return 0;
-}
-
-static int mpeg1_decode_picture(AVCodecContext *avctx,
-                                const uint8_t *buf, int buf_size)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int ref, f_code, vbv_delay;
-
-    if(mpeg_decode_postinit(s->avctx) < 0)
-       return -2;
-
-    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);
-    if(s->pict_type == 0 || s->pict_type > 3)
-        return -1;
-
-    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);
-        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
-            return -1;
-        s->mpeg_f_code[0][0] = f_code;
-        s->mpeg_f_code[0][1] = f_code;
-    }
-    if (s->pict_type == B_TYPE) {
-        s->full_pel[1] = get_bits1(&s->gb);
-        f_code = get_bits(&s->gb, 3);
-        if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
-            return -1;
-        s->mpeg_f_code[1][0] = f_code;
-        s->mpeg_f_code[1][1] = f_code;
-    }
-    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 type:%d\n", vbv_delay, ref, s->pict_type);
-
-    s->y_dc_scale = 8;
-    s->c_dc_scale = 8;
-    s->first_slice = 1;
-    return 0;
-}
-
-static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
-{
-    MpegEncContext *s= &s1->mpeg_enc_ctx;
-    int horiz_size_ext, vert_size_ext;
-    int bit_rate_ext;
-
-    skip_bits(&s->gb, 1); /* profil and level esc*/
-    s->avctx->profile= get_bits(&s->gb, 3);
-    s->avctx->level= get_bits(&s->gb, 4);
-    s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
-    s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
-    horiz_size_ext = get_bits(&s->gb, 2);
-    vert_size_ext = get_bits(&s->gb, 2);
-    s->width |= (horiz_size_ext << 12);
-    s->height |= (vert_size_ext << 12);
-    bit_rate_ext = get_bits(&s->gb, 12);  /* XXX: handle it */
-    s->bit_rate += (bit_rate_ext << 18) * 400;
-    skip_bits1(&s->gb); /* marker */
-    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;
-
-    s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
-    s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
-
-    dprintf("sequence extension\n");
-    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
-    s->avctx->sub_id = 2; /* indicates mpeg2 found */
-
-    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
-        av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
-               s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
-
-}
-
-static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
-{
-    MpegEncContext *s= &s1->mpeg_enc_ctx;
-    int color_description, w, h;
-
-    skip_bits(&s->gb, 3); /* video format */
-    color_description= get_bits1(&s->gb);
-    if(color_description){
-        skip_bits(&s->gb, 8); /* color primaries */
-        skip_bits(&s->gb, 8); /* transfer_characteristics */
-        skip_bits(&s->gb, 8); /* matrix_coefficients */
-    }
-    w= get_bits(&s->gb, 14);
-    skip_bits(&s->gb, 1); //marker
-    h= get_bits(&s->gb, 14);
-    skip_bits(&s->gb, 1); //marker
-
-    s1->pan_scan.width= 16*w;
-    s1->pan_scan.height=16*h;
-
-    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
-        av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
-}
-
-static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
-{
-    MpegEncContext *s= &s1->mpeg_enc_ctx;
-    int i,nofco;
-
-    nofco = 1;
-    if(s->progressive_sequence){
-        if(s->repeat_first_field){
-            nofco++;
-            if(s->top_field_first)
-                nofco++;
-        }
-    }else{
-        if(s->picture_structure == PICT_FRAME){
-            nofco++;
-            if(s->repeat_first_field)
-                nofco++;
-        }
-    }
-    for(i=0; i<nofco; i++){
-        s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
-        skip_bits(&s->gb, 1); //marker
-        s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
-        skip_bits(&s->gb, 1); //marker
-    }
-
-    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
-        av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
-            s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
-            s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
-            s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
-        );
-}
-
-static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
-{
-    int i, v, j;
-
-    dprintf("matrix extension\n");
-
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->intra_matrix[j] = v;
-            s->chroma_intra_matrix[j] = v;
-        }
-    }
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->inter_matrix[j] = v;
-            s->chroma_inter_matrix[j] = v;
-        }
-    }
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->chroma_intra_matrix[j] = v;
-        }
-    }
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->chroma_inter_matrix[j] = v;
-        }
-    }
-}
-
-static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
-{
-    s->full_pel[0] = s->full_pel[1] = 0;
-    s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
-    s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
-    s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
-    s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
-    s->intra_dc_precision = get_bits(&s->gb, 2);
-    s->picture_structure = get_bits(&s->gb, 2);
-    s->top_field_first = get_bits1(&s->gb);
-    s->frame_pred_frame_dct = get_bits1(&s->gb);
-    s->concealment_motion_vectors = get_bits1(&s->gb);
-    s->q_scale_type = get_bits1(&s->gb);
-    s->intra_vlc_format = get_bits1(&s->gb);
-    s->alternate_scan = get_bits1(&s->gb);
-    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{
-        s->first_field ^= 1;
-        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
-    }
-
-    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);
-    }
-
-    /* composite display not parsed */
-    dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
-    dprintf("picture_structure=%d\n", s->picture_structure);
-    dprintf("top field first=%d\n", s->top_field_first);
-    dprintf("repeat first field=%d\n", s->repeat_first_field);
-    dprintf("conceal=%d\n", s->concealment_motion_vectors);
-    dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
-    dprintf("alternate_scan=%d\n", s->alternate_scan);
-    dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
-    dprintf("progressive_frame=%d\n", s->progressive_frame);
-}
-
-static void mpeg_decode_extension(AVCodecContext *avctx,
-                                  const uint8_t *buf, int buf_size)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int ext_type;
-
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    ext_type = get_bits(&s->gb, 4);
-    switch(ext_type) {
-    case 0x1:
-        mpeg_decode_sequence_extension(s1);
-        break;
-    case 0x2:
-        mpeg_decode_sequence_display_extension(s1);
-        break;
-    case 0x3:
-        mpeg_decode_quant_matrix_extension(s);
-        break;
-    case 0x7:
-        mpeg_decode_picture_display_extension(s1);
-        break;
-    case 0x8:
-        mpeg_decode_picture_coding_extension(s);
-        break;
-    }
-}
-
-static void exchange_uv(MpegEncContext *s){
-    short * tmp = s->pblocks[4];
-    s->pblocks[4] = s->pblocks[5];
-    s->pblocks[5] = tmp;
-}
-
-static int mpeg_field_start(MpegEncContext *s){
-    AVCodecContext *avctx= s->avctx;
-    Mpeg1Context *s1 = (Mpeg1Context*)s;
-
-    /* start frame decoding */
-    if(s->first_field || s->picture_structure==PICT_FRAME){
-        if(MPV_frame_start(s, avctx) < 0)
-            return -1;
-
-        ff_er_frame_start(s);
-
-        /* first check if we must repeat the frame */
-        s->current_picture_ptr->repeat_pict = 0;
-        if (s->repeat_first_field) {
-            if (s->progressive_sequence) {
-                if (s->top_field_first)
-                    s->current_picture_ptr->repeat_pict = 4;
-                else
-                    s->current_picture_ptr->repeat_pict = 2;
-            } else if (s->progressive_frame) {
-                s->current_picture_ptr->repeat_pict = 1;
-            }
-        }
-
-        *s->current_picture_ptr->pan_scan= s1->pan_scan;
-    }else{ //second field
-            int i;
-
-            if(!s->current_picture_ptr){
-                av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
-                return -1;
-            }
-
-            for(i=0; i<4; i++){
-                s->current_picture.data[i] = s->current_picture_ptr->data[i];
-                if(s->picture_structure == PICT_BOTTOM_FIELD){
-                    s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
-                }
-            }
-    }
-#ifdef HAVE_XVMC
-// MPV_frame_start will call this function too,
-// but we need to call it on every field
-    if(s->avctx->xvmc_acceleration)
-         XVMC_field_start(s,avctx);
-#endif
-
-    return 0;
-}
-
-#define DECODE_SLICE_ERROR -1
-#define DECODE_SLICE_OK 0
-
-/**
- * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
- * @return DECODE_SLICE_ERROR if the slice is damaged<br>
- *         DECODE_SLICE_OK if this slice is ok<br>
- */
-static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
-                             const uint8_t **buf, int buf_size)
-{
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    AVCodecContext *avctx= s->avctx;
-    int ret;
-    const int field_pic= s->picture_structure != PICT_FRAME;
-    const int lowres= s->avctx->lowres;
-
-    s->resync_mb_x=
-    s->resync_mb_y= -1;
-
-    if (mb_y<<field_pic >= s->mb_height){
-        av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
-        return -1;
-    }
-
-    init_get_bits(&s->gb, *buf, buf_size*8);
-
-    ff_mpeg1_clean_buffers(s);
-    s->interlaced_dct = 0;
-
-    s->qscale = get_qscale(s);
-
-    if(s->qscale == 0){
-        av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
-        return -1;
-    }
-
-    /* extra slice info */
-    while (get_bits1(&s->gb) != 0) {
-        skip_bits(&s->gb, 8);
-    }
-
-    s->mb_x=0;
-
-    for(;;) {
-        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
-        if (code < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
-            return -1;
-        }
-        if (code >= 33) {
-            if (code == 33) {
-                s->mb_x += 33;
-            }
-            /* otherwise, stuffing, nothing to do */
-        } else {
-            s->mb_x += code;
-            break;
-        }
-    }
-
-    s->resync_mb_x= s->mb_x;
-    s->resync_mb_y= s->mb_y= mb_y;
-    s->mb_skip_run= 0;
-    ff_init_block_index(s);
-
-    if (s->mb_y==0 && s->mb_x==0 && (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 %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 ? "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" :"");
-        }
-    }
-
-    for(;;) {
-#ifdef HAVE_XVMC
-        //one 1 we memcpy blocks in xvmcvideo
-        if(s->avctx->xvmc_acceleration > 1)
-            XVMC_init_block(s);//set s->block
-#endif
-
-        ret = mpeg_decode_mb(s, s->block);
-        s->chroma_qscale= s->qscale;
-
-        dprintf("ret=%d\n", ret);
-        if (ret < 0)
-            return -1;
-
-        if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
-            const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
-            int xy = s->mb_x*2 + s->mb_y*2*wrap;
-            int motion_x, motion_y, dir, i;
-            if(field_pic && !s->first_field)
-                xy += wrap/2;
-
-            for(i=0; i<2; i++){
-                for(dir=0; dir<2; dir++){
-                    if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
-                        motion_x = motion_y = 0;
-                    }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
-                        motion_x = s->mv[dir][0][0];
-                        motion_y = s->mv[dir][0][1];
-                    } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
-                        motion_x = s->mv[dir][i][0];
-                        motion_y = s->mv[dir][i][1];
-                    }
-
-                    s->current_picture.motion_val[dir][xy    ][0] = motion_x;
-                    s->current_picture.motion_val[dir][xy    ][1] = motion_y;
-                    s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
-                    s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
-                    s->current_picture.ref_index [dir][xy    ]=
-                    s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
-                    assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
-                }
-                xy += wrap;
-            }
-        }
-
-        s->dest[0] += 16 >> lowres;
-        s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
-        s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
-
-        MPV_decode_mb(s, s->block);
-
-        if (++s->mb_x >= s->mb_width) {
-            const int mb_size= 16>>s->avctx->lowres;
-
-            ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
-
-            s->mb_x = 0;
-            s->mb_y++;
-
-            if(s->mb_y<<field_pic >= s->mb_height){
-                int left= s->gb.size_in_bits - get_bits_count(&s->gb);
-                int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
-                            && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
-                            && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
-
-                if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
-                   || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
-                    av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
-                    return -1;
-                }else
-                    goto eos;
-            }
-
-            ff_init_block_index(s);
-        }
-
-        /* skip mb handling */
-        if (s->mb_skip_run == -1) {
-            /* read again increment */
-            s->mb_skip_run = 0;
-            for(;;) {
-                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
-                if (code < 0){
-                    av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
-                    return -1;
-                }
-                if (code >= 33) {
-                    if (code == 33) {
-                        s->mb_skip_run += 33;
-                    }else if(code == 35){
-                        if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
-                            av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
-                            return -1;
-                        }
-                        goto eos; /* end of slice */
-                    }
-                    /* otherwise, stuffing, nothing to do */
-                } else {
-                    s->mb_skip_run += code;
-                    break;
-                }
-            }
-        }
-    }
-eos: // end of slice
-    *buf += get_bits_count(&s->gb)/8 - 1;
-//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
-    return 0;
-}
-
-static int slice_decode_thread(AVCodecContext *c, void *arg){
-    MpegEncContext *s= arg;
-    const uint8_t *buf= s->gb.buffer;
-    int mb_y= s->start_mb_y;
-
-    s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
-
-    for(;;){
-        uint32_t start_code;
-        int ret;
-
-        ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
-        emms_c();
-//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
-//ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
-        if(ret < 0){
-            if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
-                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
-        }else{
-            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
-        }
-
-        if(s->mb_y == s->end_mb_y)
-            return 0;
-
-        start_code= -1;
-        buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
-        mb_y= start_code - SLICE_MIN_START_CODE;
-        if(mb_y < 0 || mb_y >= s->end_mb_y)
-            return -1;
-    }
-
-    return 0; //not reached
-}
-
-/**
- * handles slice ends.
- * @return 1 if it seems to be the last slice of
- */
-static int slice_end(AVCodecContext *avctx, AVFrame *pict)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-
-    if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
-        return 0;
-
-#ifdef HAVE_XVMC
-    if(s->avctx->xvmc_acceleration)
-        XVMC_field_end(s);
-#endif
-    /* end of slice reached */
-    if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
-        /* end of image */
-
-        s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
-
-        ff_er_frame_end(s);
-
-        MPV_frame_end(s);
-
-        if (s->pict_type == B_TYPE || s->low_delay) {
-            *pict= *(AVFrame*)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, pict);
-            }
-        }
-
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
-static int mpeg1_decode_sequence(AVCodecContext *avctx,
-                                 const uint8_t *buf, int buf_size)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int width,height;
-    int i, v, j;
-
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    width = get_bits(&s->gb, 12);
-    height = get_bits(&s->gb, 12);
-    if (width <= 0 || height <= 0 ||
-        (width % 2) != 0 || (height % 2) != 0)
-        return -1;
-    s->aspect_ratio_info= get_bits(&s->gb, 4);
-    if (s->aspect_ratio_info == 0)
-        return -1;
-    s->frame_rate_index = get_bits(&s->gb, 4);
-    if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
-        return -1;
-    s->bit_rate = get_bits(&s->gb, 18) * 400;
-    if (get_bits1(&s->gb) == 0) /* marker */
-        return -1;
-    s->width = width;
-    s->height = height;
-
-    s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
-    skip_bits(&s->gb, 1);
-
-    /* get matrix */
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            if(v==0){
-                av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
-                return -1;
-            }
-            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->intra_matrix[j] = v;
-            s->chroma_intra_matrix[j] = v;
-        }
-#ifdef DEBUG
-        dprintf("intra matrix present\n");
-        for(i=0;i<64;i++)
-            dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
-        dprintf("\n");
-#endif
-    } else {
-        for(i=0;i<64;i++) {
-            j = s->dsp.idct_permutation[i];
-            v = ff_mpeg1_default_intra_matrix[i];
-            s->intra_matrix[j] = v;
-            s->chroma_intra_matrix[j] = v;
-        }
-    }
-    if (get_bits1(&s->gb)) {
-        for(i=0;i<64;i++) {
-            v = get_bits(&s->gb, 8);
-            if(v==0){
-                av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
-                return -1;
-            }
-            j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
-            s->inter_matrix[j] = v;
-            s->chroma_inter_matrix[j] = v;
-        }
-#ifdef DEBUG
-        dprintf("non intra matrix present\n");
-        for(i=0;i<64;i++)
-            dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
-        dprintf("\n");
-#endif
-    } else {
-        for(i=0;i<64;i++) {
-            int j= s->dsp.idct_permutation[i];
-            v = ff_mpeg1_default_non_intra_matrix[i];
-            s->inter_matrix[j] = v;
-            s->chroma_inter_matrix[j] = v;
-        }
-    }
-
-    if(show_bits(&s->gb, 23) != 0){
-        av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
-        return -1;
-    }
-
-    /* we set mpeg2 parameters so that it emulates mpeg1 */
-    s->progressive_sequence = 1;
-    s->progressive_frame = 1;
-    s->picture_structure = PICT_FRAME;
-    s->frame_pred_frame_dct = 1;
-    s->chroma_format = 1;
-    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
-    avctx->sub_id = 1; /* indicates mpeg1 */
-    s->out_format = FMT_MPEG1;
-    s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
-    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;
-}
-
-static int vcr2_init_sequence(AVCodecContext *avctx)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-    int i, v;
-
-    /* start new mpeg1 context decoding */
-    s->out_format = FMT_MPEG1;
-    if (s1->mpeg_enc_ctx_allocated) {
-        MPV_common_end(s);
-    }
-    s->width  = avctx->coded_width;
-    s->height = avctx->coded_height;
-    avctx->has_b_frames= 0; //true?
-    s->low_delay= 1;
-
-    if(avctx->xvmc_acceleration){
-        avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
-    }else{
-        avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
-    }
-
-    if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
-        if( avctx->idct_algo == FF_IDCT_AUTO )
-            avctx->idct_algo = FF_IDCT_SIMPLE;
-
-    if (MPV_common_init(s) < 0)
-        return -1;
-    exchange_uv(s);//common init reset pblocks, so we swap them here
-    s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
-    s1->mpeg_enc_ctx_allocated = 1;
-
-    for(i=0;i<64;i++) {
-        int j= s->dsp.idct_permutation[i];
-        v = ff_mpeg1_default_intra_matrix[i];
-        s->intra_matrix[j] = v;
-        s->chroma_intra_matrix[j] = v;
-
-        v = ff_mpeg1_default_non_intra_matrix[i];
-        s->inter_matrix[j] = v;
-        s->chroma_inter_matrix[j] = v;
-    }
-
-    s->progressive_sequence = 1;
-    s->progressive_frame = 1;
-    s->picture_structure = PICT_FRAME;
-    s->frame_pred_frame_dct = 1;
-    s->chroma_format = 1;
-    s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
-    avctx->sub_id = 2; /* indicates mpeg2 */
-    return 0;
-}
-
-
-static void mpeg_decode_user_data(AVCodecContext *avctx,
-                                  const uint8_t *buf, int buf_size)
-{
-    const uint8_t *p;
-    int len, flags;
-    p = buf;
-    len = buf_size;
-
-    /* we parse the DTG active format information */
-    if (len >= 5 &&
-        p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
-        flags = p[4];
-        p += 5;
-        len -= 5;
-        if (flags & 0x80) {
-            /* skip event id */
-            if (len < 2)
-                return;
-            p += 2;
-            len -= 2;
-        }
-        if (flags & 0x40) {
-            if (len < 1)
-                return;
-            avctx->dtg_active_format = p[0] & 0x0f;
-        }
-    }
-}
-
-static void mpeg_decode_gop(AVCodecContext *avctx,
-                            const uint8_t *buf, int buf_size){
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-
-    int drop_frame_flag;
-    int time_code_hours, time_code_minutes;
-    int time_code_seconds, time_code_pictures;
-    int broken_link;
-
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    drop_frame_flag = get_bits1(&s->gb);
-
-    time_code_hours=get_bits(&s->gb,5);
-    time_code_minutes = get_bits(&s->gb,6);
-    skip_bits1(&s->gb);//marker bit
-    time_code_seconds = get_bits(&s->gb,6);
-    time_code_pictures = get_bits(&s->gb,6);
-
-    /*broken_link indicate that after editing the
-      reference frames of the first B-Frames after GOP I-Frame
-      are missing (open gop)*/
-    broken_link = get_bits1(&s->gb);
-
-    if(s->avctx->debug & FF_DEBUG_PICT_INFO)
-        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
-            time_code_hours, time_code_minutes, time_code_seconds,
-            time_code_pictures, broken_link);
-}
-/**
- * finds the end of the current frame in the bitstream.
- * @return the position of the first byte of the next frame, or -1
- */
-int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
-{
-    int i;
-    uint32_t state= pc->state;
-
-    i=0;
-    if(!pc->frame_start_found){
-        for(i=0; i<buf_size; i++){
-            i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
-            if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
-                i++;
-                pc->frame_start_found=1;
-                break;
-            }
-        }
-    }
-
-    if(pc->frame_start_found){
-        /* EOF considered as end of frame */
-        if (buf_size == 0)
-            return 0;
-        for(; i<buf_size; i++){
-            i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
-            if((state&0xFFFFFF00) == 0x100){
-                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
-                    pc->frame_start_found=0;
-                    pc->state=-1;
-                    return i-3;
-                }
-            }
-        }
-    }
-    pc->state= state;
-    return END_NOT_FOUND;
-}
-
-/* handle buffering and image synchronisation */
-static int mpeg_decode_frame(AVCodecContext *avctx,
-                             void *data, int *data_size,
-                             uint8_t *buf, int buf_size)
-{
-    Mpeg1Context *s = avctx->priv_data;
-    const uint8_t *buf_end;
-    const uint8_t *buf_ptr;
-    uint32_t start_code;
-    int ret, input_size;
-    AVFrame *picture = data;
-    MpegEncContext *s2 = &s->mpeg_enc_ctx;
-    dprintf("fill_buffer\n");
-
-    if (buf_size == 0) {
-        /* special case for last picture */
-        if (s2->low_delay==0 && s2->next_picture_ptr) {
-            *picture= *(AVFrame*)s2->next_picture_ptr;
-            s2->next_picture_ptr= NULL;
-
-            *data_size = sizeof(AVFrame);
-        }
-        return 0;
-    }
-
-    if(s2->flags&CODEC_FLAG_TRUNCATED){
-        int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
-
-        if( ff_combine_frame(&s2->parse_context, next, &buf, &buf_size) < 0 )
-            return buf_size;
-    }
-
-    buf_ptr = buf;
-    buf_end = buf + buf_size;
-
-#if 0
-    if (s->repeat_field % 2 == 1) {
-        s->repeat_field++;
-        //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
-        //        s2->picture_number, s->repeat_field);
-        if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
-            *data_size = sizeof(AVPicture);
-            goto the_end;
-        }
-    }
-#endif
-
-    if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
-        vcr2_init_sequence(avctx);
-
-    s->slice_count= 0;
-
-    for(;;) {
-        /* find start next code */
-        start_code = -1;
-        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
-        if (start_code > 0x1ff){
-            if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
-                if(avctx->thread_count > 1){
-                    int i;
-
-                    avctx->execute(avctx, slice_decode_thread,  (void**)&(s2->thread_context[0]), NULL, s->slice_count);
-                    for(i=0; i<s->slice_count; i++)
-                        s2->error_count += s2->thread_context[i]->error_count;
-                }
-                if (slice_end(avctx, picture)) {
-                    if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
-                        *data_size = sizeof(AVPicture);
-                }
-            }
-            return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
-        }
-
-        input_size = buf_end - buf_ptr;
-
-        if(avctx->debug & FF_DEBUG_STARTCODE){
-            av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
-        }
-
-                /* prepare data for next start code */
-                switch(start_code) {
-                case SEQ_START_CODE:
-                    mpeg1_decode_sequence(avctx, buf_ptr,
-                                          input_size);
-                    break;
-
-                case PICTURE_START_CODE:
-                    /* we have a complete image : we try to decompress it */
-                    mpeg1_decode_picture(avctx,
-                                         buf_ptr, input_size);
-                    break;
-                case EXT_START_CODE:
-                    mpeg_decode_extension(avctx,
-                                          buf_ptr, input_size);
-                    break;
-                case USER_START_CODE:
-                    mpeg_decode_user_data(avctx,
-                                          buf_ptr, input_size);
-                    break;
-                case GOP_START_CODE:
-                    s2->first_field=0;
-                    mpeg_decode_gop(avctx,
-                                          buf_ptr, input_size);
-                    break;
-                default:
-                    if (start_code >= SLICE_MIN_START_CODE &&
-                        start_code <= SLICE_MAX_START_CODE) {
-                        int mb_y= start_code - SLICE_MIN_START_CODE;
-
-                        if(s2->last_picture_ptr==NULL){
-                        /* skip b frames if we dont have reference frames */
-                            if(s2->pict_type==B_TYPE) break;
-                        /* skip P frames if we dont have reference frame no valid header */
-//                            if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
-                        }
-                        /* skip b frames if we are in a hurry */
-                        if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
-                        if(  (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
-                           ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
-                           || avctx->skip_frame >= AVDISCARD_ALL)
-                            break;
-                        /* skip everything if we are in a hurry>=5 */
-                        if(avctx->hurry_up>=5) break;
-
-                        if (!s->mpeg_enc_ctx_allocated) break;
-
-                        if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
-                            if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
-                                break;
-                        }
-
-                        if(s2->first_slice){
-                            s2->first_slice=0;
-                            if(mpeg_field_start(s2) < 0)
-                                return -1;
-                        }
-
-                        if(avctx->thread_count > 1){
-                            int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
-                            if(threshold <= mb_y){
-                                MpegEncContext *thread_context= s2->thread_context[s->slice_count];
-
-                                thread_context->start_mb_y= mb_y;
-                                thread_context->end_mb_y  = s2->mb_height;
-                                if(s->slice_count){
-                                    s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
-                                    ff_update_duplicate_context(thread_context, s2);
-                                }
-                                init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
-                                s->slice_count++;
-                            }
-                            buf_ptr += 2; //FIXME add minimum num of bytes per slice
-                        }else{
-                            ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
-                            emms_c();
-
-                            if(ret < 0){
-                                if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
-                                    ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
-                            }else{
-                                ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
-                            }
-                        }
-                    }
-                    break;
-                }
-    }
-}
-
-static int mpeg_decode_end(AVCodecContext *avctx)
-{
-    Mpeg1Context *s = avctx->priv_data;
-
-    if (s->mpeg_enc_ctx_allocated)
-        MPV_common_end(&s->mpeg_enc_ctx);
-    return 0;
-}
-
-AVCodec mpeg1video_decoder = {
-    "mpeg1video",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MPEG1VIDEO,
-    sizeof(Mpeg1Context),
-    mpeg_decode_init,
-    NULL,
-    mpeg_decode_end,
-    mpeg_decode_frame,
-    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
-    .flush= ff_mpeg_flush,
-};
-
-AVCodec mpeg2video_decoder = {
-    "mpeg2video",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MPEG2VIDEO,
-    sizeof(Mpeg1Context),
-    mpeg_decode_init,
-    NULL,
-    mpeg_decode_end,
-    mpeg_decode_frame,
-    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
-    .flush= ff_mpeg_flush,
-};
-
-//legacy decoder
-AVCodec mpegvideo_decoder = {
-    "mpegvideo",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MPEG2VIDEO,
-    sizeof(Mpeg1Context),
-    mpeg_decode_init,
-    NULL,
-    mpeg_decode_end,
-    mpeg_decode_frame,
-    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
-    .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,
-    .supported_framerates= ff_frame_rate_tab+1,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
-    .capabilities= CODEC_CAP_DELAY,
-};
-
-AVCodec mpeg2video_encoder = {
-    "mpeg2video",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MPEG2VIDEO,
-    sizeof(MpegEncContext),
-    encode_init,
-    MPV_encode_picture,
-    MPV_encode_end,
-    .supported_framerates= ff_frame_rate_tab+1,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
-    .capabilities= CODEC_CAP_DELAY,
-};
-#endif
-
-#ifdef HAVE_XVMC
-static int mpeg_mc_decode_init(AVCodecContext *avctx){
-    Mpeg1Context *s;
-
-    if( avctx->thread_count > 1)
-        return -1;
-    if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
-        return -1;
-    if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
-        dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
-    }
-    mpeg_decode_init(avctx);
-    s = avctx->priv_data;
-
-    avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
-    avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
-
-    return 0;
-}
-
-AVCodec mpeg_xvmc_decoder = {
-    "mpegvideo_xvmc",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_MPEG2VIDEO_XVMC,
-    sizeof(Mpeg1Context),
-    mpeg_mc_decode_init,
-    NULL,
-    mpeg_decode_end,
-    mpeg_decode_frame,
-    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
-    .flush= ff_mpeg_flush,
-};
-
-#endif
-
-/* this is ugly i know, but the alternative is too make
-   hundreds of vars global and prefix them with ff_mpeg1_
-   which is far uglier. */
-#include "mdec.c"
--- a/src/ffmpeg/libavcodec/msmpeg4.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1938 +0,0 @@
-/*
- * MSMPEG4 backend for ffmpeg encoder and decoder
- * Copyright (c) 2001 Fabrice Bellard.
- * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
- */
-
-/**
- * @file msmpeg4.c
- * MSMPEG4 backend for ffmpeg encoder and decoder.
- */
-
-#include "avcodec.h"
-#include "dsputil.h"
-#include "mpegvideo.h"
-
-/*
- * You can also call this codec : MPEG4 with a twist !
- *
- * TODO:
- *        - (encoding) select best mv table (two choices)
- *        - (encoding) select best vlc/dc table
- */
-//#define DEBUG
-
-#define DC_VLC_BITS 9
-#define CBPY_VLC_BITS 6
-#define INTER_INTRA_VLC_BITS 3
-#define V1_INTRA_CBPC_VLC_BITS 6
-#define V1_INTER_CBPC_VLC_BITS 6
-#define V2_INTRA_CBPC_VLC_BITS 3
-#define V2_MB_TYPE_VLC_BITS 7
-#define MV_VLC_BITS 9
-#define V2_MV_VLC_BITS 9
-#define TEX_VLC_BITS 9
-#define MB_NON_INTRA_VLC_BITS 9
-#define MB_INTRA_VLC_BITS 9
-
-#define II_BITRATE 128*1024
-#define MBAC_BITRATE 50*1024
-
-#define DEFAULT_INTER_INDEX 3
-
-static uint32_t v2_dc_lum_table[512][2];
-static uint32_t v2_dc_chroma_table[512][2];
-
-static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
-static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
-                                       int n, int coded, const uint8_t *scantable);
-static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
-static int msmpeg4_decode_motion(MpegEncContext * s,
-                                 int *mx_ptr, int *my_ptr);
-static void init_h263_dc_for_msmpeg4(void);
-static inline void msmpeg4_memsetw(short *tab, int val, int n);
-#ifdef CONFIG_ENCODERS
-static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
-static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
-#endif //CONFIG_ENCODERS
-static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
-static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
-static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
-
-/* vc1 externs */
-extern uint8_t wmv3_dc_scale_table[32];
-
-#ifdef DEBUG
-int intra_count = 0;
-int frame_count = 0;
-#endif
-
-#include "msmpeg4data.h"
-
-#ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references
-static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
-#endif //CONFIG_ENCODERS
-
-static void common_init(MpegEncContext * s)
-{
-    static int inited=0;
-
-    switch(s->msmpeg4_version){
-    case 1:
-    case 2:
-        s->y_dc_scale_table=
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
-        break;
-    case 3:
-        if(s->workaround_bugs){
-            s->y_dc_scale_table= old_ff_y_dc_scale_table;
-            s->c_dc_scale_table= old_ff_c_dc_scale_table;
-        } else{
-            s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
-            s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
-        }
-        break;
-    case 4:
-    case 5:
-        s->y_dc_scale_table= wmv1_y_dc_scale_table;
-        s->c_dc_scale_table= wmv1_c_dc_scale_table;
-        break;
-#if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC1_DECODER)
-    case 6:
-        s->y_dc_scale_table= wmv3_dc_scale_table;
-        s->c_dc_scale_table= wmv3_dc_scale_table;
-        break;
-#endif
-
-    }
-
-
-    if(s->msmpeg4_version>=4){
-        ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable  , wmv1_scantable[1]);
-        ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]);
-        ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]);
-        ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable  , wmv1_scantable[0]);
-    }
-    //Note the default tables are set in common_init in mpegvideo.c
-
-    if(!inited){
-        inited=1;
-
-        init_h263_dc_for_msmpeg4();
-    }
-}
-
-#ifdef CONFIG_ENCODERS
-
-/* build the table which associate a (x,y) motion vector to a vlc */
-static void init_mv_table(MVTable *tab)
-{
-    int i, x, y;
-
-    tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
-    /* mark all entries as not used */
-    for(i=0;i<4096;i++)
-        tab->table_mv_index[i] = tab->n;
-
-    for(i=0;i<tab->n;i++) {
-        x = tab->table_mvx[i];
-        y = tab->table_mvy[i];
-        tab->table_mv_index[(x << 6) | y] = i;
-    }
-}
-
-static void code012(PutBitContext *pb, int n)
-{
-    if (n == 0) {
-        put_bits(pb, 1, 0);
-    } else {
-        put_bits(pb, 1, 1);
-        put_bits(pb, 1, (n >= 2));
-    }
-}
-
-void ff_msmpeg4_encode_init(MpegEncContext *s)
-{
-    static int init_done=0;
-    int i;
-
-    common_init(s);
-    if(s->msmpeg4_version>=4){
-        s->min_qcoeff= -255;
-        s->max_qcoeff=  255;
-    }
-
-    if (!init_done) {
-        /* init various encoding tables */
-        init_done = 1;
-        init_mv_table(&mv_tables[0]);
-        init_mv_table(&mv_tables[1]);
-        for(i=0;i<NB_RL_TABLES;i++)
-            init_rl(&rl_table[i], 1);
-
-        for(i=0; i<NB_RL_TABLES; i++){
-            int level;
-            for(level=0; level<=MAX_LEVEL; level++){
-                int run;
-                for(run=0; run<=MAX_RUN; run++){
-                    int last;
-                    for(last=0; last<2; last++){
-                        rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[  i], last, run, level, 0);
-                    }
-                }
-            }
-        }
-    }
-}
-
-static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
-    int size=0;
-    int code;
-    int run_diff= intra ? 0 : 1;
-
-    code = get_rl_index(rl, last, run, level);
-    size+= rl->table_vlc[code][1];
-    if (code == rl->n) {
-        int level1, run1;
-
-        level1 = level - rl->max_level[last][run];
-        if (level1 < 1)
-            goto esc2;
-        code = get_rl_index(rl, last, run, level1);
-        if (code == rl->n) {
-            esc2:
-            size++;
-            if (level > MAX_LEVEL)
-                goto esc3;
-            run1 = run - rl->max_run[last][level] - run_diff;
-            if (run1 < 0)
-                goto esc3;
-            code = get_rl_index(rl, last, run1, level);
-            if (code == rl->n) {
-            esc3:
-                /* third escape */
-                size+=1+1+6+8;
-            } else {
-                /* second escape */
-                size+= 1+1+ rl->table_vlc[code][1];
-            }
-        } else {
-            /* first escape */
-            size+= 1+1+ rl->table_vlc[code][1];
-        }
-    } else {
-        size++;
-    }
-    return size;
-}
-
-static void find_best_tables(MpegEncContext * s)
-{
-    int i;
-    int best       =-1, best_size       =9999999;
-    int chroma_best=-1, best_chroma_size=9999999;
-
-    for(i=0; i<3; i++){
-        int level;
-        int chroma_size=0;
-        int size=0;
-
-        if(i>0){// ;)
-            size++;
-            chroma_size++;
-        }
-        for(level=0; level<=MAX_LEVEL; level++){
-            int run;
-            for(run=0; run<=MAX_RUN; run++){
-                int last;
-                const int last_size= size + chroma_size;
-                for(last=0; last<2; last++){
-                    int inter_count       = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
-                    int intra_luma_count  = s->ac_stats[1][0][level][run][last];
-                    int intra_chroma_count= s->ac_stats[1][1][level][run][last];
-
-                    if(s->pict_type==I_TYPE){
-                        size       += intra_luma_count  *rl_length[i  ][level][run][last];
-                        chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
-                    }else{
-                        size+=        intra_luma_count  *rl_length[i  ][level][run][last]
-                                     +intra_chroma_count*rl_length[i+3][level][run][last]
-                                     +inter_count       *rl_length[i+3][level][run][last];
-                    }
-                }
-                if(last_size == size+chroma_size) break;
-            }
-        }
-        if(size<best_size){
-            best_size= size;
-            best= i;
-        }
-        if(chroma_size<best_chroma_size){
-            best_chroma_size= chroma_size;
-            chroma_best= i;
-        }
-    }
-
-//    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
-//           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
-
-    if(s->pict_type==P_TYPE) chroma_best= best;
-
-    memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
-
-    s->rl_table_index       =        best;
-    s->rl_chroma_table_index= chroma_best;
-
-    if(s->pict_type != s->last_non_b_pict_type){
-        s->rl_table_index= 2;
-        if(s->pict_type==I_TYPE)
-            s->rl_chroma_table_index= 1;
-        else
-            s->rl_chroma_table_index= 2;
-    }
-
-}
-
-/* write MSMPEG4 compatible frame header */
-void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
-{
-    find_best_tables(s);
-
-    align_put_bits(&s->pb);
-    put_bits(&s->pb, 2, s->pict_type - 1);
-
-    put_bits(&s->pb, 5, s->qscale);
-    if(s->msmpeg4_version<=2){
-        s->rl_table_index = 2;
-        s->rl_chroma_table_index = 2;
-    }
-
-    s->dc_table_index = 1;
-    s->mv_table_index = 1; /* only if P frame */
-    s->use_skip_mb_code = 1; /* only if P frame */
-    s->per_mb_rl_table = 0;
-    if(s->msmpeg4_version==4)
-        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
-//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
-
-    if (s->pict_type == I_TYPE) {
-        s->slice_height= s->mb_height/1;
-        put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
-
-        if(s->msmpeg4_version==4){
-            msmpeg4_encode_ext_header(s);
-            if(s->bit_rate>MBAC_BITRATE)
-                put_bits(&s->pb, 1, s->per_mb_rl_table);
-        }
-
-        if(s->msmpeg4_version>2){
-            if(!s->per_mb_rl_table){
-                code012(&s->pb, s->rl_chroma_table_index);
-                code012(&s->pb, s->rl_table_index);
-            }
-
-            put_bits(&s->pb, 1, s->dc_table_index);
-        }
-    } else {
-        put_bits(&s->pb, 1, s->use_skip_mb_code);
-
-        if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
-            put_bits(&s->pb, 1, s->per_mb_rl_table);
-
-        if(s->msmpeg4_version>2){
-            if(!s->per_mb_rl_table)
-                code012(&s->pb, s->rl_table_index);
-
-            put_bits(&s->pb, 1, s->dc_table_index);
-
-            put_bits(&s->pb, 1, s->mv_table_index);
-        }
-    }
-
-    s->esc3_level_length= 0;
-    s->esc3_run_length= 0;
-
-#ifdef DEBUG
-    intra_count = 0;
-    av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
-#endif
-}
-
-void msmpeg4_encode_ext_header(MpegEncContext * s)
-{
-        put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29
-
-        put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
-
-        if(s->msmpeg4_version>=3)
-            put_bits(&s->pb, 1, s->flipflop_rounding);
-        else
-            assert(s->flipflop_rounding==0);
-}
-
-#endif //CONFIG_ENCODERS
-
-/* predict coded block */
-static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
-{
-    int xy, wrap, pred, a, b, c;
-
-    xy = s->block_index[n];
-    wrap = s->b8_stride;
-
-    /* B C
-     * A X
-     */
-    a = s->coded_block[xy - 1       ];
-    b = s->coded_block[xy - 1 - wrap];
-    c = s->coded_block[xy     - wrap];
-
-    if (b == c) {
-        pred = a;
-    } else {
-        pred = c;
-    }
-
-    /* store value */
-    *coded_block_ptr = &s->coded_block[xy];
-
-    return pred;
-}
-
-#ifdef CONFIG_ENCODERS
-
-static void msmpeg4_encode_motion(MpegEncContext * s,
-                                  int mx, int my)
-{
-    int code;
-    MVTable *mv;
-
-    /* modulo encoding */
-    /* WARNING : you cannot reach all the MVs even with the modulo
-       encoding. This is a somewhat strange compromise they took !!!  */
-    if (mx <= -64)
-        mx += 64;
-    else if (mx >= 64)
-        mx -= 64;
-    if (my <= -64)
-        my += 64;
-    else if (my >= 64)
-        my -= 64;
-
-    mx += 32;
-    my += 32;
-#if 0
-    if ((unsigned)mx >= 64 ||
-        (unsigned)my >= 64)
-        av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
-#endif
-    mv = &mv_tables[s->mv_table_index];
-
-    code = mv->table_mv_index[(mx << 6) | my];
-    put_bits(&s->pb,
-             mv->table_mv_bits[code],
-             mv->table_mv_code[code]);
-    if (code == mv->n) {
-        /* escape : code litterally */
-        put_bits(&s->pb, 6, mx);
-        put_bits(&s->pb, 6, my);
-    }
-}
-
-static inline void handle_slices(MpegEncContext *s){
-    if (s->mb_x == 0) {
-        if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
-            if(s->msmpeg4_version < 4){
-                ff_mpeg4_clean_buffers(s);
-            }
-            s->first_slice_line = 1;
-        } else {
-            s->first_slice_line = 0;
-        }
-    }
-}
-
-void msmpeg4_encode_mb(MpegEncContext * s,
-                       DCTELEM block[6][64],
-                       int motion_x, int motion_y)
-{
-    int cbp, coded_cbp, i;
-    int pred_x, pred_y;
-    uint8_t *coded_block;
-
-    handle_slices(s);
-
-    if (!s->mb_intra) {
-        /* compute cbp */
-        cbp = 0;
-        for (i = 0; i < 6; i++) {
-            if (s->block_last_index[i] >= 0)
-                cbp |= 1 << (5 - i);
-        }
-        if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
-            /* skip macroblock */
-            put_bits(&s->pb, 1, 1);
-            s->last_bits++;
-            s->misc_bits++;
-            s->skip_count++;
-
-            return;
-        }
-        if (s->use_skip_mb_code)
-            put_bits(&s->pb, 1, 0);     /* mb coded */
-
-        if(s->msmpeg4_version<=2){
-            put_bits(&s->pb,
-                     v2_mb_type[cbp&3][1],
-                     v2_mb_type[cbp&3][0]);
-            if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
-            else             coded_cbp= cbp;
-
-            put_bits(&s->pb,
-                     cbpy_tab[coded_cbp>>2][1],
-                     cbpy_tab[coded_cbp>>2][0]);
-
-            s->misc_bits += get_bits_diff(s);
-
-            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
-            msmpeg4v2_encode_motion(s, motion_x - pred_x);
-            msmpeg4v2_encode_motion(s, motion_y - pred_y);
-        }else{
-            put_bits(&s->pb,
-                     table_mb_non_intra[cbp + 64][1],
-                     table_mb_non_intra[cbp + 64][0]);
-
-            s->misc_bits += get_bits_diff(s);
-
-            /* motion vector */
-            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
-            msmpeg4_encode_motion(s, motion_x - pred_x,
-                                  motion_y - pred_y);
-        }
-
-        s->mv_bits += get_bits_diff(s);
-
-        for (i = 0; i < 6; i++) {
-            msmpeg4_encode_block(s, block[i], i);
-        }
-        s->p_tex_bits += get_bits_diff(s);
-    } else {
-        /* compute cbp */
-        cbp = 0;
-        coded_cbp = 0;
-        for (i = 0; i < 6; i++) {
-            int val, pred;
-            val = (s->block_last_index[i] >= 1);
-            cbp |= val << (5 - i);
-            if (i < 4) {
-                /* predict value for close blocks only for luma */
-                pred = coded_block_pred(s, i, &coded_block);
-                *coded_block = val;
-                val = val ^ pred;
-            }
-            coded_cbp |= val << (5 - i);
-        }
-#if 0
-        if (coded_cbp)
-            printf("cbp=%x %x\n", cbp, coded_cbp);
-#endif
-
-        if(s->msmpeg4_version<=2){
-            if (s->pict_type == I_TYPE) {
-                put_bits(&s->pb,
-                         v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
-            } else {
-                if (s->use_skip_mb_code)
-                    put_bits(&s->pb, 1, 0);     /* mb coded */
-                put_bits(&s->pb,
-                         v2_mb_type[(cbp&3) + 4][1],
-                         v2_mb_type[(cbp&3) + 4][0]);
-            }
-            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
-            put_bits(&s->pb,
-                     cbpy_tab[cbp>>2][1],
-                     cbpy_tab[cbp>>2][0]);
-        }else{
-            if (s->pict_type == I_TYPE) {
-                put_bits(&s->pb,
-                         ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
-            } else {
-                if (s->use_skip_mb_code)
-                    put_bits(&s->pb, 1, 0);     /* mb coded */
-                put_bits(&s->pb,
-                         table_mb_non_intra[cbp][1],
-                         table_mb_non_intra[cbp][0]);
-            }
-            put_bits(&s->pb, 1, 0);             /* no AC prediction yet */
-            if(s->inter_intra_pred){
-                s->h263_aic_dir=0;
-                put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
-            }
-        }
-        s->misc_bits += get_bits_diff(s);
-
-        for (i = 0; i < 6; i++) {
-            msmpeg4_encode_block(s, block[i], i);
-        }
-        s->i_tex_bits += get_bits_diff(s);
-        s->i_count++;
-    }
-}
-
-#endif //CONFIG_ENCODERS
-
-static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
-                                    int32_t **dc_val_ptr)
-{
-    int i;
-
-    if (n < 4) {
-        i= 0;
-    } else {
-        i= n-3;
-    }
-
-    *dc_val_ptr= &s->last_dc[i];
-    return s->last_dc[i];
-}
-
-static int get_dc(uint8_t *src, int stride, int scale)
-{
-    int y;
-    int sum=0;
-    for(y=0; y<8; y++){
-        int x;
-        for(x=0; x<8; x++){
-            sum+=src[x + y*stride];
-        }
-    }
-    return FASTDIV((sum + (scale>>1)), scale);
-}
-
-/* dir = 0: left, dir = 1: top prediction */
-static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
-                             int16_t **dc_val_ptr, int *dir_ptr)
-{
-    int a, b, c, wrap, pred, scale;
-    int16_t *dc_val;
-
-    /* find prediction */
-    if (n < 4) {
-        scale = s->y_dc_scale;
-    } else {
-        scale = s->c_dc_scale;
-    }
-
-    wrap = s->block_wrap[n];
-    dc_val= s->dc_val[0] + s->block_index[n];
-
-    /* B C
-     * A X
-     */
-    a = dc_val[ - 1];
-    b = dc_val[ - 1 - wrap];
-    c = dc_val[ - wrap];
-
-    if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
-        b=c=1024;
-    }
-
-    /* XXX: the following solution consumes divisions, but it does not
-       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(ARCH_X86_64)) && !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...
-     */
-    if (scale == 8) {
-        a = (a + (8 >> 1)) / 8;
-        b = (b + (8 >> 1)) / 8;
-        c = (c + (8 >> 1)) / 8;
-    } else {
-        a = FASTDIV((a + (scale >> 1)), scale);
-        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){
-        if(s->inter_intra_pred){
-            uint8_t *dest;
-            int wrap;
-
-            if(n==1){
-                pred=a;
-                *dir_ptr = 0;
-            }else if(n==2){
-                pred=c;
-                *dir_ptr = 1;
-            }else if(n==3){
-                if (abs(a - b) < abs(b - c)) {
-                    pred = c;
-                    *dir_ptr = 1;
-                } else {
-                    pred = a;
-                    *dir_ptr = 0;
-                }
-            }else{
-                if(n<4){
-                    wrap= s->linesize;
-                    dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8*  wrap ) + ((n&1) + 2*s->mb_x) * 8;
-                }else{
-                    wrap= s->uvlinesize;
-                    dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
-                }
-                if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
-                else           a= get_dc(dest-8, wrap, scale*8);
-                if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
-                else           c= get_dc(dest-8*wrap, wrap, scale*8);
-
-                if (s->h263_aic_dir==0) {
-                    pred= a;
-                    *dir_ptr = 0;
-                }else if (s->h263_aic_dir==1) {
-                    if(n==0){
-                        pred= c;
-                        *dir_ptr = 1;
-                    }else{
-                        pred= a;
-                        *dir_ptr = 0;
-                    }
-                }else if (s->h263_aic_dir==2) {
-                    if(n==0){
-                        pred= a;
-                        *dir_ptr = 0;
-                    }else{
-                        pred= c;
-                        *dir_ptr = 1;
-                    }
-                } else {
-                    pred= c;
-                    *dir_ptr = 1;
-                }
-            }
-        }else{
-            if (abs(a - b) < abs(b - c)) {
-                pred = c;
-                *dir_ptr = 1;
-            } else {
-                pred = a;
-                *dir_ptr = 0;
-            }
-        }
-    }else{
-        if (abs(a - b) <= abs(b - c)) {
-            pred = c;
-            *dir_ptr = 1;
-        } else {
-            pred = a;
-            *dir_ptr = 0;
-        }
-    }
-
-    /* update predictor */
-    *dc_val_ptr = &dc_val[0];
-    return pred;
-}
-
-#define DC_MAX 119
-
-static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
-{
-    int sign, code;
-    int pred;
-
-    if(s->msmpeg4_version==1){
-        int32_t *dc_val;
-        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
-
-        /* update predictor */
-        *dc_val= level;
-    }else{
-        int16_t *dc_val;
-        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
-
-        /* update predictor */
-        if (n < 4) {
-            *dc_val = level * s->y_dc_scale;
-        } else {
-            *dc_val = level * s->c_dc_scale;
-        }
-    }
-
-    /* do the prediction */
-    level -= pred;
-
-    if(s->msmpeg4_version<=2){
-        if (n < 4) {
-            put_bits(&s->pb,
-                     v2_dc_lum_table[level+256][1],
-                     v2_dc_lum_table[level+256][0]);
-        }else{
-            put_bits(&s->pb,
-                     v2_dc_chroma_table[level+256][1],
-                     v2_dc_chroma_table[level+256][0]);
-        }
-    }else{
-        sign = 0;
-        if (level < 0) {
-            level = -level;
-            sign = 1;
-        }
-        code = level;
-        if (code > DC_MAX)
-            code = DC_MAX;
-
-        if (s->dc_table_index == 0) {
-            if (n < 4) {
-                put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
-            } else {
-                put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
-            }
-        } else {
-            if (n < 4) {
-                put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
-            } else {
-                put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
-            }
-        }
-
-        if (code == DC_MAX)
-            put_bits(&s->pb, 8, level);
-
-        if (level != 0) {
-            put_bits(&s->pb, 1, sign);
-        }
-    }
-}
-
-/* Encoding of a block. Very similar to MPEG4 except for a different
-   escape coding (same as H263) and more vlc tables.
- */
-static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
-{
-    int level, run, last, i, j, last_index;
-    int last_non_zero, sign, slevel;
-    int code, run_diff, dc_pred_dir;
-    const RLTable *rl;
-    const uint8_t *scantable;
-
-    if (s->mb_intra) {
-        msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
-        i = 1;
-        if (n < 4) {
-            rl = &rl_table[s->rl_table_index];
-        } else {
-            rl = &rl_table[3 + s->rl_chroma_table_index];
-        }
-        run_diff = 0;
-        scantable= s->intra_scantable.permutated;
-    } else {
-        i = 0;
-        rl = &rl_table[3 + s->rl_table_index];
-        if(s->msmpeg4_version<=2)
-            run_diff = 0;
-        else
-            run_diff = 1;
-        scantable= s->inter_scantable.permutated;
-    }
-
-    /* recalculate block_last_index for M$ wmv1 */
-    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
-        for(last_index=63; last_index>=0; last_index--){
-            if(block[scantable[last_index]]) break;
-        }
-        s->block_last_index[n]= last_index;
-    }else
-        last_index = s->block_last_index[n];
-    /* AC coefs */
-    last_non_zero = i - 1;
-    for (; i <= last_index; i++) {
-        j = scantable[i];
-        level = block[j];
-        if (level) {
-            run = i - last_non_zero - 1;
-            last = (i == last_index);
-            sign = 0;
-            slevel = level;
-            if (level < 0) {
-                sign = 1;
-                level = -level;
-            }
-
-            if(level<=MAX_LEVEL && run<=MAX_RUN){
-                s->ac_stats[s->mb_intra][n>3][level][run][last]++;
-            }
-#if 0
-else
-    s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
-#endif
-            code = get_rl_index(rl, last, run, level);
-            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
-            if (code == rl->n) {
-                int level1, run1;
-
-                level1 = level - rl->max_level[last][run];
-                if (level1 < 1)
-                    goto esc2;
-                code = get_rl_index(rl, last, run, level1);
-                if (code == rl->n) {
-                esc2:
-                    put_bits(&s->pb, 1, 0);
-                    if (level > MAX_LEVEL)
-                        goto esc3;
-                    run1 = run - rl->max_run[last][level] - run_diff;
-                    if (run1 < 0)
-                        goto esc3;
-                    code = get_rl_index(rl, last, run1, level);
-                    if (code == rl->n) {
-                    esc3:
-                        /* third escape */
-                        put_bits(&s->pb, 1, 0);
-                        put_bits(&s->pb, 1, last);
-                        if(s->msmpeg4_version>=4){
-                            if(s->esc3_level_length==0){
-                                s->esc3_level_length=8;
-                                s->esc3_run_length= 6;
-                                if(s->qscale<8)
-                                    put_bits(&s->pb, 6, 3);
-                                else
-                                    put_bits(&s->pb, 8, 3);
-                            }
-                            put_bits(&s->pb, s->esc3_run_length, run);
-                            put_bits(&s->pb, 1, sign);
-                            put_bits(&s->pb, s->esc3_level_length, level);
-                        }else{
-                            put_bits(&s->pb, 6, run);
-                            put_bits(&s->pb, 8, slevel & 0xff);
-                        }
-                    } else {
-                        /* second escape */
-                        put_bits(&s->pb, 1, 1);
-                        put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
-                        put_bits(&s->pb, 1, sign);
-                    }
-                } else {
-                    /* first escape */
-                    put_bits(&s->pb, 1, 1);
-                    put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
-                    put_bits(&s->pb, 1, sign);
-                }
-            } else {
-                put_bits(&s->pb, 1, sign);
-            }
-            last_non_zero = i;
-        }
-    }
-}
-
-/****************************************/
-/* decoding stuff */
-
-static VLC mb_non_intra_vlc[4];
-VLC ff_msmp4_mb_i_vlc;
-VLC ff_msmp4_dc_luma_vlc[2];
-VLC ff_msmp4_dc_chroma_vlc[2];
-static VLC v2_dc_lum_vlc;
-static VLC v2_dc_chroma_vlc;
-static VLC cbpy_vlc;
-static VLC v2_intra_cbpc_vlc;
-static VLC v2_mb_type_vlc;
-static VLC v2_mv_vlc;
-static VLC v1_intra_cbpc_vlc;
-static VLC v1_inter_cbpc_vlc;
-static VLC inter_intra_vlc;
-
-/* this table is practically identical to the one from h263 except that its inverted */
-static void init_h263_dc_for_msmpeg4(void)
-{
-        int level, uni_code, uni_len;
-
-        for(level=-256; level<256; level++){
-            int size, v, l;
-            /* find number of bits */
-            size = 0;
-            v = abs(level);
-            while (v) {
-                v >>= 1;
-                    size++;
-            }
-
-            if (level < 0)
-                l= (-level) ^ ((1 << size) - 1);
-            else
-                l= level;
-
-            /* luminance h263 */
-            uni_code= DCtab_lum[size][0];
-            uni_len = DCtab_lum[size][1];
-            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
-
-            if (size > 0) {
-                uni_code<<=size; uni_code|=l;
-                uni_len+=size;
-                if (size > 8){
-                    uni_code<<=1; uni_code|=1;
-                    uni_len++;
-                }
-            }
-            v2_dc_lum_table[level+256][0]= uni_code;
-            v2_dc_lum_table[level+256][1]= uni_len;
-
-            /* chrominance h263 */
-            uni_code= DCtab_chrom[size][0];
-            uni_len = DCtab_chrom[size][1];
-            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
-
-            if (size > 0) {
-                uni_code<<=size; uni_code|=l;
-                uni_len+=size;
-                if (size > 8){
-                    uni_code<<=1; uni_code|=1;
-                    uni_len++;
-                }
-            }
-            v2_dc_chroma_table[level+256][0]= uni_code;
-            v2_dc_chroma_table[level+256][1]= uni_len;
-
-        }
-}
-
-/* init all vlc decoding tables */
-int ff_msmpeg4_decode_init(MpegEncContext *s)
-{
-    static int done = 0;
-    int i;
-    MVTable *mv;
-
-    common_init(s);
-
-    if (!done) {
-        done = 1;
-
-        for(i=0;i<NB_RL_TABLES;i++) {
-            init_rl(&rl_table[i], 1);
-            init_vlc_rl(&rl_table[i], 1);
-        }
-        for(i=0;i<2;i++) {
-            mv = &mv_tables[i];
-            init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
-                     mv->table_mv_bits, 1, 1,
-                     mv->table_mv_code, 2, 2, 1);
-        }
-
-        init_vlc(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
-                 &ff_table0_dc_lum[0][1], 8, 4,
-                 &ff_table0_dc_lum[0][0], 8, 4, 1);
-        init_vlc(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
-                 &ff_table0_dc_chroma[0][1], 8, 4,
-                 &ff_table0_dc_chroma[0][0], 8, 4, 1);
-        init_vlc(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
-                 &ff_table1_dc_lum[0][1], 8, 4,
-                 &ff_table1_dc_lum[0][0], 8, 4, 1);
-        init_vlc(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
-                 &ff_table1_dc_chroma[0][1], 8, 4,
-                 &ff_table1_dc_chroma[0][0], 8, 4, 1);
-
-        init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
-                 &v2_dc_lum_table[0][1], 8, 4,
-                 &v2_dc_lum_table[0][0], 8, 4, 1);
-        init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
-                 &v2_dc_chroma_table[0][1], 8, 4,
-                 &v2_dc_chroma_table[0][0], 8, 4, 1);
-
-        init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
-                 &cbpy_tab[0][1], 2, 1,
-                 &cbpy_tab[0][0], 2, 1, 1);
-        init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
-                 &v2_intra_cbpc[0][1], 2, 1,
-                 &v2_intra_cbpc[0][0], 2, 1, 1);
-        init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
-                 &v2_mb_type[0][1], 2, 1,
-                 &v2_mb_type[0][0], 2, 1, 1);
-        init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
-                 &mvtab[0][1], 2, 1,
-                 &mvtab[0][0], 2, 1, 1);
-
-        for(i=0; i<4; i++){
-            init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
-                     &wmv2_inter_table[i][0][1], 8, 4,
-                     &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
-        }
-
-        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
-                 &ff_msmp4_mb_i_table[0][1], 4, 2,
-                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
-
-        init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
-                 intra_MCBPC_bits, 1, 1,
-                 intra_MCBPC_code, 1, 1, 1);
-        init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
-                 inter_MCBPC_bits, 1, 1,
-                 inter_MCBPC_code, 1, 1, 1);
-
-        init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
-                 &table_inter_intra[0][1], 2, 1,
-                 &table_inter_intra[0][0], 2, 1, 1);
-    }
-
-    switch(s->msmpeg4_version){
-    case 1:
-    case 2:
-        s->decode_mb= msmpeg4v12_decode_mb;
-        break;
-    case 3:
-    case 4:
-        s->decode_mb= msmpeg4v34_decode_mb;
-        break;
-    case 5:
-        s->decode_mb= wmv2_decode_mb;
-    case 6:
-        //FIXME + TODO VC1 decode mb
-        break;
-    }
-
-    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
-
-    return 0;
-}
-
-int msmpeg4_decode_picture_header(MpegEncContext * s)
-{
-    int code;
-
-#if 0
-{
-int i;
-for(i=0; i<s->gb.size_in_bits; i++)
-    av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
-//    get_bits1(&s->gb);
-av_log(s->avctx, AV_LOG_DEBUG, "END\n");
-return -1;
-}
-#endif
-
-    if(s->msmpeg4_version==1){
-        int start_code, num;
-        start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
-        if(start_code!=0x00000100){
-            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
-            return -1;
-        }
-
-        num= get_bits(&s->gb, 5); // frame number */
-    }
-
-    s->pict_type = get_bits(&s->gb, 2) + 1;
-    if (s->pict_type != I_TYPE &&
-        s->pict_type != P_TYPE){
-        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
-        return -1;
-    }
-#if 0
-{
-    static int had_i=0;
-    if(s->pict_type == I_TYPE) had_i=1;
-    if(!had_i) return -1;
-}
-#endif
-    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
-    if(s->qscale==0){
-        av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
-        return -1;
-    }
-
-    if (s->pict_type == I_TYPE) {
-        code = get_bits(&s->gb, 5);
-        if(s->msmpeg4_version==1){
-            if(code==0 || code>s->mb_height){
-                av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
-                return -1;
-            }
-
-            s->slice_height = code;
-        }else{
-            /* 0x17: one slice, 0x18: two slices, ... */
-            if (code < 0x17){
-                av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
-                return -1;
-            }
-
-            s->slice_height = s->mb_height / (code - 0x16);
-        }
-
-        switch(s->msmpeg4_version){
-        case 1:
-        case 2:
-            s->rl_chroma_table_index = 2;
-            s->rl_table_index = 2;
-
-            s->dc_table_index = 0; //not used
-            break;
-        case 3:
-            s->rl_chroma_table_index = decode012(&s->gb);
-            s->rl_table_index = decode012(&s->gb);
-
-            s->dc_table_index = get_bits1(&s->gb);
-            break;
-        case 4:
-            msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
-
-            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
-            else                           s->per_mb_rl_table= 0;
-
-            if(!s->per_mb_rl_table){
-                s->rl_chroma_table_index = decode012(&s->gb);
-                s->rl_table_index = decode012(&s->gb);
-            }
-
-            s->dc_table_index = get_bits1(&s->gb);
-            s->inter_intra_pred= 0;
-            break;
-        }
-        s->no_rounding = 1;
-        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
-            av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d   \n",
-                s->qscale,
-                s->rl_chroma_table_index,
-                s->rl_table_index,
-                s->dc_table_index,
-                s->per_mb_rl_table,
-                s->slice_height);
-    } else {
-        switch(s->msmpeg4_version){
-        case 1:
-        case 2:
-            if(s->msmpeg4_version==1)
-                s->use_skip_mb_code = 1;
-            else
-                s->use_skip_mb_code = get_bits1(&s->gb);
-            s->rl_table_index = 2;
-            s->rl_chroma_table_index = s->rl_table_index;
-            s->dc_table_index = 0; //not used
-            s->mv_table_index = 0;
-            break;
-        case 3:
-            s->use_skip_mb_code = get_bits1(&s->gb);
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-
-            s->dc_table_index = get_bits1(&s->gb);
-
-            s->mv_table_index = get_bits1(&s->gb);
-            break;
-        case 4:
-            s->use_skip_mb_code = get_bits1(&s->gb);
-
-            if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
-            else                           s->per_mb_rl_table= 0;
-
-            if(!s->per_mb_rl_table){
-                s->rl_table_index = decode012(&s->gb);
-                s->rl_chroma_table_index = s->rl_table_index;
-            }
-
-            s->dc_table_index = get_bits1(&s->gb);
-
-            s->mv_table_index = get_bits1(&s->gb);
-            s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
-            break;
-        }
-
-        if(s->avctx->debug&FF_DEBUG_PICT_INFO)
-            av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d   \n",
-                s->use_skip_mb_code,
-                s->rl_table_index,
-                s->rl_chroma_table_index,
-                s->dc_table_index,
-                s->mv_table_index,
-                s->per_mb_rl_table,
-                s->qscale);
-
-        if(s->flipflop_rounding){
-            s->no_rounding ^= 1;
-        }else{
-            s->no_rounding = 0;
-        }
-    }
-//printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
-
-    s->esc3_level_length= 0;
-    s->esc3_run_length= 0;
-
-#ifdef DEBUG
-    av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
-#endif
-    return 0;
-}
-
-int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
-{
-    int left= buf_size*8 - get_bits_count(&s->gb);
-    int length= s->msmpeg4_version>=3 ? 17 : 16;
-    /* the alt_bitstream reader could read over the end so we need to check it */
-    if(left>=length && left<length+8)
-    {
-        int fps;
-
-        fps= get_bits(&s->gb, 5);
-        s->bit_rate= get_bits(&s->gb, 11)*1024;
-        if(s->msmpeg4_version>=3)
-            s->flipflop_rounding= get_bits1(&s->gb);
-        else
-            s->flipflop_rounding= 0;
-
-//        printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
-    }
-    else if(left<length+8)
-    {
-        s->flipflop_rounding= 0;
-        if(s->msmpeg4_version != 2)
-            av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
-    }
-    else
-    {
-        av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
-    }
-
-    return 0;
-}
-
-static inline void msmpeg4_memsetw(short *tab, int val, int n)
-{
-    int i;
-    for(i=0;i<n;i++)
-        tab[i] = val;
-}
-
-#ifdef CONFIG_ENCODERS
-static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
-{
-    int range, bit_size, sign, code, bits;
-
-    if (val == 0) {
-        /* zero vector */
-        code = 0;
-        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
-    } else {
-        bit_size = s->f_code - 1;
-        range = 1 << bit_size;
-        if (val <= -64)
-            val += 64;
-        else if (val >= 64)
-            val -= 64;
-
-        if (val >= 0) {
-            sign = 0;
-        } else {
-            val = -val;
-            sign = 1;
-        }
-        val--;
-        code = (val >> bit_size) + 1;
-        bits = val & (range - 1);
-
-        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
-        if (bit_size > 0) {
-            put_bits(&s->pb, bit_size, bits);
-        }
-    }
-}
-#endif
-
-/* this is identical to h263 except that its range is multiplied by 2 */
-static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
-{
-    int code, val, sign, shift;
-
-    code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
-//     printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
-    if (code < 0)
-        return 0xffff;
-
-    if (code == 0)
-        return pred;
-    sign = get_bits1(&s->gb);
-    shift = f_code - 1;
-    val = code;
-    if (shift) {
-        val = (val - 1) << shift;
-        val |= get_bits(&s->gb, shift);
-        val++;
-    }
-    if (sign)
-        val = -val;
-
-    val += pred;
-    if (val <= -64)
-        val += 64;
-    else if (val >= 64)
-        val -= 64;
-
-    return val;
-}
-
-static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
-{
-    int cbp, code, i;
-
-    if (s->pict_type == P_TYPE) {
-        if (s->use_skip_mb_code) {
-            if (get_bits1(&s->gb)) {
-                /* skip mb */
-                s->mb_intra = 0;
-                for(i=0;i<6;i++)
-                    s->block_last_index[i] = -1;
-                s->mv_dir = MV_DIR_FORWARD;
-                s->mv_type = MV_TYPE_16X16;
-                s->mv[0][0][0] = 0;
-                s->mv[0][0][1] = 0;
-                s->mb_skipped = 1;
-                return 0;
-            }
-        }
-
-        if(s->msmpeg4_version==2)
-            code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
-        else
-            code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
-        if(code<0 || code>7){
-            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
-            return -1;
-        }
-
-        s->mb_intra = code >>2;
-
-        cbp = code & 0x3;
-    } else {
-        s->mb_intra = 1;
-        if(s->msmpeg4_version==2)
-            cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
-        else
-            cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
-        if(cbp<0 || cbp>3){
-            av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
-            return -1;
-        }
-    }
-
-    if (!s->mb_intra) {
-        int mx, my, cbpy;
-
-        cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
-        if(cbpy<0){
-            av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
-            return -1;
-        }
-
-        cbp|= cbpy<<2;
-        if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
-
-        h263_pred_motion(s, 0, 0, &mx, &my);
-        mx= msmpeg4v2_decode_motion(s, mx, 1);
-        my= msmpeg4v2_decode_motion(s, my, 1);
-
-        s->mv_dir = MV_DIR_FORWARD;
-        s->mv_type = MV_TYPE_16X16;
-        s->mv[0][0][0] = mx;
-        s->mv[0][0][1] = my;
-    } else {
-        if(s->msmpeg4_version==2){
-            s->ac_pred = get_bits1(&s->gb);
-            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
-        } else{
-            s->ac_pred = 0;
-            cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
-            if(s->pict_type==P_TYPE) cbp^=0x3C;
-        }
-    }
-
-    s->dsp.clear_blocks(s->block[0]);
-    for (i = 0; i < 6; i++) {
-        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
-        {
-             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
-             return -1;
-        }
-    }
-    return 0;
-}
-
-static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
-{
-    int cbp, code, i;
-    uint8_t *coded_val;
-    uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
-
-    if (s->pict_type == P_TYPE) {
-        if (s->use_skip_mb_code) {
-            if (get_bits1(&s->gb)) {
-                /* skip mb */
-                s->mb_intra = 0;
-                for(i=0;i<6;i++)
-                    s->block_last_index[i] = -1;
-                s->mv_dir = MV_DIR_FORWARD;
-                s->mv_type = MV_TYPE_16X16;
-                s->mv[0][0][0] = 0;
-                s->mv[0][0][1] = 0;
-                s->mb_skipped = 1;
-                *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
-
-                return 0;
-            }
-        }
-
-        code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
-        if (code < 0)
-            return -1;
-        //s->mb_intra = (code & 0x40) ? 0 : 1;
-        s->mb_intra = (~code & 0x40) >> 6;
-
-        cbp = code & 0x3f;
-    } else {
-        s->mb_intra = 1;
-        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
-        if (code < 0)
-            return -1;
-        /* predict coded block pattern */
-        cbp = 0;
-        for(i=0;i<6;i++) {
-            int val = ((code >> (5 - i)) & 1);
-            if (i < 4) {
-                int pred = coded_block_pred(s, i, &coded_val);
-                val = val ^ pred;
-                *coded_val = val;
-            }
-            cbp |= val << (5 - i);
-        }
-    }
-
-    if (!s->mb_intra) {
-        int mx, my;
-//printf("P at %d %d\n", s->mb_x, s->mb_y);
-        if(s->per_mb_rl_table && cbp){
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-        h263_pred_motion(s, 0, 0, &mx, &my);
-        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
-            return -1;
-        s->mv_dir = MV_DIR_FORWARD;
-        s->mv_type = MV_TYPE_16X16;
-        s->mv[0][0][0] = mx;
-        s->mv[0][0][1] = my;
-        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
-    } else {
-//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
-        s->ac_pred = get_bits1(&s->gb);
-        *mb_type_ptr = MB_TYPE_INTRA;
-        if(s->inter_intra_pred){
-            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
-//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
-        }
-        if(s->per_mb_rl_table && cbp){
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-    }
-
-    s->dsp.clear_blocks(s->block[0]);
-    for (i = 0; i < 6; i++) {
-        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
-        {
-            av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
-            return -1;
-        }
-    }
-
-    return 0;
-}
-//#define ERROR_DETAILS
-static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
-                              int n, int coded, const uint8_t *scan_table)
-{
-    int level, i, last, run, run_diff;
-    int dc_pred_dir;
-    RLTable *rl;
-    RL_VLC_ELEM *rl_vlc;
-    int qmul, qadd;
-
-    if (s->mb_intra) {
-        qmul=1;
-        qadd=0;
-
-        /* DC coef */
-        level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
-
-        if (level < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
-            if(s->inter_intra_pred) level=0;
-            else                    return -1;
-        }
-        if (n < 4) {
-            rl = &rl_table[s->rl_table_index];
-            if(level > 256*s->y_dc_scale){
-                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
-                if(!s->inter_intra_pred) return -1;
-            }
-        } else {
-            rl = &rl_table[3 + s->rl_chroma_table_index];
-            if(level > 256*s->c_dc_scale){
-                av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
-                if(!s->inter_intra_pred) return -1;
-            }
-        }
-        block[0] = level;
-
-        run_diff = 0;
-        i = 0;
-        if (!coded) {
-            goto not_coded;
-        }
-        if (s->ac_pred) {
-            if (dc_pred_dir == 0)
-                scan_table = s->intra_v_scantable.permutated; /* left */
-            else
-                scan_table = s->intra_h_scantable.permutated; /* top */
-        } else {
-            scan_table = s->intra_scantable.permutated;
-        }
-        rl_vlc= rl->rl_vlc[0];
-    } else {
-        qmul = s->qscale << 1;
-        qadd = (s->qscale - 1) | 1;
-        i = -1;
-        rl = &rl_table[3 + s->rl_table_index];
-
-        if(s->msmpeg4_version==2)
-            run_diff = 0;
-        else
-            run_diff = 1;
-
-        if (!coded) {
-            s->block_last_index[n] = i;
-            return 0;
-        }
-        if(!scan_table)
-            scan_table = s->inter_scantable.permutated;
-        rl_vlc= rl->rl_vlc[s->qscale];
-    }
-  {
-    OPEN_READER(re, &s->gb);
-    for(;;) {
-        UPDATE_CACHE(re, &s->gb);
-        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
-        if (level==0) {
-            int cache;
-            cache= GET_CACHE(re, &s->gb);
-            /* escape */
-            if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
-                if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
-                    /* third escape */
-                    if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
-                    UPDATE_CACHE(re, &s->gb);
-                    if(s->msmpeg4_version<=3){
-                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
-                        run=   SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
-                        level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
-                        SKIP_COUNTER(re, &s->gb, 1+6+8);
-                    }else{
-                        int sign;
-                        last=  SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
-                        if(!s->esc3_level_length){
-                            int ll;
-                            //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
-                            if(s->qscale<8){
-                                ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
-                                if(ll==0){
-                                    if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
-                                    SKIP_BITS(re, &s->gb, 1);
-                                    ll=8;
-                                }
-                            }else{
-                                ll=2;
-                                while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
-                                    ll++;
-                                    SKIP_BITS(re, &s->gb, 1);
-                                }
-                                if(ll<8) SKIP_BITS(re, &s->gb, 1);
-                            }
-
-                            s->esc3_level_length= ll;
-                            s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
-//printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
-                            UPDATE_CACHE(re, &s->gb);
-                        }
-                        run=   SHOW_UBITS(re, &s->gb, s->esc3_run_length);
-                        SKIP_BITS(re, &s->gb, s->esc3_run_length);
-
-                        sign=  SHOW_UBITS(re, &s->gb, 1);
-                        SKIP_BITS(re, &s->gb, 1);
-
-                        level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
-                        SKIP_BITS(re, &s->gb, s->esc3_level_length);
-                        if(sign) level= -level;
-                    }
-//printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
-#if 0 // waste of time / this will detect very few errors
-                    {
-                        const int abs_level= FFABS(level);
-                        const int run1= run - rl->max_run[last][abs_level] - run_diff;
-                        if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
-                            if(abs_level <= rl->max_level[last][run]){
-                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
-                                return DECODING_AC_LOST;
-                            }
-                            if(abs_level <= rl->max_level[last][run]*2){
-                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
-                                return DECODING_AC_LOST;
-                            }
-                            if(run1>=0 && abs_level <= rl->max_level[last][run1]){
-                                av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
-                                return DECODING_AC_LOST;
-                            }
-                        }
-                    }
-#endif
-                    //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
-                    if (level>0) level= level * qmul + qadd;
-                    else         level= level * qmul - qadd;
-#if 0 // waste of time too :(
-                    if(level>2048 || level<-2048){
-                        av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
-                        return DECODING_AC_LOST;
-                    }
-#endif
-                    i+= run + 1;
-                    if(last) i+=192;
-#ifdef ERROR_DETAILS
-                if(run==66)
-                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
-                else if((i>62 && i<192) || i>192+63)
-                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
-#endif
-                } else {
-                    /* second escape */
-#if MIN_CACHE_BITS < 23
-                    LAST_SKIP_BITS(re, &s->gb, 2);
-                    UPDATE_CACHE(re, &s->gb);
-#else
-                    SKIP_BITS(re, &s->gb, 2);
-#endif
-                    GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
-                    i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
-                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                    LAST_SKIP_BITS(re, &s->gb, 1);
-#ifdef ERROR_DETAILS
-                if(run==66)
-                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
-                else if((i>62 && i<192) || i>192+63)
-                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
-#endif
-                }
-            } else {
-                /* first escape */
-#if MIN_CACHE_BITS < 22
-                LAST_SKIP_BITS(re, &s->gb, 1);
-                UPDATE_CACHE(re, &s->gb);
-#else
-                SKIP_BITS(re, &s->gb, 1);
-#endif
-                GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
-                i+= run;
-                level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
-#ifdef ERROR_DETAILS
-                if(run==66)
-                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
-                else if((i>62 && i<192) || i>192+63)
-                    av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
-#endif
-            }
-        } else {
-            i+= run;
-            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-            LAST_SKIP_BITS(re, &s->gb, 1);
-#ifdef ERROR_DETAILS
-                if(run==66)
-                    av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
-                else if((i>62 && i<192) || i>192+63)
-                    av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
-#endif
-        }
-        if (i > 62){
-            i-= 192;
-            if(i&(~63)){
-                const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
-                if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
-                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
-                    break;
-                }else{
-                    av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                    return -1;
-                }
-            }
-
-            block[scan_table[i]] = level;
-            break;
-        }
-
-        block[scan_table[i]] = level;
-    }
-    CLOSE_READER(re, &s->gb);
-  }
- not_coded:
-    if (s->mb_intra) {
-        mpeg4_pred_ac(s, block, n, dc_pred_dir);
-        if (s->ac_pred) {
-            i = 63; /* XXX: not optimal */
-        }
-    }
-    if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
-    s->block_last_index[n] = i;
-
-    return 0;
-}
-
-static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
-{
-    int level, pred;
-
-    if(s->msmpeg4_version<=2){
-        if (n < 4) {
-            level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
-        } else {
-            level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
-        }
-        if (level < 0)
-            return -1;
-        level-=256;
-    }else{  //FIXME optimize use unified tables & index
-        if (n < 4) {
-            level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
-        } else {
-            level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
-        }
-        if (level < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
-            return -1;
-        }
-
-        if (level == DC_MAX) {
-            level = get_bits(&s->gb, 8);
-            if (get_bits1(&s->gb))
-                level = -level;
-        } else if (level != 0) {
-            if (get_bits1(&s->gb))
-                level = -level;
-        }
-    }
-
-    if(s->msmpeg4_version==1){
-        int32_t *dc_val;
-        pred = msmpeg4v1_pred_dc(s, n, &dc_val);
-        level += pred;
-
-        /* update predictor */
-        *dc_val= level;
-    }else{
-        int16_t *dc_val;
-        pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
-        level += pred;
-
-        /* update predictor */
-        if (n < 4) {
-            *dc_val = level * s->y_dc_scale;
-        } else {
-            *dc_val = level * s->c_dc_scale;
-        }
-    }
-
-    return level;
-}
-
-static int msmpeg4_decode_motion(MpegEncContext * s,
-                                 int *mx_ptr, int *my_ptr)
-{
-    MVTable *mv;
-    int code, mx, my;
-
-    mv = &mv_tables[s->mv_table_index];
-
-    code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
-    if (code < 0){
-        av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
-        return -1;
-    }
-    if (code == mv->n) {
-//printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
-        mx = get_bits(&s->gb, 6);
-        my = get_bits(&s->gb, 6);
-    } else {
-        mx = mv->table_mvx[code];
-        my = mv->table_mvy[code];
-    }
-
-    mx += *mx_ptr - 32;
-    my += *my_ptr - 32;
-    /* WARNING : they do not do exactly modulo encoding */
-    if (mx <= -64)
-        mx += 64;
-    else if (mx >= 64)
-        mx -= 64;
-
-    if (my <= -64)
-        my += 64;
-    else if (my >= 64)
-        my -= 64;
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return 0;
-}
-
-/* cleanest way to support it
- * there is too much shared between versions so that we cant have 1 file per version & 1 common
- * as allmost everything would be in the common file
- */
-#include "wmv2.c"
--- a/src/ffmpeg/libavcodec/wmv2.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,855 +0,0 @@
-/*
- * Copyright (c) 2002 The FFmpeg Project.
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
-
-/**
- * @file wmv2.c
- * wmv2 codec.
- */
-
-#include "simple_idct.h"
-
-#define SKIP_TYPE_NONE 0
-#define SKIP_TYPE_MPEG 1
-#define SKIP_TYPE_ROW  2
-#define SKIP_TYPE_COL  3
-
-
-typedef struct Wmv2Context{
-    MpegEncContext s;
-    int j_type_bit;
-    int j_type;
-    int flag3;
-    int flag63;
-    int abt_flag;
-    int abt_type;
-    int abt_type_table[6];
-    int per_mb_abt;
-    int per_block_abt;
-    int mspel_bit;
-    int cbp_table_index;
-    int top_left_mv_flag;
-    int per_mb_rl_bit;
-    int skip_type;
-    int hshift;
-
-    ScanTable abt_scantable[2];
-    DECLARE_ALIGNED_8(DCTELEM, abt_block2[6][64]);
-}Wmv2Context;
-
-static void wmv2_common_init(Wmv2Context * w){
-    MpegEncContext * const s= &w->s;
-
-    ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[0], wmv2_scantableA);
-    ff_init_scantable(s->dsp.idct_permutation, &w->abt_scantable[1], wmv2_scantableB);
-}
-
-#ifdef CONFIG_ENCODERS
-
-static int encode_ext_header(Wmv2Context *w){
-    MpegEncContext * const s= &w->s;
-    PutBitContext pb;
-    int code;
-
-    init_put_bits(&pb, s->avctx->extradata, s->avctx->extradata_size);
-
-    put_bits(&pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29
-    put_bits(&pb, 11, FFMIN(s->bit_rate/1024, 2047));
-
-    put_bits(&pb, 1, w->mspel_bit=1);
-    put_bits(&pb, 1, w->flag3=1);
-    put_bits(&pb, 1, w->abt_flag=1);
-    put_bits(&pb, 1, w->j_type_bit=1);
-    put_bits(&pb, 1, w->top_left_mv_flag=0);
-    put_bits(&pb, 1, w->per_mb_rl_bit=1);
-    put_bits(&pb, 3, code=1);
-
-    flush_put_bits(&pb);
-
-    s->slice_height = s->mb_height / code;
-
-    return 0;
-}
-
-static int wmv2_encode_init(AVCodecContext *avctx){
-    Wmv2Context * const w= avctx->priv_data;
-
-    if(MPV_encode_init(avctx) < 0)
-        return -1;
-
-    wmv2_common_init(w);
-
-    avctx->extradata_size= 4;
-    avctx->extradata= av_mallocz(avctx->extradata_size + 10);
-    encode_ext_header(w);
-
-    return 0;
-}
-
-#if 0 /* unused, remove? */
-static int wmv2_encode_end(AVCodecContext *avctx){
-
-    if(MPV_encode_end(avctx) < 0)
-        return -1;
-
-    avctx->extradata_size= 0;
-    av_freep(&avctx->extradata);
-
-    return 0;
-}
-#endif
-
-int ff_wmv2_encode_picture_header(MpegEncContext * s, int picture_number)
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-
-    put_bits(&s->pb, 1, s->pict_type - 1);
-    if(s->pict_type == I_TYPE){
-        put_bits(&s->pb, 7, 0);
-    }
-    put_bits(&s->pb, 5, s->qscale);
-
-    s->dc_table_index = 1;
-    s->mv_table_index = 1; /* only if P frame */
-//    s->use_skip_mb_code = 1; /* only if P frame */
-    s->per_mb_rl_table = 0;
-    s->mspel= 0;
-    w->per_mb_abt=0;
-    w->abt_type=0;
-    w->j_type=0;
-
-    assert(s->flipflop_rounding);
-
-    if (s->pict_type == I_TYPE) {
-        assert(s->no_rounding==1);
-        if(w->j_type_bit) put_bits(&s->pb, 1, w->j_type);
-
-        if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table);
-
-        if(!s->per_mb_rl_table){
-            code012(&s->pb, s->rl_chroma_table_index);
-            code012(&s->pb, s->rl_table_index);
-        }
-
-        put_bits(&s->pb, 1, s->dc_table_index);
-
-        s->inter_intra_pred= 0;
-    }else{
-        int cbp_index;
-
-        put_bits(&s->pb, 2, SKIP_TYPE_NONE);
-
-        code012(&s->pb, cbp_index=0);
-        if(s->qscale <= 10){
-            int map[3]= {0,2,1};
-            w->cbp_table_index= map[cbp_index];
-        }else if(s->qscale <= 20){
-            int map[3]= {1,0,2};
-            w->cbp_table_index= map[cbp_index];
-        }else{
-            int map[3]= {2,1,0};
-            w->cbp_table_index= map[cbp_index];
-        }
-
-        if(w->mspel_bit) put_bits(&s->pb, 1, s->mspel);
-
-        if(w->abt_flag){
-            put_bits(&s->pb, 1, w->per_mb_abt^1);
-            if(!w->per_mb_abt){
-                code012(&s->pb, w->abt_type);
-            }
-        }
-
-        if(w->per_mb_rl_bit) put_bits(&s->pb, 1, s->per_mb_rl_table);
-
-        if(!s->per_mb_rl_table){
-            code012(&s->pb, s->rl_table_index);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-        put_bits(&s->pb, 1, s->dc_table_index);
-        put_bits(&s->pb, 1, s->mv_table_index);
-
-        s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
-    }
-    s->esc3_level_length= 0;
-    s->esc3_run_length= 0;
-
-    return 0;
-}
-
-// nearly idential to wmv1 but thats just because we dont use the useless M$ crap features
-// its duplicated here in case someone wants to add support for these carp features
-void ff_wmv2_encode_mb(MpegEncContext * s,
-                       DCTELEM block[6][64],
-                       int motion_x, int motion_y)
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-    int cbp, coded_cbp, i;
-    int pred_x, pred_y;
-    uint8_t *coded_block;
-
-    handle_slices(s);
-
-    if (!s->mb_intra) {
-        /* compute cbp */
-        cbp = 0;
-        for (i = 0; i < 6; i++) {
-            if (s->block_last_index[i] >= 0)
-                cbp |= 1 << (5 - i);
-        }
-
-        put_bits(&s->pb,
-                 wmv2_inter_table[w->cbp_table_index][cbp + 64][1],
-                 wmv2_inter_table[w->cbp_table_index][cbp + 64][0]);
-
-        /* motion vector */
-        h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
-        msmpeg4_encode_motion(s, motion_x - pred_x,
-                              motion_y - pred_y);
-    } else {
-        /* compute cbp */
-        cbp = 0;
-        coded_cbp = 0;
-        for (i = 0; i < 6; i++) {
-            int val, pred;
-            val = (s->block_last_index[i] >= 1);
-            cbp |= val << (5 - i);
-            if (i < 4) {
-                /* predict value for close blocks only for luma */
-                pred = coded_block_pred(s, i, &coded_block);
-                *coded_block = val;
-                val = val ^ pred;
-            }
-            coded_cbp |= val << (5 - i);
-        }
-#if 0
-        if (coded_cbp)
-            printf("cbp=%x %x\n", cbp, coded_cbp);
-#endif
-
-        if (s->pict_type == I_TYPE) {
-            put_bits(&s->pb,
-                     ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
-        } else {
-            put_bits(&s->pb,
-                     wmv2_inter_table[w->cbp_table_index][cbp][1],
-                     wmv2_inter_table[w->cbp_table_index][cbp][0]);
-        }
-        put_bits(&s->pb, 1, 0);         /* no AC prediction yet */
-        if(s->inter_intra_pred){
-            s->h263_aic_dir=0;
-            put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
-        }
-    }
-
-    for (i = 0; i < 6; i++) {
-        msmpeg4_encode_block(s, block[i], i);
-    }
-}
-#endif //CONFIG_ENCODERS
-
-static void parse_mb_skip(Wmv2Context * w){
-    int mb_x, mb_y;
-    MpegEncContext * const s= &w->s;
-    uint32_t * const mb_type= s->current_picture_ptr->mb_type;
-
-    w->skip_type= get_bits(&s->gb, 2);
-    switch(w->skip_type){
-    case SKIP_TYPE_NONE:
-        for(mb_y=0; mb_y<s->mb_height; mb_y++){
-            for(mb_x=0; mb_x<s->mb_width; mb_x++){
-                mb_type[mb_y*s->mb_stride + mb_x]= MB_TYPE_16x16 | MB_TYPE_L0;
-            }
-        }
-        break;
-    case SKIP_TYPE_MPEG:
-        for(mb_y=0; mb_y<s->mb_height; mb_y++){
-            for(mb_x=0; mb_x<s->mb_width; mb_x++){
-                mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
-            }
-        }
-        break;
-    case SKIP_TYPE_ROW:
-        for(mb_y=0; mb_y<s->mb_height; mb_y++){
-            if(get_bits1(&s->gb)){
-                for(mb_x=0; mb_x<s->mb_width; mb_x++){
-                    mb_type[mb_y*s->mb_stride + mb_x]=  MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
-                }
-            }else{
-                for(mb_x=0; mb_x<s->mb_width; mb_x++){
-                    mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
-                }
-            }
-        }
-        break;
-    case SKIP_TYPE_COL:
-        for(mb_x=0; mb_x<s->mb_width; mb_x++){
-            if(get_bits1(&s->gb)){
-                for(mb_y=0; mb_y<s->mb_height; mb_y++){
-                    mb_type[mb_y*s->mb_stride + mb_x]=  MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
-                }
-            }else{
-                for(mb_y=0; mb_y<s->mb_height; mb_y++){
-                    mb_type[mb_y*s->mb_stride + mb_x]= (get_bits1(&s->gb) ? MB_TYPE_SKIP : 0) | MB_TYPE_16x16 | MB_TYPE_L0;
-                }
-            }
-        }
-        break;
-    }
-}
-
-static int decode_ext_header(Wmv2Context *w){
-    MpegEncContext * const s= &w->s;
-    GetBitContext gb;
-    int fps;
-    int code;
-
-    if(s->avctx->extradata_size<4) return -1;
-
-    init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
-
-    fps                = get_bits(&gb, 5);
-    s->bit_rate        = get_bits(&gb, 11)*1024;
-    w->mspel_bit       = get_bits1(&gb);
-    w->flag3           = get_bits1(&gb);
-    w->abt_flag        = get_bits1(&gb);
-    w->j_type_bit      = get_bits1(&gb);
-    w->top_left_mv_flag= get_bits1(&gb);
-    w->per_mb_rl_bit   = get_bits1(&gb);
-    code               = get_bits(&gb, 3);
-
-    if(code==0) return -1;
-
-    s->slice_height = s->mb_height / code;
-
-    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
-        av_log(s->avctx, AV_LOG_DEBUG, "fps:%d, br:%d, qpbit:%d, abt_flag:%d, j_type_bit:%d, tl_mv_flag:%d, mbrl_bit:%d, code:%d, flag3:%d, slices:%d\n",
-        fps, s->bit_rate, w->mspel_bit, w->abt_flag, w->j_type_bit, w->top_left_mv_flag, w->per_mb_rl_bit, code, w->flag3,
-        code);
-    }
-    return 0;
-}
-
-int ff_wmv2_decode_picture_header(MpegEncContext * s)
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-    int code;
-
-#if 0
-{
-int i;
-for(i=0; i<s->gb.size*8; i++)
-    printf("%d", get_bits1(&s->gb));
-//    get_bits1(&s->gb);
-printf("END\n");
-return -1;
-}
-#endif
-    if(s->picture_number==0)
-        decode_ext_header(w);
-
-    s->pict_type = get_bits(&s->gb, 1) + 1;
-    if(s->pict_type == I_TYPE){
-        code = get_bits(&s->gb, 7);
-        av_log(s->avctx, AV_LOG_DEBUG, "I7:%X/\n", code);
-    }
-    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
-    if(s->qscale < 0)
-       return -1;
-
-    return 0;
-}
-
-int ff_wmv2_decode_secondary_picture_header(MpegEncContext * s)
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-
-    if (s->pict_type == I_TYPE) {
-        if(w->j_type_bit) w->j_type= get_bits1(&s->gb);
-        else              w->j_type= 0; //FIXME check
-
-        if(!w->j_type){
-            if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
-            else                 s->per_mb_rl_table= 0;
-
-            if(!s->per_mb_rl_table){
-                s->rl_chroma_table_index = decode012(&s->gb);
-                s->rl_table_index = decode012(&s->gb);
-            }
-
-            s->dc_table_index = get_bits1(&s->gb);
-        }
-        s->inter_intra_pred= 0;
-        s->no_rounding = 1;
-        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
-            av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d j_type:%d \n",
-                s->qscale,
-                s->rl_chroma_table_index,
-                s->rl_table_index,
-                s->dc_table_index,
-                s->per_mb_rl_table,
-                w->j_type);
-        }
-    }else{
-        int cbp_index;
-        w->j_type=0;
-
-        parse_mb_skip(w);
-        cbp_index= decode012(&s->gb);
-        if(s->qscale <= 10){
-            int map[3]= {0,2,1};
-            w->cbp_table_index= map[cbp_index];
-        }else if(s->qscale <= 20){
-            int map[3]= {1,0,2};
-            w->cbp_table_index= map[cbp_index];
-        }else{
-            int map[3]= {2,1,0};
-            w->cbp_table_index= map[cbp_index];
-        }
-
-        if(w->mspel_bit) s->mspel= get_bits1(&s->gb);
-        else             s->mspel= 0; //FIXME check
-
-        if(w->abt_flag){
-            w->per_mb_abt= get_bits1(&s->gb)^1;
-            if(!w->per_mb_abt){
-                w->abt_type= decode012(&s->gb);
-            }
-        }
-
-        if(w->per_mb_rl_bit) s->per_mb_rl_table= get_bits1(&s->gb);
-        else                 s->per_mb_rl_table= 0;
-
-        if(!s->per_mb_rl_table){
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-
-        s->dc_table_index = get_bits1(&s->gb);
-        s->mv_table_index = get_bits1(&s->gb);
-
-        s->inter_intra_pred= 0;//(s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
-        s->no_rounding ^= 1;
-
-        if(s->avctx->debug&FF_DEBUG_PICT_INFO){
-            av_log(s->avctx, AV_LOG_DEBUG, "rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d mspel:%d per_mb_abt:%d abt_type:%d cbp:%d ii:%d\n",
-                s->rl_table_index,
-                s->rl_chroma_table_index,
-                s->dc_table_index,
-                s->mv_table_index,
-                s->per_mb_rl_table,
-                s->qscale,
-                s->mspel,
-                w->per_mb_abt,
-                w->abt_type,
-                w->cbp_table_index,
-                s->inter_intra_pred);
-        }
-    }
-    s->esc3_level_length= 0;
-    s->esc3_run_length= 0;
-
-s->picture_number++; //FIXME ?
-
-
-//    if(w->j_type)
-//        return wmv2_decode_j_picture(w); //FIXME
-
-    if(w->j_type){
-        av_log(s->avctx, AV_LOG_ERROR, "J-type picture is not supported\n");
-        return -1;
-    }
-
-    return 0;
-}
-
-static inline int wmv2_decode_motion(Wmv2Context *w, int *mx_ptr, int *my_ptr){
-    MpegEncContext * const s= &w->s;
-    int ret;
-
-    ret= msmpeg4_decode_motion(s, mx_ptr, my_ptr);
-
-    if(ret<0) return -1;
-
-    if((((*mx_ptr)|(*my_ptr)) & 1) && s->mspel)
-        w->hshift= get_bits1(&s->gb);
-    else
-        w->hshift= 0;
-
-//printf("%d %d  ", *mx_ptr, *my_ptr);
-
-    return 0;
-}
-
-static int16_t *wmv2_pred_motion(Wmv2Context *w, int *px, int *py){
-    MpegEncContext * const s= &w->s;
-    int xy, wrap, diff, type;
-    int16_t *A, *B, *C, *mot_val;
-
-    wrap = s->b8_stride;
-    xy = s->block_index[0];
-
-    mot_val = s->current_picture.motion_val[0][xy];
-
-    A = s->current_picture.motion_val[0][xy - 1];
-    B = s->current_picture.motion_val[0][xy - wrap];
-    C = s->current_picture.motion_val[0][xy + 2 - wrap];
-
-    if(s->mb_x && !s->first_slice_line && !s->mspel && w->top_left_mv_flag)
-        diff= FFMAX(FFABS(A[0] - B[0]), FFABS(A[1] - B[1]));
-    else
-        diff=0;
-
-    if(diff >= 8)
-        type= get_bits1(&s->gb);
-    else
-        type= 2;
-
-    if(type == 0){
-        *px= A[0];
-        *py= A[1];
-    }else if(type == 1){
-        *px= B[0];
-        *py= B[1];
-    }else{
-        /* special case for first (slice) line */
-        if (s->first_slice_line) {
-            *px = A[0];
-            *py = A[1];
-        } else {
-            *px = mid_pred(A[0], B[0], C[0]);
-            *py = mid_pred(A[1], B[1], C[1]);
-        }
-    }
-
-    return mot_val;
-}
-
-static inline int wmv2_decode_inter_block(Wmv2Context *w, DCTELEM *block, int n, int cbp){
-    MpegEncContext * const s= &w->s;
-    static const int sub_cbp_table[3]= {2,3,1};
-    int sub_cbp;
-
-    if(!cbp){
-        s->block_last_index[n] = -1;
-
-        return 0;
-    }
-
-    if(w->per_block_abt)
-        w->abt_type= decode012(&s->gb);
-#if 0
-    if(w->per_block_abt)
-        printf("B%d", w->abt_type);
-#endif
-    w->abt_type_table[n]= w->abt_type;
-
-    if(w->abt_type){
-//        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].permutated;
-        const uint8_t *scantable= w->abt_scantable[w->abt_type-1].scantable;
-//        const uint8_t *scantable= w->abt_type-1 ? w->abt_scantable[1].permutated : w->abt_scantable[0].scantable;
-
-        sub_cbp= sub_cbp_table[ decode012(&s->gb) ];
-//        printf("S%d", sub_cbp);
-
-        if(sub_cbp&1){
-            if (msmpeg4_decode_block(s, block, n, 1, scantable) < 0)
-                return -1;
-        }
-
-        if(sub_cbp&2){
-            if (msmpeg4_decode_block(s, w->abt_block2[n], n, 1, scantable) < 0)
-                return -1;
-        }
-        s->block_last_index[n] = 63;
-
-        return 0;
-    }else{
-        return msmpeg4_decode_block(s, block, n, 1, s->inter_scantable.permutated);
-    }
-}
-
-static void wmv2_add_block(Wmv2Context *w, DCTELEM *block1, uint8_t *dst, int stride, int n){
-    MpegEncContext * const s= &w->s;
-
-  if (s->block_last_index[n] >= 0) {
-    switch(w->abt_type_table[n]){
-    case 0:
-        s->dsp.idct_add (dst, stride, block1);
-        break;
-    case 1:
-        simple_idct84_add(dst           , stride, block1);
-        simple_idct84_add(dst + 4*stride, stride, w->abt_block2[n]);
-        memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM));
-        break;
-    case 2:
-        simple_idct48_add(dst           , stride, block1);
-        simple_idct48_add(dst + 4       , stride, w->abt_block2[n]);
-        memset(w->abt_block2[n], 0, 64*sizeof(DCTELEM));
-        break;
-    default:
-        av_log(s->avctx, AV_LOG_ERROR, "internal error in WMV2 abt\n");
-    }
-  }
-}
-
-void ff_wmv2_add_mb(MpegEncContext *s, DCTELEM block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr){
-    Wmv2Context * const w= (Wmv2Context*)s;
-
-    wmv2_add_block(w, block1[0], dest_y                    , s->linesize, 0);
-    wmv2_add_block(w, block1[1], dest_y + 8                , s->linesize, 1);
-    wmv2_add_block(w, block1[2], dest_y +     8*s->linesize, s->linesize, 2);
-    wmv2_add_block(w, block1[3], dest_y + 8 + 8*s->linesize, s->linesize, 3);
-
-    if(s->flags&CODEC_FLAG_GRAY) return;
-
-    wmv2_add_block(w, block1[4], dest_cb                   , s->uvlinesize, 4);
-    wmv2_add_block(w, block1[5], dest_cr                   , s->uvlinesize, 5);
-}
-
-void ff_mspel_motion(MpegEncContext *s,
-                               uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
-                               uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
-                               int motion_x, int motion_y, int h)
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-    uint8_t *ptr;
-    int dxy, offset, mx, my, src_x, src_y, v_edge_pos, linesize, uvlinesize;
-    int emu=0;
-
-    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
-    dxy = 2*dxy + w->hshift;
-    src_x = s->mb_x * 16 + (motion_x >> 1);
-    src_y = s->mb_y * 16 + (motion_y >> 1);
-
-    /* WARNING: do no forget half pels */
-    v_edge_pos = s->v_edge_pos;
-    src_x = clip(src_x, -16, s->width);
-    src_y = clip(src_y, -16, s->height);
-    linesize   = s->linesize;
-    uvlinesize = s->uvlinesize;
-    ptr = ref_picture[0] + (src_y * linesize) + src_x;
-
-    if(s->flags&CODEC_FLAG_EMU_EDGE){
-        if(src_x<1 || src_y<1 || src_x + 17  >= s->h_edge_pos
-                              || src_y + h+1 >= v_edge_pos){
-            ff_emulated_edge_mc(s->edge_emu_buffer, ptr - 1 - s->linesize, s->linesize, 19, 19,
-                             src_x-1, src_y-1, s->h_edge_pos, s->v_edge_pos);
-            ptr= s->edge_emu_buffer + 1 + s->linesize;
-            emu=1;
-        }
-    }
-
-    s->dsp.put_mspel_pixels_tab[dxy](dest_y             , ptr             , linesize);
-    s->dsp.put_mspel_pixels_tab[dxy](dest_y+8           , ptr+8           , linesize);
-    s->dsp.put_mspel_pixels_tab[dxy](dest_y  +8*linesize, ptr  +8*linesize, linesize);
-    s->dsp.put_mspel_pixels_tab[dxy](dest_y+8+8*linesize, ptr+8+8*linesize, linesize);
-
-    if(s->flags&CODEC_FLAG_GRAY) return;
-
-    if (s->out_format == FMT_H263) {
-        dxy = 0;
-        if ((motion_x & 3) != 0)
-            dxy |= 1;
-        if ((motion_y & 3) != 0)
-            dxy |= 2;
-        mx = motion_x >> 2;
-        my = motion_y >> 2;
-    } else {
-        mx = motion_x / 2;
-        my = motion_y / 2;
-        dxy = ((my & 1) << 1) | (mx & 1);
-        mx >>= 1;
-        my >>= 1;
-    }
-
-    src_x = s->mb_x * 8 + mx;
-    src_y = s->mb_y * 8 + my;
-    src_x = clip(src_x, -8, s->width >> 1);
-    if (src_x == (s->width >> 1))
-        dxy &= ~1;
-    src_y = clip(src_y, -8, s->height >> 1);
-    if (src_y == (s->height >> 1))
-        dxy &= ~2;
-    offset = (src_y * uvlinesize) + src_x;
-    ptr = ref_picture[1] + offset;
-    if(emu){
-        ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
-                         src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
-        ptr= s->edge_emu_buffer;
-    }
-    pix_op[1][dxy](dest_cb, ptr, uvlinesize, h >> 1);
-
-    ptr = ref_picture[2] + offset;
-    if(emu){
-        ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
-                         src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
-        ptr= s->edge_emu_buffer;
-    }
-    pix_op[1][dxy](dest_cr, ptr, uvlinesize, h >> 1);
-}
-
-
-static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
-{
-    Wmv2Context * const w= (Wmv2Context*)s;
-    int cbp, code, i;
-    uint8_t *coded_val;
-
-    if(w->j_type) return 0;
-
-    if (s->pict_type == P_TYPE) {
-        if(IS_SKIP(s->current_picture.mb_type[s->mb_y * s->mb_stride + s->mb_x])){
-            /* skip mb */
-            s->mb_intra = 0;
-            for(i=0;i<6;i++)
-                s->block_last_index[i] = -1;
-            s->mv_dir = MV_DIR_FORWARD;
-            s->mv_type = MV_TYPE_16X16;
-            s->mv[0][0][0] = 0;
-            s->mv[0][0][1] = 0;
-            s->mb_skipped = 1;
-            w->hshift=0;
-            return 0;
-        }
-
-        code = get_vlc2(&s->gb, mb_non_intra_vlc[w->cbp_table_index].table, MB_NON_INTRA_VLC_BITS, 3);
-        if (code < 0)
-            return -1;
-        s->mb_intra = (~code & 0x40) >> 6;
-
-        cbp = code & 0x3f;
-    } else {
-        s->mb_intra = 1;
-        code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
-        if (code < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "II-cbp illegal at %d %d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        /* predict coded block pattern */
-        cbp = 0;
-        for(i=0;i<6;i++) {
-            int val = ((code >> (5 - i)) & 1);
-            if (i < 4) {
-                int pred = coded_block_pred(s, i, &coded_val);
-                val = val ^ pred;
-                *coded_val = val;
-            }
-            cbp |= val << (5 - i);
-        }
-    }
-
-    if (!s->mb_intra) {
-        int mx, my;
-//printf("P at %d %d\n", s->mb_x, s->mb_y);
-        wmv2_pred_motion(w, &mx, &my);
-
-        if(cbp){
-            s->dsp.clear_blocks(s->block[0]);
-            if(s->per_mb_rl_table){
-                s->rl_table_index = decode012(&s->gb);
-                s->rl_chroma_table_index = s->rl_table_index;
-            }
-
-            if(w->abt_flag && w->per_mb_abt){
-                w->per_block_abt= get_bits1(&s->gb);
-                if(!w->per_block_abt)
-                    w->abt_type= decode012(&s->gb);
-            }else
-                w->per_block_abt=0;
-        }
-
-        if (wmv2_decode_motion(w, &mx, &my) < 0)
-            return -1;
-
-        s->mv_dir = MV_DIR_FORWARD;
-        s->mv_type = MV_TYPE_16X16;
-        s->mv[0][0][0] = mx;
-        s->mv[0][0][1] = my;
-
-        for (i = 0; i < 6; i++) {
-            if (wmv2_decode_inter_block(w, block[i], i, (cbp >> (5 - i)) & 1) < 0)
-            {
-                av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding inter block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
-                return -1;
-            }
-        }
-    } else {
-//if(s->pict_type==P_TYPE)
-//   printf("%d%d ", s->inter_intra_pred, cbp);
-//printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
-        s->ac_pred = get_bits1(&s->gb);
-        if(s->inter_intra_pred){
-            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
-//            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
-        }
-        if(s->per_mb_rl_table && cbp){
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-
-        s->dsp.clear_blocks(s->block[0]);
-        for (i = 0; i < 6; i++) {
-            if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
-            {
-                av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding intra block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
-                return -1;
-            }
-        }
-    }
-
-    return 0;
-}
-
-static int wmv2_decode_init(AVCodecContext *avctx){
-    Wmv2Context * const w= avctx->priv_data;
-
-    if(ff_h263_decode_init(avctx) < 0)
-        return -1;
-
-    wmv2_common_init(w);
-
-    return 0;
-}
-
-AVCodec wmv2_decoder = {
-    "wmv2",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_WMV2,
-    sizeof(Wmv2Context),
-    wmv2_decode_init,
-    NULL,
-    ff_h263_decode_end,
-    ff_h263_decode_frame,
-    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
-};
-
-#ifdef CONFIG_ENCODERS
-AVCodec wmv2_encoder = {
-    "wmv2",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_WMV2,
-    sizeof(Wmv2Context),
-    wmv2_encode_init,
-    MPV_encode_picture,
-    MPV_encode_end,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
-};
-#endif
--- a/src/ffmpeg/libavcodec/x264.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,295 +0,0 @@
-/*
- * H.264 encoding using the x264 library
- * Copyright (C) 2005  Mans Rullgard <mru@inprovide.com>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "avcodec.h"
-#include <x264.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-typedef struct X264Context {
-    x264_param_t params;
-    x264_t *enc;
-    x264_picture_t pic;
-    AVFrame out_pic;
-} X264Context;
-
-static void
-X264_log(void *p, int level, const char *fmt, va_list args)
-{
-    static const int level_map[] = {
-        [X264_LOG_ERROR]   = AV_LOG_ERROR,
-        [X264_LOG_WARNING] = AV_LOG_ERROR,
-        [X264_LOG_INFO]    = AV_LOG_INFO,
-        [X264_LOG_DEBUG]   = AV_LOG_DEBUG
-    };
-
-    if(level < 0 || level > X264_LOG_DEBUG)
-        return;
-
-    av_vlog(p, level_map[level], fmt, args);
-}
-
-
-static int
-encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
-{
-    uint8_t *p = buf;
-    int i;
-
-    for(i = 0; i < nnal; i++){
-        int s = x264_nal_encode(p, &size, 1, nals + i);
-        if(s < 0)
-            return -1;
-        p += s;
-    }
-
-    return p - buf;
-}
-
-extern int
-X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
-{
-    X264Context *x4 = ctx->priv_data;
-    AVFrame *frame = data;
-    x264_nal_t *nal;
-    int nnal, i;
-    x264_picture_t pic_out;
-
-    x4->pic.img.i_csp = X264_CSP_I420;
-    x4->pic.img.i_plane = 3;
-
-    for(i = 0; i < 3; i++){
-        x4->pic.img.plane[i] = frame->data[i];
-        x4->pic.img.i_stride[i] = frame->linesize[i];
-    }
-
-    x4->pic.i_pts = frame->pts;
-    x4->pic.i_type = X264_TYPE_AUTO;
-
-    if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out))
-        return -1;
-
-    bufsize = encode_nals(buf, bufsize, nal, nnal);
-    if(bufsize < 0)
-        return -1;
-
-    /* FIXME: dts */
-    x4->out_pic.pts = pic_out.i_pts;
-
-    switch(pic_out.i_type){
-    case X264_TYPE_IDR:
-    case X264_TYPE_I:
-        x4->out_pic.pict_type = FF_I_TYPE;
-        break;
-    case X264_TYPE_P:
-        x4->out_pic.pict_type = FF_P_TYPE;
-        break;
-    case X264_TYPE_B:
-    case X264_TYPE_BREF:
-        x4->out_pic.pict_type = FF_B_TYPE;
-        break;
-    }
-
-    x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
-    x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
-
-    return bufsize;
-}
-
-static int
-X264_close(AVCodecContext *avctx)
-{
-    X264Context *x4 = avctx->priv_data;
-
-    if(x4->enc)
-        x264_encoder_close(x4->enc);
-
-    return 0;
-}
-
-extern int
-X264_init(AVCodecContext *avctx)
-{
-    X264Context *x4 = avctx->priv_data;
-
-    x264_param_default(&x4->params);
-
-    x4->params.pf_log = X264_log;
-    x4->params.p_log_private = avctx;
-
-    x4->params.i_keyint_max = avctx->gop_size;
-    x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
-    x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
-    x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
-    x4->params.rc.b_stat_write = (avctx->flags & CODEC_FLAG_PASS1);
-    if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1;
-    else{
-        if(avctx->crf){
-            x4->params.rc.i_rc_method = X264_RC_CRF;
-            x4->params.rc.f_rf_constant = avctx->crf;
-        }else if(avctx->cqp > -1){
-            x4->params.rc.i_rc_method = X264_RC_CQP;
-            x4->params.rc.i_qp_constant = avctx->cqp;
-        }
-    }
-
-    // if neither crf nor cqp modes are selected we have to enable the RC
-    // we do it this way because we cannot check if the bitrate has been set
-    if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR;
-
-    x4->params.i_bframe = avctx->max_b_frames;
-    x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
-    x4->params.b_bframe_adaptive = avctx->b_frame_strategy;
-    x4->params.i_bframe_bias = avctx->bframebias;
-    x4->params.b_bframe_pyramid = (avctx->flags2 & CODEC_FLAG2_BPYRAMID);
-    avctx->has_b_frames= (avctx->flags2 & CODEC_FLAG2_BPYRAMID) ? 2 : !!avctx->max_b_frames;
-
-    x4->params.i_keyint_min = avctx->keyint_min;
-    if(x4->params.i_keyint_min > x4->params.i_keyint_max)
-        x4->params.i_keyint_min = x4->params.i_keyint_max;
-
-    x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
-
-    x4->params.b_deblocking_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER);
-    x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
-    x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
-
-    x4->params.rc.i_qp_min = avctx->qmin;
-    x4->params.rc.i_qp_max = avctx->qmax;
-    x4->params.rc.i_qp_step = avctx->max_qdiff;
-
-    x4->params.rc.f_qcompress = avctx->qcompress;  /* 0.0 => cbr, 1.0 => constant qp */
-    x4->params.rc.f_qblur = avctx->qblur;        /* temporally blur quants */
-    x4->params.rc.f_complexity_blur = avctx->complexityblur;
-
-    x4->params.i_frame_reference = avctx->refs;
-
-    x4->params.i_width = avctx->width;
-    x4->params.i_height = avctx->height;
-    x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
-    x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
-    x4->params.i_fps_num = avctx->time_base.den;
-    x4->params.i_fps_den = avctx->time_base.num;
-
-    x4->params.analyse.inter = 0;
-    if(avctx->partitions){
-        if(avctx->partitions & X264_PART_I4X4)
-            x4->params.analyse.inter |= X264_ANALYSE_I4x4;
-        if(avctx->partitions & X264_PART_I8X8)
-            x4->params.analyse.inter |= X264_ANALYSE_I8x8;
-        if(avctx->partitions & X264_PART_P8X8)
-            x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16;
-        if(avctx->partitions & X264_PART_P4X4)
-            x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8;
-        if(avctx->partitions & X264_PART_B8X8)
-            x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16;
-    }
-
-    x4->params.analyse.i_direct_mv_pred = avctx->directpred;
-
-    x4->params.analyse.b_weighted_bipred = (avctx->flags2 & CODEC_FLAG2_WPRED);
-
-    if(avctx->me_method == ME_EPZS)
-        x4->params.analyse.i_me_method = X264_ME_DIA;
-    else if(avctx->me_method == ME_HEX)
-        x4->params.analyse.i_me_method = X264_ME_HEX;
-    else if(avctx->me_method == ME_UMH)
-        x4->params.analyse.i_me_method = X264_ME_UMH;
-    else if(avctx->me_method == ME_FULL)
-        x4->params.analyse.i_me_method = X264_ME_ESA;
-    else x4->params.analyse.i_me_method = X264_ME_HEX;
-
-    x4->params.analyse.i_me_range = avctx->me_range;
-    x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
-
-    x4->params.analyse.b_bframe_rdo = (avctx->flags2 & CODEC_FLAG2_BRDO);
-    x4->params.analyse.b_mixed_references =
-        (avctx->flags2 & CODEC_FLAG2_MIXED_REFS);
-    x4->params.analyse.b_chroma_me = (avctx->me_cmp & FF_CMP_CHROMA);
-    x4->params.analyse.b_transform_8x8 = (avctx->flags2 & CODEC_FLAG2_8X8DCT);
-    x4->params.analyse.b_fast_pskip = (avctx->flags2 & CODEC_FLAG2_FASTPSKIP);
-
-    x4->params.analyse.i_trellis = avctx->trellis;
-    x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
-
-    if(avctx->level > 0) x4->params.i_level_idc = avctx->level;
-
-    x4->params.rc.f_rate_tolerance =
-        (float)avctx->bit_rate_tolerance/avctx->bit_rate;
-
-    if((avctx->rc_buffer_size != 0) &&
-            (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){
-        x4->params.rc.f_vbv_buffer_init =
-            (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size;
-    }
-    else x4->params.rc.f_vbv_buffer_init = 0.9;
-
-    x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor);
-    x4->params.rc.f_pb_factor = avctx->b_quant_factor;
-    x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
-    x4->params.rc.psz_rc_eq = avctx->rc_eq;
-
-    x4->params.analyse.b_psnr = (avctx->flags & CODEC_FLAG_PSNR);
-    x4->params.i_log_level = X264_LOG_DEBUG;
-
-    x4->params.b_aud = (avctx->flags2 & CODEC_FLAG2_AUD);
-
-    x4->params.i_threads = avctx->thread_count;
-
-    if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
-        x4->params.b_repeat_headers = 0;
-    }
-
-    x4->enc = x264_encoder_open(&x4->params);
-    if(!x4->enc)
-        return -1;
-
-    avctx->coded_frame = &x4->out_pic;
-
-    if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
-        x264_nal_t *nal;
-        int nnal, i, s = 0;
-
-        x264_encoder_headers(x4->enc, &nal, &nnal);
-
-        /* 5 bytes NAL header + worst case escaping */
-        for(i = 0; i < nnal; i++)
-            s += 5 + nal[i].i_payload * 4 / 3;
-
-        avctx->extradata = av_malloc(s);
-        avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal);
-    }
-
-    return 0;
-}
-
-AVCodec x264_encoder = {
-    .name = "h264",
-    .type = CODEC_TYPE_VIDEO,
-    .id = CODEC_ID_H264,
-    .priv_data_size = sizeof(X264Context),
-    .init = X264_init,
-    .encode = X264_frame,
-    .close = X264_close,
-    .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 }
-};
--- a/src/ffmpeg/libavcodec/xvid_internal.h	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-/*
- * copyright (C) 2006 Corey Hickey
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef XVID_INTERNAL_H
-#define XVID_INTERNAL_H
-
-/**
- * @file xvid_internal.h
- * common functions for use with the XviD wrappers
- */
-
-
-int av_tempfile(char *prefix, char **filename);
-
-#endif /* XVID_INTERNAL_H */
--- a/src/ffmpeg/libavcodec/xvid_rc.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,148 +0,0 @@
-/*
- * xvid Rate control wrapper for lavc video encoders
- *
- * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <xvid.h>
-#include <unistd.h>
-#include "avcodec.h"
-#include "xvid_internal.h"
-//#include "dsputil.h"
-#include "mpegvideo.h"
-
-#undef NDEBUG
-#include <assert.h>
-
-extern unsigned int xvid_debug;
-
-int ff_xvid_rate_control_init(MpegEncContext *s){
-    char *tmp_name;
-    int fd, i;
-    xvid_plg_create_t xvid_plg_create;
-    xvid_plugin_2pass2_t xvid_2pass2;
-
-//xvid_debug=-1;
-
-    fd=av_tempfile("xvidrc.", &tmp_name);
-    if (fd == -1) {
-        av_log(NULL, AV_LOG_ERROR, "Can't create temporary pass2 file.\n");
-        return -1;
-    }
-
-    for(i=0; i<s->rc_context.num_entries; i++){
-        static const char *frame_types = " ipbs";
-        char tmp[256];
-        RateControlEntry *rce;
-
-        rce= &s->rc_context.entry[i];
-
-        snprintf(tmp, sizeof(tmp), "%c %d %d %d %d %d %d\n",
-            frame_types[rce->pict_type], (int)lrintf(rce->qscale / FF_QP2LAMBDA), rce->i_count, s->mb_num - rce->i_count - rce->skip_count,
-            rce->skip_count, (rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits+7)/8, (rce->header_bits+rce->mv_bits+7)/8);
-
-//av_log(NULL, AV_LOG_ERROR, "%s\n", tmp);
-        write(fd, tmp, strlen(tmp));
-    }
-
-    close(fd);
-
-    memset(&xvid_2pass2, 0, sizeof(xvid_2pass2));
-    xvid_2pass2.version= XVID_MAKE_VERSION(1,1,0);
-    xvid_2pass2.filename= tmp_name;
-    xvid_2pass2.bitrate= s->avctx->bit_rate;
-    xvid_2pass2.vbv_size= s->avctx->rc_buffer_size;
-    xvid_2pass2.vbv_maxrate= s->avctx->rc_max_rate;
-    xvid_2pass2.vbv_initial= s->avctx->rc_initial_buffer_occupancy;
-
-    memset(&xvid_plg_create, 0, sizeof(xvid_plg_create));
-    xvid_plg_create.version= XVID_MAKE_VERSION(1,1,0);
-    xvid_plg_create.fbase= s->avctx->time_base.den;
-    xvid_plg_create.fincr= s->avctx->time_base.num;
-    xvid_plg_create.param= &xvid_2pass2;
-
-    if(xvid_plugin_2pass2(NULL, XVID_PLG_CREATE, &xvid_plg_create, &s->rc_context.non_lavc_opaque)<0){
-        av_log(NULL, AV_LOG_ERROR, "xvid_plugin_2pass2 failed\n");
-        return -1;
-    }
-    return 0;
-}
-
-float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run){
-    xvid_plg_data_t xvid_plg_data;
-
-    memset(&xvid_plg_data, 0, sizeof(xvid_plg_data));
-    xvid_plg_data.version= XVID_MAKE_VERSION(1,1,0);
-    xvid_plg_data.width = s->width;
-    xvid_plg_data.height= s->height;
-    xvid_plg_data.mb_width = s->mb_width;
-    xvid_plg_data.mb_height= s->mb_height;
-    xvid_plg_data.fbase= s->avctx->time_base.den;
-    xvid_plg_data.fincr= s->avctx->time_base.num;
-    xvid_plg_data.min_quant[0]= s->avctx->qmin;
-    xvid_plg_data.min_quant[1]= s->avctx->qmin;
-    xvid_plg_data.min_quant[2]= s->avctx->qmin; //FIXME i/b factor & offset
-    xvid_plg_data.max_quant[0]= s->avctx->qmax;
-    xvid_plg_data.max_quant[1]= s->avctx->qmax;
-    xvid_plg_data.max_quant[2]= s->avctx->qmax; //FIXME i/b factor & offset
-    xvid_plg_data.bquant_offset = 0; //  100 * s->avctx->b_quant_offset;
-    xvid_plg_data.bquant_ratio = 100; // * s->avctx->b_quant_factor;
-
-#if 0
-    xvid_plg_data.stats.hlength= X
-#endif
-
-    if(!s->rc_context.dry_run_qscale){
-        if(s->picture_number){
-            xvid_plg_data.length=
-            xvid_plg_data.stats.length= (s->frame_bits + 7)/8;
-            xvid_plg_data.frame_num= s->rc_context.last_picture_number;
-            xvid_plg_data.quant= s->qscale;
-
-            xvid_plg_data.type= s->last_pict_type;
-            if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_AFTER, &xvid_plg_data, NULL)){
-                av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_AFTER, ...) FAILED\n");
-                return -1;
-            }
-        }
-        s->rc_context.last_picture_number=
-        xvid_plg_data.frame_num= s->picture_number;
-        xvid_plg_data.quant= 0;
-        if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_BEFORE, &xvid_plg_data, NULL)){
-            av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_BEFORE, ...) FAILED\n");
-            return -1;
-        }
-        s->rc_context.dry_run_qscale= xvid_plg_data.quant;
-    }
-    xvid_plg_data.quant= s->rc_context.dry_run_qscale;
-    if(!dry_run)
-        s->rc_context.dry_run_qscale= 0;
-
-    if(s->pict_type == B_TYPE) //FIXME this is not exactly identical to xvid
-        return xvid_plg_data.quant * FF_QP2LAMBDA * s->avctx->b_quant_factor + s->avctx->b_quant_offset;
-    else
-        return xvid_plg_data.quant * FF_QP2LAMBDA;
-}
-
-void ff_xvid_rate_control_uninit(MpegEncContext *s){
-    xvid_plg_destroy_t xvid_plg_destroy;
-
-    xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_DESTROY, &xvid_plg_destroy, NULL);
-}
-
--- a/src/ffmpeg/libavcodec/xvidff.c	Mon Mar 12 11:31:14 2007 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,768 +0,0 @@
-/*
- * Interface to xvidcore for mpeg4 encoding
- * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/**
- * @file xvidmpeg4.c
- * Interface to xvidcore for MPEG-4 compliant encoding.
- * @author Adam Thayer (krevnik@comcast.net)
- */
-
-#include <xvid.h>
-#include <unistd.h>
-#include "common.h"
-#include "avcodec.h"
-#include "xvid_internal.h"
-
-/**
- * Buffer management macros.
- */
-#define BUFFER_SIZE                 1024
-#define BUFFER_REMAINING(x)         (BUFFER_SIZE - strlen(x))
-#define BUFFER_CAT(x)               (&((x)[strlen(x)]))
-
-/* For PPC Use */
-#if HAVE_ALTIVEC==1
-extern int has_altivec(void);
-#endif
-
-/**
- * Structure for the private XviD context.
- * This stores all the private context for the codec.
- */
-typedef struct xvid_context {
-    void *encoder_handle;          /** Handle for XviD Encoder */
-    int xsize, ysize;              /** Frame size */
-    int vop_flags;                 /** VOP flags for XviD Encoder */
-    int vol_flags;                 /** VOL flags for XviD Encoder */
-    int me_flags;                  /** Motion Estimation flags */
-    int qscale;                    /** Do we use constant scale? */
-    int quicktime_format;          /** Are we in a QT-based format? */
-    AVFrame encoded_picture;       /** Encoded frame information */
-    char *twopassbuffer;           /** Character buffer for two-pass */
-    char *old_twopassbuffer;       /** Old character buffer (two-pass) */
-    char *twopassfile;             /** second pass temp file name */
-    unsigned char *intra_matrix;   /** P-Frame Quant Matrix */
-    unsigned char *inter_matrix;   /** I-Frame Quant Matrix */
-} xvid_context_t;
-
-/**
- * Structure for the private first-pass plugin.
- */
-typedef struct xvid_ff_pass1 {
-    int     version;                /** XviD version */
-    xvid_context_t *context;        /** Pointer to private context */
-} xvid_ff_pass1_t;
-
-/* Prototypes - See function implementation for details */
-int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
-int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
-void xvid_correct_framerate(AVCodecContext *avctx);
-
-/**
- * Creates the private context for the encoder.
- * All buffers are allocated, settings are loaded from the user,
- * and the encoder context created.
- *
- * @param avctx AVCodecContext pointer to context
- * @return Returns 0 on success, -1 on failure
- */
-int ff_xvid_encode_init(AVCodecContext *avctx)  {
-    int xerr, i;
-    int xvid_flags = avctx->flags;
-    xvid_context_t *x = avctx->priv_data;
-    uint16_t *intra, *inter;
-    int fd;
-
-    xvid_plugin_single_t single;
-    xvid_ff_pass1_t rc2pass1;
-    xvid_plugin_2pass2_t rc2pass2;
-    xvid_gbl_init_t xvid_gbl_init;
-    xvid_enc_create_t xvid_enc_create;
-    xvid_enc_plugin_t plugins[7];
-
-    /* Bring in VOP flags from ffmpeg command-line */
-    x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
-    if( xvid_flags & CODEC_FLAG_4MV )
-        x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
-    if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT)
-        x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
-    if( xvid_flags & CODEC_FLAG_AC_PRED )
-        x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
-    if( xvid_flags & CODEC_FLAG_GRAY )
-        x->vop_flags |= XVID_VOP_GREYSCALE;
-
-    /* Decide which ME quality setting to use */
-    x->me_flags = 0;
-    switch( avctx->me_method ) {
-       case ME_FULL:   /* Quality 6 */
-           x->me_flags |=  XVID_ME_EXTSEARCH16
-                       |   XVID_ME_EXTSEARCH8;
-
-       case ME_EPZS:   /* Quality 4 */
-           x->me_flags |=  XVID_ME_ADVANCEDDIAMOND8
-                       |   XVID_ME_HALFPELREFINE8
-                       |   XVID_ME_CHROMA_PVOP
-                       |   XVID_ME_CHROMA_BVOP;
-
-       case ME_LOG:    /* Quality 2 */
-       case ME_PHODS:
-       case ME_X1:
-           x->me_flags |=  XVID_ME_ADVANCEDDIAMOND16
-                       |   XVID_ME_HALFPELREFINE16;
-
-       case ME_ZERO:   /* Quality 0 */
-       default:
-           break;
-    }
-
-    /* Decide how we should decide blocks */
-    switch( avctx->mb_decision ) {
-       case 2:
-           x->vop_flags |= XVID_VOP_MODEDECISION_RD;
-           x->me_flags |=  XVID_ME_HALFPELREFINE8_RD
-                       |   XVID_ME_QUARTERPELREFINE8_RD
-                       |   XVID_ME_EXTSEARCH_RD
-                       |   XVID_ME_CHECKPREDICTION_RD;
-       case 1:
-           if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
-               x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
-           x->me_flags |=  XVID_ME_HALFPELREFINE16_RD
-                       |   XVID_ME_QUARTERPELREFINE16_RD;
-
-       default:
-           break;
-    }
-
-    /* Bring in VOL flags from ffmpeg command-line */
-    x->vol_flags = 0;
-    if( xvid_flags & CODEC_FLAG_GMC ) {
-        x->vol_flags |= XVID_VOL_GMC;
-        x->me_flags |= XVID_ME_GME_REFINE;
-    }
-    if( xvid_flags & CODEC_FLAG_QPEL ) {
-        x->vol_flags |= XVID_VOL_QUARTERPEL;
-        x->me_flags |= XVID_ME_QUARTERPELREFINE16;
-        if( x->vop_flags & XVID_VOP_INTER4V )
-            x->me_flags |= XVID_ME_QUARTERPELREFINE8;
-    }
-
-    memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
-    xvid_gbl_init.version = XVID_VERSION;
-    xvid_gbl_init.debug = 0;
-
-#ifdef ARCH_POWERPC
-    /* XviD's PPC support is borked, use libavcodec to detect */
-#if HAVE_ALTIVEC==1
-    if( has_altivec() ) {
-        xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
-    } else
-#endif
-        xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
-#else
-    /* XviD can detect on x86 */
-    xvid_gbl_init.cpu_flags = 0;
-#endif
-
-    /* Initialize */
-    xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
-
-    /* Create the encoder reference */
-    memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
-    xvid_enc_create.version = XVID_VERSION;
-
-    /* Store the desired frame size */
-    xvid_enc_create.width = x->xsize = avctx->width;
-    xvid_enc_create.height = x->ysize = avctx->height;
-
-    /* XviD can determine the proper profile to use */
-    /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
-
-    /* We don't use zones or threads */
-    xvid_enc_create.zones = NULL;
-    xvid_enc_create.num_zones = 0;
-    xvid_enc_create.num_threads = 0;
-
-    xvid_enc_create.plugins = plugins;
-    xvid_enc_create.num_plugins = 0;
-
-    /* Initialize Buffers */
-    x->twopassbuffer = NULL;
-    x->old_twopassbuffer = NULL;
-    x->twopassfile = NULL;
-
-    if( xvid_flags & CODEC_FLAG_PASS1 ) {
-        memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t));
-        rc2pass1.version = XVID_VERSION;
-        rc2pass1.context = x;
-        x->twopassbuffer = av_malloc(BUFFER_SIZE);
-        x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
-        if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
-            av_log(avctx, AV_LOG_ERROR,
-                "XviD: Cannot allocate 2-pass log buffers\n");
-            return -1;
-        }
-        x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
-
-        plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
-        plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
-        xvid_enc_create.num_plugins++;
-    } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
-        memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
-        rc2pass2.version = XVID_VERSION;
-        rc2pass2.bitrate = avctx->bit_rate;
-
-        fd = av_tempfile("xvidff.", &(x->twopassfile));
-        if( fd == -1 ) {
-            av_log(avctx, AV_LOG_ERROR,
-                "XviD: Cannot write 2-pass pipe\n");
-            return -1;
-        }
-
-        if( avctx->stats_in == NULL ) {
-            av_log(avctx, AV_LOG_ERROR,
-                "XviD: No 2-pass information loaded for second pass\n");
-            return -1;
-        }
-
-        if( strlen(avctx->stats_in) >
-              write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
-            close(fd);
-            av_log(avctx, AV_LOG_ERROR,
-                "XviD: Cannot write to 2-pass pipe\n");
-            return -1;
-        }
-
-        close(fd);
-        rc2pass2.filename = x->twopassfile;
-        plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
-        plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
-        xvid_enc_create.num_plugins++;
-    } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
-        /* Single Pass Bitrate Control! */
-        memset(&single, 0, sizeof(xvid_plugin_single_t));
-        single.version = XVID_VERSION;
-        single.bitrate = avctx->bit_rate;
-
-        plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
-        plugins[xvid_enc_create.num_plugins].param = &single;
-        xvid_enc_create.num_plugins++;
-    }
-
-    /* Luminance Masking */
-    if( 0.0 != avctx->lumi_masking ) {
-        plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
-        plugins[xvid_enc_create.num_plugins].param = NULL;
-        xvid_enc_create.num_plugins++;
-    }
-
-    /* Frame Rate and Key Frames */
-    xvid_correct_framerate(avctx);
-    xvid_enc_create.fincr = avctx->time_base.num;
-    xvid_enc_create.fbase = avctx->time_base.den;
-    if( avctx->gop_size > 0 )
-        xvid_enc_create.max_key_interval = avctx->gop_size;
-    else
-        xvid_enc_create.max_key_interval = 240; /* XviD's best default */
-
-    /* Quants */
-    if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
-    else x->qscale = 0;
-
-    xvid_enc_create.min_quant[0] = avctx->qmin;
-    xvid_enc_create.min_quant[1] = avctx->qmin;
-    xvid_enc_create.min_quant[2] = avctx->qmin;
-    xvid_enc_create.max_quant[0] = avctx->qmax;
-    xvid_enc_create.max_quant[1] = avctx->qmax;
-    xvid_enc_create.max_quant[2] = avctx->qmax;
-
-    /* Quant Matrices */
-    x->intra_matrix = x->inter_matrix = NULL;
-    if( avctx->mpeg_quant )
-       x->vol_flags |= XVID_VOL_MPEGQUANT;
-    if( (avctx->intra_matrix || avctx->inter_matrix) ) {
-       x->vol_flags |= XVID_VOL_MPEGQUANT;
-
-       if( avctx->intra_matrix ) {
-           intra = avctx->intra_matrix;
-           x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
-       } else
-           intra = NULL;
-       if( avctx->inter_matrix ) {
-           inter = avctx->inter_matrix;
-           x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
-       } else
-           inter = NULL;
-
-       for( i = 0; i < 64; i++ ) {
-           if( intra )
-               x->intra_matrix[i] = (unsigned char)intra[i];
-           if( inter )
-               x->inter_matrix[i] = (unsigned char)inter[i];
-       }
-    }
-
-    /* Misc Settings */
-    xvid_enc_create.frame_drop_ratio = 0;
-    xvid_enc_create.global = 0;
-    if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
-        xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
-
-    /* Determines which codec mode we are operating in */
-    avctx->extradata = NULL;
-    avctx->extradata_size = 0;
-    if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
-        /* In this case, we are claiming to be MPEG4 */
-        x->quicktime_format = 1;
-        avctx->codec_id = CODEC_ID_MPEG4;
-    } else {
-        /* We are claiming to be XviD */
-        x->quicktime_format = 0;
-        if(!avctx->codec_tag)
-            avctx->codec_tag = ff_get_fourcc("xvid");
-    }
-
-    /* Bframes */
-    xvid_enc_create.max_bframes = avctx->max_b_frames;
-    xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
-    xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
-    if( avctx->max_b_frames > 0  && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
-
-    /* Create encoder context */
-    xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
-    if( xerr ) {
-        av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n");
-        return -1;
-    }
-
-    x->encoder_handle = xvid_enc_create.handle;
-    avctx->coded_frame = &x->encoded_picture;
-
-    return 0;
-}
-
-/**
- * Encodes a single frame.
- *
- * @param avctx AVCodecContext pointer to context
- * @param frame Pointer to encoded frame buffer
- * @param buf_size Size of encoded frame buffer
- * @param data Pointer to AVFrame of unencoded frame
- * @return Returns 0 on success, -1 on failure
- */
-int ff_xvid_encode_frame(AVCodecContext *avctx,
-                         unsigned char *frame, int buf_size, void *data) {
-    int xerr, i;
-    char *tmp;
-    xvid_context_t *x = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame *p = &(x->encoded_picture);
-
-    xvid_enc_frame_t xvid_enc_frame;
-    xvid_enc_stats_t xvid_enc_stats;
-
-    /* Start setting up the frame */
-    memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
-    xvid_enc_frame.version = XVID_VERSION;
-    memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
-    xvid_enc_stats.version = XVID_VERSION;
-    *p = *picture;
-
-    /* Let XviD know where to put the frame. */
-    xvid_enc_frame.bitstream = frame;
-    xvid_enc_frame.length = buf_size;
-
-    /* Initialize input image fields */
-    if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
-        av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n");
-        return -1;
-    }
-
-    xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
-
-    for( i = 0; i < 4; i++ ) {
-        xvid_enc_frame.input.plane[i] = picture->data[i];
-        xvid_enc_frame.input.stride[i] = picture->linesize[i];
-    }
-
-    /* Encoder Flags */
-    xvid_enc_frame.vop_flags = x->vop_flags;
-    xvid_enc_frame.vol_flags = x->vol_flags;
-    xvid_enc_frame.motion = x->me_flags;
-    xvid_enc_frame.type = XVID_TYPE_AUTO;
-
-    /* Quant Setting */
-    if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
-    else xvid_enc_frame.quant = 0;
-
-    /* Matrices */
-    xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
-    xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
-
-    /* Encode */
-    xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
-        &xvid_enc_frame, &xvid_enc_stats);
-
-    /* Two-pass log buffer swapping */
-    avctx->stats_out = NULL;
-    if( x->twopassbuffer ) {
-        tmp = x->old_twopassbuffer;
-        x->old_twopassbuffer = x->twopassbuffer;
-        x->twopassbuffer = tmp;
-        x->twopassbuffer[0] = 0;
-        if( x->old_twopassbuffer[0] != 0 ) {
-            avctx->stats_out = x->old_twopassbuffer;
-        }
-    }
-
-    if( 0 <= xerr ) {
-        p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
-        if( xvid_enc_stats.type == XVID_TYPE_PVOP )
-            p->pict_type = FF_P_TYPE;
-        else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
-            p->pict_type = FF_B_TYPE;
-        else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
-            p->pict_type = FF_S_TYPE;
-        else
-            p->pict_type = FF_I_TYPE;
-        if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
-            p->key_frame = 1;
-            if( x->quicktime_format )
-                return xvid_strip_vol_header(avctx, frame,
-                    xvid_enc_stats.hlength, xerr);
-         } else
-            p->key_frame = 0;
-
-        return xerr;
-    } else {
-        av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr);
-        return -1;
-    }
-}
-
-/**
- * Destroys the private context for the encoder.
- * All buffers are freed, and the XviD encoder context is destroyed.
- *
- * @param avctx AVCodecContext pointer to context
- * @return Returns 0, success guaranteed
- */
-int ff_xvid_encode_close(AVCodecContext *avctx) {
-    xvid_context_t *x = avctx->priv_data;
-
-    xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
-
-    if( avctx->extradata != NULL )
-        av_free(avctx->extradata);
-    if( x->twopassbuffer != NULL ) {
-        av_free(x->twopassbuffer);
-        av_free(x->old_twopassbuffer);
-    }
-    if( x->twopassfile != NULL )
-        av_free(x->twopassfile);
-    if( x->intra_matrix != NULL )
-        av_free(x->intra_matrix);
-    if( x->inter_matrix != NULL )
-        av_free(x->inter_matrix);
-
-    return 0;
-}
-
-/**
- * Routine to create a global VO/VOL header for MP4 container.
- * What we do here is extract the header from the XviD bitstream
- * as it is encoded. We also strip the repeated headers from the
- * bitstream when a global header is requested for MPEG-4 ISO
- * compliance.
- *
- * @param avctx AVCodecContext pointer to context
- * @param frame Pointer to encoded frame data
- * @param header_len Length of header to search
- * @param frame_len Length of encoded frame data
- * @return Returns new length of frame data
- */
-int xvid_strip_vol_header(AVCodecContext *avctx,
-                  unsigned char *frame,
-                  unsigned int header_len,
-                  unsigned int frame_len) {
-    int vo_len = 0, i;
-
-    for( i = 0; i < header_len - 3; i++ ) {
-        if( frame[i] == 0x00 &&
-            frame[i+1] == 0x00 &&
-            frame[i+2] == 0x01 &&
-            frame[i+3] == 0xB6 ) {
-            vo_len = i;
-            break;
-        }
-    }
-
-    if( vo_len > 0 ) {
-        /* We need to store the header, so extract it */
-        if( avctx->extradata == NULL ) {
-            avctx->extradata = av_malloc(vo_len);
-            memcpy(avctx->extradata, frame, vo_len);
-            avctx->extradata_size = vo_len;
-        }
-        /* Less dangerous now, memmove properly copies the two
-           chunks of overlapping data */
-        memmove(frame, &(frame[vo_len]), frame_len - vo_len);
-        return frame_len - vo_len;
-    } else
-        return frame_len;
-}
-
-/**
- * Routine to correct a possibly erroneous framerate being fed to us.
- * XviD currently chokes on framerates where the ticks per frame is
- * extremely large. This function works to correct problems in this area
- * by estimating a new framerate and taking the simpler fraction of
- * the two presented.
- *
- * @param avctx Context that contains the framerate to correct.
- */
-void xvid_correct_framerate(AVCodecContext *avctx) {
-    int frate, fbase;
-    int est_frate, est_fbase;
-    int gcd;
-    float est_fps, fps;
-
-    frate = avctx->time_base.den;
-    fbase = avctx->time_base.num;
-
-    gcd = ff_gcd(frate, fbase);
-    if( gcd > 1 ) {
-        frate /= gcd;
-        fbase /= gcd;
-    }
-
-    if( frate <= 65000 && fbase <= 65000 ) {
-        avctx->time_base.den = frate;
-        avctx->time_base.num = fbase;
-        return;
-    }
-
-    fps = (float)frate / (float)fbase;
-    est_fps = roundf(fps * 1000.0) / 1000.0;
-
-    est_frate = (int)est_fps;
-    if( est_fps > (int)est_fps ) {
-        est_frate = (est_frate + 1) * 1000;
-        est_fbase = (int)roundf((float)est_frate / est_fps);
-    } else
-        est_fbase = 1;
-
-    gcd = ff_gcd(est_frate, est_fbase);
-    if( gcd > 1 ) {
-        est_frate /= gcd;
-        est_fbase /= gcd;
-    }
-
-    if( fbase > est_fbase ) {
-        avctx->time_base.den = est_frate;
-        avctx->time_base.num = est_fbase;
-        av_log(avctx, AV_LOG_DEBUG,
-            "XviD: framerate re-estimated: %.2f, %.3f%% correction\n",
-            est_fps, (((est_fps - fps)/fps) * 100.0));
-    } else {
-        avctx->time_base.den = frate;
-        avctx->time_base.num = fbase;
-    }
-}
-
-/*
- * XviD 2-Pass Kludge Section
- *
- * XviD's default 2-pass doesn't allow us to create data as we need to, so
- * this section spends time replacing the first pass plugin so we can write
- * statistic information as libavcodec requests in. We have another kludge
- * that allows us to pass data to the second pass in XviD without a custom
- * rate-control plugin.
- */
-
-/**
- * Initializes the two-pass plugin and context.
- *
- * @param param Input construction parameter structure
- * @param handle Private context handle
- * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
- */
-static int xvid_ff_2pass_create(xvid_plg_create_t * param,
-                                void ** handle) {
-    xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param;
-    char *log = x->context->twopassbuffer;
-
-    /* Do a quick bounds check */
-    if( log == NULL )
-        return XVID_ERR_FAIL;
-
-    /* We use snprintf() */
-    /* This is because we can safely prevent a buffer overflow */
-    log[0] = 0;
-    snprintf(log, BUFFER_REMAINING(log),
-        "# ffmpeg 2-pass log file, using xvid codec\n");
-    snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
-        "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
-        XVID_VERSION_MAJOR(XVID_VERSION),
-        XVID_VERSION_MINOR(XVID_VERSION),
-        XVID_VERSION_PATCH(XVID_VERSION));
-
-    *handle = x->context;
-    return 0;
-}
-
-/**
- * Destroys the two-pass plugin context.
- *
- * @param ref Context pointer for the plugin
- * @param param Destrooy context
- * @return Returns 0, success guaranteed
- */
-static int xvid_ff_2pass_destroy(xvid_context_t *ref,
-                                xvid_plg_destroy_t *param) {
-    /* Currently cannot think of anything to do on destruction */
-    /* Still, the framework should be here for reference/use */
-    if( ref->twopassbuffer != NULL )
-        ref->twopassbuffer[0] = 0;
-    return 0;
-}
-
-/**
- * Enables fast encode mode during the first pass.
- *
- * @param ref Context pointer for the plugin
- * @param param Frame data
- * @return Returns 0, success guaranteed
- */
-static int xvid_ff_2pass_before(xvid_context_t *ref,
-                                xvid_plg_data_t *param) {
-    int motion_remove;
-    int motion_replacements;
-    int vop_remove;
-
-    /* Nothing to do here, result is changed too much */
-    if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
-        return 0;
-
-    /* We can implement a 'turbo' first pass mode here */
-    param->quant = 2;
-
-    /* Init values */
-    motion_remove = ~XVID_ME_CHROMA_PVOP &
-                    ~XVID_ME_CHROMA_BVOP &
-                    ~XVID_ME_EXTSEARCH16 &
-                    ~XVID_ME_ADVANCEDDIAMOND16;
-    motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
-                          XVID_ME_SKIP_DELTASEARCH |
-                          XVID_ME_FASTREFINE16 |
-                          XVID_ME_BFRAME_EARLYSTOP;
-    vop_remove = ~XVID_VOP_MODEDECISION_RD &
-                 ~XVID_VOP_FAST_MODEDECISION_RD &
-                 ~XVID_VOP_TRELLISQUANT &
-                 ~XVID_VOP_INTER4V &
-                 ~XVID_VOP_HQACPRED;
-
-    param->vol_flags &= ~XVID_VOL_GMC;
-    param->vop_flags &= vop_remove;
-    param->motion_flags &= motion_remove;
-    param->motion_flags |= motion_replacements;
-
-    return 0;
-}
-
-/**
- * Captures statistic data and writes it during first pass.
- *
- * @param ref Context pointer for the plugin
- * @param param Statistic data
- * @return Returns XVID_ERR_xxxx on failure, or 0 on success
- */
-static int xvid_ff_2pass_after(xvid_context_t *ref,
-                                xvid_plg_data_t *param) {
-    char *log = ref->twopassbuffer;
-    char *frame_types = " ipbs";
-    char frame_type;
-
-    /* Quick bounds check */
-    if( log == NULL )
-        return XVID_ERR_FAIL;
-
-    /* Convert the type given to us into a character */
-    if( param->type < 5 && param->type > 0 ) {
-        frame_type = frame_types[param->type];
-    } else {
-        return XVID_ERR_FAIL;
-    }
-
-    snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
-        "%c %d %d %d %d %d %d\n",
-        frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
-        param->stats.ublks, param->stats.length, param->stats.hlength);
-
-    return 0;
-}
-
-/**
- * Dispatch function for our custom plugin.
- * This handles the dispatch for the XviD plugin. It passes data
- * on to other functions for actual processing.
- *
- * @param ref Context pointer for the plugin
- * @param cmd The task given for us to complete
- * @param p1 First parameter (varies)
- * @param p2 Second parameter (varies)
- * @return Returns XVID_ERR_xxxx on failure, or 0 on success
- */
-int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
-    switch( cmd ) {
-        case XVID_PLG_INFO:
-        case XVID_PLG_FRAME:
-            return 0;
-
-        case XVID_PLG_BEFORE:
-            return xvid_ff_2pass_before(ref, p1);
-
-        case XVID_PLG_CREATE:
-            return xvid_ff_2pass_create(p1, p2);
-
-        case XVID_PLG_AFTER:
-            return xvid_ff_2pass_after(ref, p1);
-
-        case XVID_PLG_DESTROY:
-            return xvid_ff_2pass_destroy(ref, p1);
-
-        default:
-            return XVID_ERR_FAIL;
-    }
-}
-
-/**
- * XviD codec definition for libavcodec.
- */
-AVCodec xvid_encoder = {
-    "xvid",
-    CODEC_TYPE_VIDEO,
-    CODEC_ID_XVID,
-    sizeof(xvid_context_t),
-    ff_xvid_encode_init,
-    ff_xvid_encode_frame,
-    ff_xvid_encode_close,
-    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
-};